对象成员以及基类的初始化须在成员初始化列表中给出,通过调用对象成员的方法和基类的构造函数实现。
- 定义机动车Vehicle类,数据成员有出厂日期(先封装日期类,定义日期类型的对象作为Vehicle类的对象成员)和售价,成员函数有Show函数用于显式Vehicle类对象数据,编写并封装该类;
- 定义Vehicle类的派生类Car类和Truck类,Car类增加乘客数量,Truck类增加载重吨数,编写并封装这两个子类;
- 编写main函数,显式父类和子类对象信息
- 运行程序观察基类、内嵌对象成员、派生类的构造函数、复制构造函数和析构函数被调用的情况。
-
#include<iostream> using namespace std; class data{ public: int year,month,day; data(int a=0,int b=0,int c=0){year=a;month=b;day=c;cout<<"data构造"<<endl;} data(const data&a){year = a.year;month = a.month;day = a.day;cout<<"data复制构造"<<endl;} void data_set(int a,int b,int c){year=a;month=b;day=c;} ~data(){cout<<"data析构"<<endl;} }; class Vehicle{ data time; int shoujia; public: Vehicle(int a,int b,int c,int s):time(a,b,c)//调用data的构造函数,实现对data的对象time的初始化 { shoujia = s;//对其他数据成员进行初始化 cout<<"Vehicle构造"<<endl; } ~Vehicle(){ cout<<"Vehicle析构"<<endl; } Vehicle(const Vehicle&t):time(t.time.year,t.time.month,t.time.day)//复制构造时首先在成员初始化列表中给出其对象成员的初始化 { shoujia = t.shoujia; cout<<"Vehicle复制构造"<<endl; } void V_show(){ cout<<"生产日期:"<<time.year<<'-'<<time.month<<'-'<<time.day<<endl; cout<<"售价:"<<shoujia<<endl; } void V_set(int a,int b,int c,int s) { time.data_set(a,b,c); shoujia = s; } }; class Car:public Vehicle{ int shuliang; public: Car(int a,int b,int c,int shou,int shu):Vehicle(a,b,c,shou)//在成员初始化列表中通过调用基类的构造函数完成对基类的构造 { shuliang = shu;//初始化派生类新增的数据成员 cout<<"Car构造"<<endl; } ~Car(){ cout<<"Car析构"<<endl; } Car(const Car&c):Vehicle(c) { shuliang = c.shuliang; cout<<"Car复制构造"<<endl; } void C_set(int a,int b,int c,int shou,int shu) { V_set(a,b,c,shou); shuliang = shu; } void C_show(){ V_show(); cout<<"数量:"<<shuliang<<endl; } }; class Truck:public Vehicle{ int dunshu; public: Truck(int a,int b,int c,int s,int d):Vehicle(a,b,c,s) { dunshu = d; cout<<"Truck构造:"<<endl; } ~Truck(){ cout<<"Truck析构"<<endl; } Truck(const Truck&t):Vehicle(t) { dunshu = t.dunshu; cout<<"Truck复制构造"<<endl; } void T_set(int a,int b,int c,int s,int d) { V_set(a,b,c,s); dunshu = d; } void T_show() { V_show(); cout<<"吨数:"<<dunshu<<endl; } }; int main() { Vehicle v(2022,10,1,10000);//顺序调用 data,Vehicle构造函数 , //其中data为Vehicle的对象成员,即data的一个对象为Vehicle的数据成员 //输出结果 data构造 //Vehicle构造 cout<<endl; Car c(2022,10,2,9000,4); //顺序调用data,Vehicle,Car的构造函数; //其中Vehicle是Car的基类,在派生类构造的时候先构造基类 //结果 data构造 // Vehicle构造 // Car构造 cout<<endl; Truck t(2022,10,3,20000,50);//构造函数调用顺序同上 cout<<endl; cout<<endl; //分别显示各个对象的数据成员 v.V_show(); cout<<endl; c.C_show(); cout<<endl; t.T_show(); cout<<endl; cout<<endl; //分别调用复制构造函数,查看其复制构造函数的调用顺序,以及默认构造的顺序 Vehicle v2(v);//在调用Vehicle的构造函数的时候,首先要在成员初始化列表中显式调用data的构造函数 //完成对对象成员的复制构造,其次完成其他数据成员的复制构造 //结果 data构造 // Vehicle复制构造 cout<<endl; Car c2(c); //调用Car的复制构造函数,首先要对其基类Vehicle完成复制构造 // 采用在成员初始化列表中显式调用基类的复制构造函数实现 //结果 data构造 // Vehicle复制构造 // Car复制构造 cout<<endl; Truck t2(t);//构造方式同上 cout<<endl; cout<<endl; }
-
输出结果为:
data构造
Vehicle构造data构造
Vehicle构造
Car构造data构造
Vehicle构造
Truck构造:
生产日期:2022-10-1
售价:10000生产日期:2022-10-2
售价:9000
数量:4生产日期:2022-10-3
售价:20000
吨数:50
data构造
Vehicle复制构造data构造
Vehicle复制构造
Car复制构造data构造
Vehicle复制构造
Truck复制构造
Truck析构
Vehicle析构
data析构
Car析构
Vehicle析构
data析构
Vehicle析构
data析构
Truck析构
Vehicle析构
data析构
Car析构
Vehicle析构
data析构
Vehicle析构
data析构
-