2 langdon1996 Langdon1996 于 2016.05.06 00:11 提问

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个回答

caozhy
caozhy   Ds   Rxr 2016.05.06 01: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;

}
caozhy
caozhy   Ds   Rxr 2016.05.06 01:02

建筑物名称:5号楼
层数:5
教室(房间)数40
总面积4000
建筑物功能:机房

建筑物名称:宿舍C3
层数:8
教室(房间)数140
总面积1352
可容纳人数:523

qq_26904543
qq_26904543   2016.05.06 08:14

building类中的缺少初始构造函数

就是你应该有无参数传入的构造函数

ZGZ1002
ZGZ1002   2016.05.06 12:38

你的程序中有:string name;所以你要加上头文件#include
其次,你的这句代码TeachBuilding(string n,int f,int r,float w,string func):Building(n,f,r,w)。有些问题

qiang_zi_
qiang_zi_   2016.05.06 00:18

没有错误提示吗?错误提前????

NK_test
NK_test   Rxr 2016.05.06 00:21

string头文件都没加

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!