9_80.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:2k
源码类别:

文章/文档

开发平台:

C/C++

  1. #include<iostream.h>
  2. #include<string.h>
  3. class father
  4. {
  5. protected:
  6. char* fname;
  7. char* sname;
  8. int age;
  9. public:
  10. father()
  11. {cout<<"father默认构造函数调用!"<<endl;fname=NULL;sname=NULL;}
  12. father(char *fn,char *sn,int a)
  13. {
  14. cout<<"father构造函数调用!"<<endl;
  15. fname=new char[strlen(fn)+1];
  16. strcpy(fname,fn);
  17. sname=new char[strlen(sn)+1];
  18. strcpy(sname,sn);
  19. age=a;
  20. }
  21. ~father()
  22. {
  23. cout<<"father析构函数调用!"<<endl;
  24. delete fname;
  25. delete sname;
  26. }
  27. char *getfname(){return fname;}
  28. void show(){cout<<fname<<sname<<"  年龄:"<<age;}
  29. };
  30. class mother
  31. {
  32. protected:
  33. char* fname;
  34. char* sname;
  35. int age;
  36. public:
  37. mother()
  38. {cout<<"mother默认构造函数调用!"<<endl;fname=NULL;sname=NULL;}
  39. mother(char *fn,char *sn,int a)
  40. {
  41. cout<<"mother构造函数调用!"<<endl;
  42. fname=new char[strlen(fn)+1];
  43. strcpy(fname,fn);
  44.         sname=new char[strlen(sn)+1];
  45. strcpy(sname,sn);
  46. age=a;
  47. }
  48. ~mother()
  49. {
  50. cout<<"mother析构函数调用!"<<endl;
  51. delete fname;
  52. delete sname;
  53. }
  54. char *getfname(){return fname;}  //此句可省略
  55. void show(){cout<<fname<<sname<<"  年龄:"<<age;}
  56. };
  57. class child:public mother,public father
  58. {
  59. private:
  60. father* myfather;
  61. mother* mymother;
  62. public:
  63. child(){cout<<"child构造函数调用!"<<endl;}
  64. child(father& fa,mother &mo,char *na,int a)
  65. :myfather(&fa),mymother(&mo)
  66. {
  67. cout<<"child构造函数调用!"<<endl;
  68.       //不能省略mother::否则出现二义性
  69. mother::fname=new char[strlen(fa.getfname())+1];  
  70. //如子随母姓,则改为strcpy(mother::fname,mo.getfname());
  71. strcpy(mother::fname,fa.getfname());
  72.         mother::sname=new char[strlen(na)+1];
  73. strcpy(mother::sname,na);
  74. mother::age=a;  //不能写成age=a; 
  75. }
  76. ~child(){cout<<"child析构函数调用!"<<endl;}
  77. void show()
  78. {
  79. cout<<"  姓名:"; mother::show();
  80. cout<<endl;
  81. cout<<"父亲:"; myfather->show();
  82. cout<<endl;
  83. cout<<"母亲:"; mymother->show();
  84. cout<<endl;
  85. }
  86. };
  87. void main()
  88. {
  89. father fa1("欧阳","东海",50),fa2("张","伟大",40);
  90. mother mo1("李","超丽",47),mo2("许","英",35);
  91. child ch1(fa1,mo1,"智超",23),ch2(fa2,mo2,"宁",10);
  92. cout<<"输出结果:"<<endl;
  93. ch1.show();
  94. ch2.show();
  95. }