问题及代码:
#include <iostream>
using namespace std;
class A
{
public:
A(char *s){cout<<s<<endl;}
};
class B:public A
{
public:
B(char *s1,char *s2):A(s1){cout<<s2<<endl;}
};
class C:public A
{
public:
C(char*s1,char *s2):A(s1){cout<<s2<<endl;}
};
class D:public B,C
{
public:
D(char *s1,char *s2,char *s3,char *s4):
B(s1,s2),C(s3,s4){cout<<s4<<endl;}
};
int main()
{
D d("class A","class B","class C","class D");
return 0;
}
运行结果:
知识点总结:
这个程序的过程是对对象d执行构造函数,对B和C类执行初始化,B和C类分别对A进行初始化,输出ABCDD.
学习心得:
对类的构造函数,继承都要熟练掌握。