关于继承和派生代码
先上代码:#include <iostream.h>
class Base {
public:
Base(){ a=0;cout<<"Base缺省构造函数\n"; }
Base(int i){ a=i;cout<<"Base带参构造函数\n"; }
void print(){ cout<<"a="<<a<<","; }
int Geta(){ return a; }
private:
int a;
};
class Derived:public Base {
public:
Derived(){ b=0;cout<<"Derived缺省构造函数\n"; }
Derived (int i,int j,int k);
void print();
private:
int b;
Base Ba; //子对象或称为内嵌对象
};
Derived::Derived(int i,int j,int k):Base(i), Ba(j),b(k)
{ cout<<"Derived带参构造函数\n"; }
void Derived::print()
{ Base::print(); //调用基类Base的print()函数
cout<<"b="<<b<<",Ba.a="<<Ba.Geta()<<endl;
}
void main()
{ Derived d1,d2(5,3,7);
d1.print(); d2.print();
}
运行结果如何?