改错1
#include <iostream>
#include<string>
using namespace std;
class CBase1
{
public:
CBase1() :a(a)//CBase1不存在默认构造函数,应删去括号内的int a,使系统生成默认构造函数
{
cout << "base1 structure..." << endl;
}
~CBase1()
{
cout << "base1 destructure ..." << endl;
}
void print()
{
cout << "a=" << a << endl;
}
protected:
int a;
};
class CBase2
{
public:
CBase2() :b(b)//CBase2不存在默认构造函数,应删去括号内的int b,使系统生成默认构造函数
{
cout << "base2 structure..." << endl;
}
~CBase2()
{
cout << "base2 destructure..." << endl;
}
void print()
{
cout << "b=" << b << endl;
}
protected:
int b;
};
class CDerive :public CBase1, public CBase2
{
public:
CDerive()
{
cout << "derive structure..." << endl;
}
~CDerive()
{
cout << "derive destructure..." << endl;
}
void print()
{
CBase1::print();
CBase2::print();
b1.print();
b2.print();
cout << "c=" << c << endl;
}
private:
CBase1 b1;
CBase2 b2;
int c;
};
int main()//main 的返回类型应为int
{
CDerive d;
d.print();//调用函数时应加上括号
}
改错2
#include<iostream>
#include<string>
using namespace std;
class CBase
{
public:
CBase(int a) :a(a)
{}
int a;
};
class CDerive1 : public CBase
{
public:
CDerive1(int a) :CBase(a)
{}
};
class CDerive2 : public CBase
{
public:
CDerive2(int a) : CBase(a)
{}
};
class CDerive :public CDerive1, public CDerive2
{
public:
CDerive(int a, int b) :CDerive1(a), CDerive2(b)
{}
};
int main()
{
CDerive d(1, 2);
cout << d.CDerive1::a << endl;
cout << d.CDerive2::a << endl;
}
建立普通的基类building,用来存储一座楼房的层数、房间数以及它的总平方数。建立派生类house,继承building,并存储卧室与浴室的数量,另外,建立派生类office,继承building,并存储灭火器与电话的数目。设计一主函数来测试以上类的用法。
#include <iostream>
#include<string>
using namespace std;
class building
{
public:
building(int f, int r, double a) { floors = f, rooms = r, area = a; }
/*更改处:调用基类内的参数时应同名函数:到相应参数
void Building(int f, int r, int a)
{
floors = f;
rooms = r;
area = a;
}*/
void show()
{
cout << "建筑层数" << floors << endl;
cout << "建筑房间数" << rooms << endl;
cout << "建筑总平方米数" << area << endl;
}
protected:
int floors;
int rooms;
double area;
};
class house :public building
{
public:
house(int f, int r, double a, int bed, int bash) :building(f, r, a)
{
bedrooms = bed;
bashrooms = bash;
}
void show()
{
cout << "建筑层数" << floors << endl;
cout << "建筑房间数" << rooms << endl;
cout << "建筑总平方米数" << area << endl;
cout << "卧室数量" << bedrooms << endl;
cout << "浴室数量" << bashrooms << endl;
}
private:
int bedrooms;
int bashrooms;
};
class office :public building
{
public:
office(int f, int r, double a, int fire, int phone) :building(f, r, a)
{
antifire = fire;
phones = phone;
}
void show()
{
cout << "建筑层数" << floors << endl;
cout << "建筑房间数" << rooms << endl;
cout << "建筑总平方米数" << area << endl;
cout << "灭火器数量" << antifire << endl;
cout << "电话数量" << phones << endl;
}
private:
int antifire;
int phones;
};
int main()
{
building b1(8, 14, 2000.0);
b1.show();
cout << endl;
house h1(10, 20, 5000.0, 10, 20);
h1.show();
cout << endl;
office o1(12, 28, 8000.0, 40,100);
o1.show();@[TOC](这里写自定义目录标题)
}
- 按照下图的类层次结构编写程序,定义属于score的对象c1以及类teacher的对象t1,分别输入各个数据成员的值后再显示出这些数据。
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
person(string n, string i) { name = n, id = i; }
void printper()
{
cout << "姓名:" << name << endl;
cout << "身份证号码:" << id << endl;
}
protected:
string name;
string id;
};
class teacher :public person
{
public:
teacher(string name ,string id,string deg, string dep) :person(name,id){ degree = deg, department = dep; }
void printtea()
{
printper();//不能用void的形式
cout << "学位:" << degree << endl;
cout << "部门:" << department << endl;
}
private:
string degree;
string department;
};
class student :public person
{
public:
student(string n, string i, int old1, string sno1) :person(n, i) { old = old1, sno = sno1; }
void printstu()
{
printper();
cout << "学生年龄:" << old << endl;
cout << "学生学号:" << sno << endl;
}
protected:
int old;
string sno;
};
class stud
{
public:
stud(string addr, string tel) { address = addr, tellphone = tel; }
void printstud()
{
cout << "学生地址:" << address << endl;
cout << "学生电话号码:" << tellphone << endl;
}
protected:
string address;
string tellphone;
};
class score :public stud, public student
{
public:
score(string name,string id,int old,string sno,string address,string tellphone,float ma, float eng) : student( name,id,old,sno),stud(address,tellphone){ math = ma, english = eng; }
void printsco()
{
printstu();
printstud();
cout << "数学成绩:" << math << endl;
cout << "英语成绩:" << english << endl;
}
private:
float math;
float english;
};
int main()
{
score c1( "张三","100100100101010011",19, "2019010101","北京海淀区","1234567890123", 100.0, 100.0);
c1.printsco();
cout << endl;
teacher t1("李四", "100100002222222222", "博士生导师", "法学院");
t1.printtea();
}