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

时间:2022-09-07 21:29:53
#include<iostream>
using namespace std;
class B
{
 public:
     B();
     B(int i);
     void print()const;
 private:
    int b;
};
B::B()
{
    b=0;
    cout<<"B's dafault constructor called."<<endl;
}
B::B(int i)
{
    b=i;
    cout<<"B's dafault constructor called."<<endl;
}
void B::print()const
{
    cout<<b<<endl;
}
class C:public B
{
public:
    C();
    C(int i,int j);
    void print()const;
private:
    int c;
};
C::C()
{
        cout<<"C's dafault constructor called."<<endl;
}
C::C(int i,int j):B(i),c(j)
{
    cout<<"C's dafault constructor called."<<endl;
}
void C::print()const
{
    B::print();
    cout<<c<<endl;
}
int main()
{
    C obj(5,6);
    obj.print();
    return 0;
}
运行结果:
    派生类生成过程——单继承的构造函数和析构函数