派生类生成过程——单继承的构造函数和析构函数(2)

时间:2022-09-07 21:34:01
#include<iostream>
using namespace std;
class B
{
public:
    B(int i);
    ~B();
    void print()const;
private:
    int b;
};
B::B(int i)
{
    b=i;
    cout<<"B's constructor called."<<endl;
}
B::~B()
{
    cout<<"B's constructor called."<<endl;
}
void B::print()const
{
    cout<<b<<endl;
}
class C:public B
{
public:
    C(int i,int j);
    ~C();
    void print()const;
private:
    int c;
};
C::C(int i,int j):B(i),c(j)
{
    cout<<"C's constructor called."<<endl;
}
C::~C()
{
    cout<<"C's constructor called."<<endl;
}
void C::print()const
{
    B::print();
    cout<<c<<endl;
}
int main()
{
    C obj(5,6);
    obj.print();
    return 0;
}


 

 

 

运行结果:

 

派生类生成过程——单继承的构造函数和析构函数(2)