#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; }
运行结果: