#include<iostream>#include<cstring>#include<iomanip>#include<cstdlib>usingnamespace std;classTime{public:Time(){cout<<"Time is constructor."<<endl;}~Time(){cout<<"Time is constructor."<<endl;}};classDate{public:Date(int y,int m,int d){
year = y;
month = m;
day = d;
cout<<"Date's constructor."<<endl;}~Date(){
cout<<"Date's de-constructor."<<endl;}private:int year,month,day;};classPerson{public:Person(int _age){
age = _age;
cout<<"Person's constructor."<<endl;}~Person(){
cout<<"Person's de-constructor."<<endl;}protected://只放开给子类,不给组合类开放
int age;
Time time;};classStudent:public Person,public Time{public:Student(int _age,string _id,int y,int m,int d):birthday(y,m,d),Person(_age){//构造函数调用顺序:先父再组合再子
id = _id;
cout<<"Student's constructor."<<endl;}~Student(){
cout<<"Student's de-constructor."<<endl;}voidshow(){
cout<<age<<' '<<id<<endl;}private:
string id;
Date birthday;// Time time;};intmain(){
Student a(18,"1060305",2002,5,22);
a.show();return0;}
#include<iostream>#include<cstring>#include<iomanip>#include<cstdlib>usingnamespace std;classDate{public:Date(int y =0,int m =0,int d =0){
year = y;
month = m;
day = d;
cout<<"Date is constructor."<<endl;}~Date(){
cout<<"Date is de-constructor."<<endl;}private:int year,month,day;};classVehicle{public:Vehicle(int _speed =0){
speed = _speed;
cout<<"Vehicle is default."<<endl;}Vehicle(const Vehicle &another){
speed = another.speed;
cout<<"Vehicle is copyed."<<endl;}~Vehicle(){
cout<<"Vehicle is de-constructor."<<endl;}voidshow(){
cout<<speed<<endl;}protected:int speed;staticint cnt;};classCar:public Vehicle{public:Car(int speed =0,int y =0,int m =0,int d =0,string b =" "):Vehicle(speed),produceDate(y,m,d){
brand = b;
cout<<"Car is constructor."<<endl;}Car(const Car &another):Vehicle(another){//拷贝构造函数
brand = another.brand;
cout<<"Car is copied."<<endl;}~Car(){
cout<<"Car is de-constructor."<<endl;}// using Vehicle::speed;private:
string brand;// using Vehicle::speed;
Date produceDate;protected:};//class My_Car:public Car{// public:// My_Car(){// cout<<"My_Car is constructor."<<endl;// }// void show()// {// cout<<speed<<endl;// }//};int Vehicle::cnt =0;intmain(){
Car x(80,2002,5,22,"baoma");
cout<<endl;
Car y(x);
y.Vehicle::show();//通过子类调用父类的方法 // My_Car a;// a.speed = 1;// cout<<a.speed<<endl;return0;}