罗蜜斯丹 2016-05-05 16:11 采纳率: 0%
浏览 1322
已采纳

C++面向对象入门~构造函数编译不通过

一个简单的面向对象入门,在codeblock上,构造函数的参数编译失败


#include <iostream>
using namespace std;
class Building
{
protected:
    string name;
    int floorNum,roomAmount;
    float wholeArea;
public:
    Building(string n,int f,int r,float w)
    {
        this->name=n;
        this->floorNum=f;
        this->roomAmount=r;
        this->wholeArea=w;
    }
    void display()
    {
        cout<<"建筑物名称:"<<name<<endl;
        cout<<"层数:"<<floorNum<<endl;
        cout<<"教室(房间)数"<<roomAmount<<endl;
        cout<<"总面积"<<wholeArea<<endl;
    }
};
class TeachBuilding:protected Building
{
protected:
    string func;
public:
    TeachBuilding(string n,int f,int r,float w,string func):Building(string n,int f,int r,float w)
    {
        this->func=func;
    }
    void display()
    {
        Building::display();
        cout<<"建筑物功能:"<<func<<endl;
    }
};
class DormBuilding :public Building
{
protected:
    int people;
public:
    DormBuilding(string n,int f,int r,float w,int people):Building(string n,int f,int r,float w)
    {
        this->people=people;
    }
    void display()
    {
        Building::display();
        cout<<"可容纳人数:"<<people<<endl;
    }
};
int main()
{
    TeachBuilding TB("5号楼",5,40,4000,"机房");
    TB.display();
    cout<<endl;
    DormBuilding DB("宿舍C3",8,140,1352,523);
    DB.display();
    return 0;

}
  • 写回答

6条回答 默认 最新

  • threenewbee 2016-05-05 17:02
    关注
     #include <iostream>
    using namespace std;
    class Building
    {
    protected:
        string name;
        int floorNum,roomAmount;
        float wholeArea;
    public:
        Building(string n,int f,int r,float w)
        {
            this->name=n;
            this->floorNum=f;
            this->roomAmount=r;
            this->wholeArea=w;
        }
        void display()
        {
            cout<<"建筑物名称:"<<name<<endl;
            cout<<"层数:"<<floorNum<<endl;
            cout<<"教室(房间)数"<<roomAmount<<endl;
            cout<<"总面积"<<wholeArea<<endl;
        }
    };
    class TeachBuilding:protected Building
    {
    protected:
        string func;
    public:
        TeachBuilding(string n,int f,int r,float w,string func):Building(n,f,r,w) //修改
        {
            this->func=func;
        }
        void display()
        {
            Building::display();
            cout<<"建筑物功能:"<<func<<endl;
        }
    };
    class DormBuilding :public Building
    {
    protected:
        int people;
    public:
        DormBuilding(string n,int f,int r,float w,int people):Building(n,f,r,w) //修改
        {
            this->people=people;
        }
        void display()
        {
            Building::display();
            cout<<"可容纳人数:"<<people<<endl;
        }
    };
    int main()
    {
        TeachBuilding TB("5号楼",5,40,4000,"机房");
        TB.display();
        cout<<endl;
        DormBuilding DB("宿舍C3",8,140,1352,523);
        DB.display();
        return 0;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R