#include <iostream>
using namespace std;
class A
{public:
A()
{
a=0;
cout<<"类A的默认构造函数"<<endl;
}
A(int i)
{
a=i;
cout<<"类A的构造函数"<<endl;
}
~A()
{
cout<<"类A的析构函数"<<endl;
}
void out() const
{
cout<<a<<",";
}
int geta()
{
return a;
}
private:
int a;
};
class B:public A
{
public:
B()
{
b=0;
cout<<"类B的默认构造函数"<<endl;
}
B(int i,int j,int k);
~B()
{
cout<<"类B的析构函数"<<endl;
}
void out();
private:
int b;
A a1;
};
B::B(int i,int j,int k):A(i),a1(j)
{
b=k;
cout<<"类B的函数"<<endl;
}
void B::out()
{
A::out();
cout<<b<<","<<a1.geta()<<endl;
}
int main()
{
B b1[2];
b1[0]=B(1,2,3);
return 0;
}
注:执行顺序始终是:先基类,再对象,再派生类