c++问题求助啊~~高手进

来源:百度知道 编辑:UC知道 时间:2024/06/20 10:03:42
#include <iostream>
using namespace std ;
class B
{
public :
B();
B(int i);
~B();
void print () const ;
private :
int b ;
};
B::B()
{
b=0;
cout<<"1"<<endl;
}
B::B(int i)
{
b=i;
cout<<"2"<<endl;
}
B::~B()
{
cout<<"3"<<endl;
}
void B::print()const
{
cout<<b<<endl;
}
class C:public B
{
public:
C();//自己定义构造函数
C(int i,int j);//带参数函数
~C();//析构函数
void print () const;//覆盖父类函数
private:
int c;
};

C::C()
{
c=0;
cout<<"4"<<endl;
}
C::C(int i,int j):B(i)
{
c=j;
cout<<"5"<<endl;
}
C::~C()
{

代码没有什么意思,主要是让你看输出数据,通过观察派生类和基类的构造函数和析构函数的调用顺序,理解继承关系中派生类对象实例的创建过程和销毁过程

我很想帮助你,可是我也是初学者,不好意思,一起学习哈!