题目:
定义一个抽象类Structure(含有纯虚函数type函数,用以显示当前结构的类型; 含有show函数),
在此基础上派生出Building类, 用来存储一座楼房的层数、房间数以及它的总平方米数。 建立派生
类House,继承Building类,并存储下面的内容:卧室与浴室的数量。另外,建立派生类Office,继承
Building类,并存储灭火器与电话的个数。要求定义各个类的数据成员,成员函数(包括构造函数、
析构函数、显示各数据成员的show()函数,type()函数)。
(1)在完成上述类的基础上,在main函数中定义相应的对象,并测试所有的函数功能。
(2)在main()中,用Office类定义一个对象,请分析此时调用构造函数的顺序和析构函数的顺序。
(3)实现Structure类族中show()函数的动态多态性,要求要在每个层级都实现,并调用测试。
(4)在Office类中重载运算符“<<”,以便通过它来直接对Office类对象内容直接输出。
(5)调用“<<”运算符将Office对象的信息输出到文本文件“d:\\office.txt”中。
程序:
#include<iostream>
#include<fstream>//(5)文件读写操作需要用到的头文件
using namespace std;
class Structure {//抽象类
public:
virtual void type() = 0;
virtual void show() = 0;//(3)纯虚函数,Structure类族动态多态性
};
class Building :public Structure {
protected:
int floor;//记录层数
int room;//记录房间数
int square;//记录平方米数
public:
Building(int f = 1, int r = 0, int s = 0) {
this->floor = f;
this->room = r;
this->square = s;
cout << "the constructor function of Building has been called" << endl;//验证(2)构造函数调用顺序
return;
}
~Building() {
cout << "the destructor function of Building has been called" << endl;//验证(2)析构函数调用顺序
}
public:
virtual void type() {
cout << "The type is Building" << endl;
return;
}
virtual void show() {//(3)虚函数,Structure类族动态多态性
cout << "floor:" << floor << endl;
cout << "room:" << room << endl;
cout << "square:" << square << "m^2" << endl;
return;
}
};
class House :public Building {
protected:
int bedroom;//记录卧室数
int bathroom;//记录浴室数量
public:
House(int floor = 1, int room = 0, int square = 0, int bedroom = 0, int bathroom = 0)
:Building(floor, room, square) {//重载构造函数(参数表法)
this->bedroom = bedroom;
this->bathroom = bathroom;
return;
}
~House() { }
public:
void type() {
cout << "The type is House" << endl;
}
void show() {
cout << "floor:" << floor << endl;
cout << "room:" << room << endl;
cout << "bedroom:" << bedroom << endl;
cout << "bathroom" << bathroom << endl;
cout << "square:" << square << "m^2" << endl;
return;
}
};
class Office :public Building {
protected:
int hydrant;//记录灭火器数
int telephone;//记录电话数
public:
Office(int floor = 1, int room = 0, int square = 0, int hydrant = 0, int telephone = 0)
:Building(floor, room, square) {//重载构造函数(参数表法)
this->telephone = telephone;
this->hydrant = hydrant;
cout << "the constructor function of Office has been called" << endl;//验证(2)构造函数调用顺序
return;
}
~Office() {
cout << "The destructor function of Office has been called" << endl;//验证(2)析构函数调用顺序
};
public:
friend void operator<<(ostream&, Office&);//(4)运算符重载函数声明,流插入运算符只能重载为友元函数
public:
void type() {
cout << "The type is Office" << endl;
}
void show() {
cout << "floor:" << floor << endl;
cout << "room:" << room << endl;
cout << "hydrant:" << hydrant << endl;
cout << "telephone" << telephone << endl;
cout << "square:" << square << "m^2" << endl;
return;
}
public:
void document() {//(5)文件读写操作
ofstream outfile("d:\\office.txt", ios::out);
if (!outfile) {
cerr << "OPEN ERROR!" << endl;
exit(1);
}
outfile << "floor:" << floor << endl;
outfile << "room:" << room << endl;
outfile << "hydrant:" << hydrant << endl;
outfile << "telephone" << telephone << endl;
outfile << "square:" << square << "m^2" << endl;
return;
}
};
void operator <<(ostream& output, Office& of) {//(4)重载运算符,用于直接输出office类
cout << "floor:" << of.floor << endl;
cout << "room:" << of.room << endl;
cout << "hydrant:" << of.hydrant << endl;
cout << "telephone" << of.telephone << endl;
cout << "square:" << of.square << "m^2" << endl;
return;
}
int main() {
Building bu(2, 3, 100);
House ho(2, 4, 100, 3, 2);
Office of(3, 10, 300, 5, 4);
//(1)测试部分程序
bu.show();
bu.type();
cout << endl;
ho.show();
ho.type();
cout << endl;
of.show();
of.type();
cout << endl;
//(4)测试部分程序
cout << of;//直接输出office类成员
cout << endl;
//(5)测试部分程序
of.document();
return 0;
}