为什么这样输出?求输出的每句原因!
#include "stdio.h"#include "iostream.h"
class A
{
public:
A()
{
a=0;
cout<<"调用A类的无参构造函数A()"<<endl;
}
A(int i)
{
a=i;
cout<<"调用A类的带参构造函数A(int)"<<endl;
}
~A()
{
cout<<"调用A类的析构函数"<<endl;
}
void Print() const
{
cout<<a<<",";
}
int Geta()
{
return a;
}
private:
int a;
};
//用类B继承类A
class B:public A
{
public:
B()
{
b=0;
cout<<"调用B类的无参构造函数B()"<<endl;
}
B(int i,int j,int k);
~B()
{
cout<<"调用B类的析构函数"<<endl;
}
void Print();
private:
int b;
A oa; //父类对象为子类成员变量.
};
B::B(int i,int j,int k)
{
b=k;
cout<<"调用B类的带参构造函数B(int,int,int)"<<endl;
}
void B::Print()
{
A::Print();
cout<<b<<","<<oa.Geta()<<endl;
}
void main()
{
B ob[3];
ob[0]=B(1,2,3);
ob[1]=B(4,5,6);
ob[2]=B();
for(int i=0;i<3;i++)
ob[i].Print();
getchar();
}