继承函数

来源:互联网 发布:淘宝买演唱会门票 编辑:程序博客网 时间:2024/06/05 17:16
 #include <iostream.h>
class A
{
private:
 int x1;
public:
 A()
 {
  x1=0;
  cout<<"A Constructor1"<<endl;
 }
 A(int i)
 {
  x1=i;
  cout<<"A Constructor2"<<endl;
 }
 void disp()
 {
     cout<<"x1="<<x1<<endl;
 }
};
class B
{
 int x2;
public:
 B()
 {
     x2=0;
  cout<<"B Constructor1"<<endl;
 }
 B(int i)
 {
  x2=i;
  cout<<"B Constructor2"<<endl;
 }
 void disp()
 {
  cout<<"x2="<<x2<<endl;
 }
};
class C:public A
{
private:
 int x3;
 B b1;
public:
 C()
 {
  x3=0;
  cout<<"C Constructor1"<<endl;
 }
 C(int x,int y,int z):A(x),b1(y)
 {
  x3=z;
  cout<<"C Constructor2"<<endl;
 }
 void disp()
 {
  A::disp();
  b1.disp();
  cout<<"X3="<<x3<<endl;
 }
};
void main()
{
 C c1;
 c1.disp();
 C c2(1,2,3);
 c2.disp();
}