?? 9_80.cpp
字號:
#include<iostream.h>
#include<string.h>
class father
{
protected:
char* fname;
char* sname;
int age;
public:
father()
{cout<<"father默認構造函數調用!"<<endl;fname=NULL;sname=NULL;}
father(char *fn,char *sn,int a)
{
cout<<"father構造函數調用!"<<endl;
fname=new char[strlen(fn)+1];
strcpy(fname,fn);
sname=new char[strlen(sn)+1];
strcpy(sname,sn);
age=a;
}
~father()
{
cout<<"father析構函數調用!"<<endl;
delete fname;
delete sname;
}
char *getfname(){return fname;}
void show(){cout<<fname<<sname<<" 年齡:"<<age;}
};
class mother
{
protected:
char* fname;
char* sname;
int age;
public:
mother()
{cout<<"mother默認構造函數調用!"<<endl;fname=NULL;sname=NULL;}
mother(char *fn,char *sn,int a)
{
cout<<"mother構造函數調用!"<<endl;
fname=new char[strlen(fn)+1];
strcpy(fname,fn);
sname=new char[strlen(sn)+1];
strcpy(sname,sn);
age=a;
}
~mother()
{
cout<<"mother析構函數調用!"<<endl;
delete fname;
delete sname;
}
char *getfname(){return fname;} //此句可省略
void show(){cout<<fname<<sname<<" 年齡:"<<age;}
};
class child:public mother,public father
{
private:
father* myfather;
mother* mymother;
public:
child(){cout<<"child構造函數調用!"<<endl;}
child(father& fa,mother &mo,char *na,int a)
:myfather(&fa),mymother(&mo)
{
cout<<"child構造函數調用!"<<endl;
//不能省略mother::否則出現二義性
mother::fname=new char[strlen(fa.getfname())+1];
//如子隨母姓,則改為strcpy(mother::fname,mo.getfname());
strcpy(mother::fname,fa.getfname());
mother::sname=new char[strlen(na)+1];
strcpy(mother::sname,na);
mother::age=a; //不能寫成age=a;
}
~child(){cout<<"child析構函數調用!"<<endl;}
void show()
{
cout<<" 姓名:"; mother::show();
cout<<endl;
cout<<"父親:"; myfather->show();
cout<<endl;
cout<<"母親:"; mymother->show();
cout<<endl;
}
};
void main()
{
father fa1("歐陽","東海",50),fa2("張","偉大",40);
mother mo1("李","超麗",47),mo2("許","英",35);
child ch1(fa1,mo1,"智超",23),ch2(fa2,mo2,"寧",10);
cout<<"輸出結果:"<<endl;
ch1.show();
ch2.show();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -