单继承的构造函数2


#include<iostream>
using namespace std;
class A
{ private: int x;
public:  A(){ cout<<"A Constructor 1..."<<endl;}
A(int i) { x=i; cout<<"A Constructor 2..."<<endl; }
void disp_x(){  cout<<"x= "<<x<<endl; }


};


class B:public A
{
private: int y;
public: B() { cout<<"B Constructor 1..."<<endl;}
B(int j):A(j+10) //使用 基类的构造函数来初始化
{  y=j; cout<<"B Constructor 2..."<<endl;   }

void disp_y()   { disp_x(); cout<<"y= "<<y<<endl; } 
};


int main()
{
B b(2);
b.disp_y();
}



猜你喜欢

转载自blog.csdn.net/u010608296/article/details/81004345