继承,派生类的构造(C++)

以前面第 18 题中 CBook 类为基础定义一个派生类 CComputerBook,该派生类有如下属性成员和成员函数:
(1) 属性成员:
a) 领域属性(string 类型):如人工智能领域、嵌入式领域、机器学习领域等
b) 类别属性(string 类型):如教材、实验指导书、练习册等
(2) 成员函数:
a) 无参构造函数。其中 name="无"; author="无"; price=0.0; publisher="无"; 领域属性为:无;类别为:无
b) 带参数的构造函数
c) 析构函数,析构函数中要求输出:“CComputerBook 类对象被析构了!”
d) 流输出符重载函数,显示时要求每个属性信息占一行
(3) 提供类的完整测试程序,要求界面友好,输出结果应该有相应的提示信息。
(4) 在测试程序中 new 一个 CComputerBook 对象,并设置其属性如下:
a) 书名为:“C 程序设计教程”
b) 作者:“王涛”
c) 价格:35.0
d) 出版社:“清华大学出版社”
e) 领域:“程序设计”
f) 类别:“教材”
(5) 在测试层序中 delete 第(4)步创建的对象,体会析构函数的运行过程。
 

/*=======================================================
*学号:
*作业:E22
*功能:(1) 属性成员:
           a) 领域属性(string 类型):如人工智能领域、嵌入式领域、机器学习领域等
           b) 类别属性(string 类型):如教材、实验指导书、练习册等
       (2) 成员函数:
           a) 无参构造函数。其中 name="无"; author="无"; price=0.0; publisher="无"; 领域
              属性为:无;类别为:无
           b) 带参数的构造函数
           c) 析构函数,析构函数中要求输出:“CComputerBook 类对象被析构了!”
           d) 流输出符重载函数,显示时要求每个属性信息占一行
       (3) 提供类的完整测试程序,要求界面友好,输出结果应该有相应的提示信息。
       (4) 在测试程序中 new 一个 CComputerBook 对象,并设置其属性如下:
           a) 书名为:“C 程序设计教程”
           b) 作者:“王涛”
           c) 价格:35.0
           d) 出版社:“清华大学出版社”
           e) 领域:“程序设计”
           f) 类别:“教材”
        (5) 在测试层序中 delete 第(4)步创建的对象,体会析构函数的运行过程。
*作者:
*日期:2016.6.4
*========================================================*/

#include<iostream>
#include<string>

using namespace std;

class CBook
{
private:
	string name;
	string author;
	double price;
	string publisher;
public:
	CBook(){name="无";author="无";price=0.0;publisher="无";}
	CBook(const CBook&);                                             // 拷贝构造函数
	CBook(string,string,double,string);                              // 带参数的构造函数
	CBook(char*,char*,double,char*);                                 // 带参数的构造函数
	~CBook();                                                        //析构函数
	void SetName(char*);                                             // 设置书名的成员函数
	void SetName(string&);                                           // 设置书名的成员函数
	void print()const;                                               // 在屏幕上显示书的信息的成员函数
	friend ostream & operator<<(ostream& out,const CBook&);          //输出操作符重载函数
};

CBook::CBook(const CBook& t)                             //拷贝构造函数
{
	name=t.name;
	author=t.author;
	price=t.price;
	publisher=t.publisher;
}

CBook::CBook(string a,string b,double c,string d)        //带参数的构造函数
{
	name=a;
	author=b;
	price=c;
	publisher=d;
}

CBook::CBook(char* a,char* b,double c,char* d)              //带参数的构造函数
{
	name=a;
	author=b;
	price=c;
	publisher=d;
}

CBook::~CBook()                                                //析构函数
{
	cout<<"Destructing"<<name<<endl;
}

void CBook::SetName(char* a)                               //设置书名的成员函数
{
	name=a;
}

void CBook::SetName(string& a)                             //设置书名的成员函数
{
	name=a;
}

void CBook::print()const                                        //在屏幕上显示书的信息的成员函数
{ 
	cout<<"书名:"<<name<<endl
		<<"作者:"<<author<<endl
		<<"价格:"<<price<<endl
		<<"出版社:"<<publisher<<endl;
}

ostream& operator<<(ostream& out,const CBook& t)                //输出操作符重载函数
{
	out<<"书名:"<<t.name<<endl
		<<"作者:"<<t.author<<endl
		<<"价格:"<<t.price<<endl
		<<"出版社:"<<t.publisher<<endl;
	return out;
}

class CComputerBook:public CBook
{
private:
	string field;                      //领域属性
	string sort;                       //类别属性
public:
	CComputerBook()                    //无参构造函数
	{
		CBook();
		field="无";
		sort="无";
	}
	CComputerBook(string& a,string& b,double c,string d,string e,string f):CBook(a,b,c,d),field(e),sort(f){}    //带参数的构造函数
	~CComputerBook()                                    //析构函数
	{
		cout<<"CComputerBook类对象被析构了!"<<endl;
	}
	friend ostream & operator<<(ostream& out,const CComputerBook&);     //流输出符重载函数
};

ostream& operator<<(ostream& out,const CComputerBook& t)
{
	t.print();
	out<<"领域:"<<t.field<<endl
		<<"类别:"<<t.sort<<endl;
	return out;
}

int main()
{
	CComputerBook c1;
	cout<<c1;

	cout<<"\n";
	CComputerBook c2((string)"C 程序设计教程",(string)"王涛",35.0,(string)"清华大学出版社",(string)"程序设计",(string)"教材");
	cout<<c2;

	cout<<"\n";
	CComputerBook *c3=new CComputerBook((string)"C 程序设计教程",(string)"王涛",35.0,(string)"清华大学出版社",(string)"程序设计",(string)"教材");
	cout<<*c3;
	delete c3;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ukco_well/article/details/86577023