标签:c++
先给结论:
class point1 { public: point1(); point1(int i); virtual ~point1(); private: int x; }; point1::point1() { x=1; cout<<"point1's default constructor called!\n"; } point1::point1(int i) { x=i; cout<<"point1's constructor called!\n"; } point1::~point1() { } class point2 : public point1 { public: point2(); point2(int i,int j,int k); virtual ~point2(); point1 p; }; point2::point2() { cout<<"point2's defaule constructor called\n"; } point2::point2(int i,int j,int k) { cout<<"point2's constructor called\n"; } point2::~point2() { } //主函数 void main() { point2 pp; point2 pp1(1,2,3); }
运行结果
point1's default constructor called!// point1 p;调用了基类默认构造函数 point1's default constructor called!// point2's defaule constructor called point1's default constructor called!// point1 p;调用了基类默认构造函数 point1's default constructor called! point2's constructor called
point2::point2(int i,int j,int k) { cout<<"point2's constructor called\n"; }
point2::point2(int i,int j,int k):p(j) { cout<<"point2's constructor called\n"; }
point1's default constructor called! point1's default constructor called! point2's defaule constructor called point1's default constructor called! point1's constructor called! point2's constructor called
标签:c++
原文地址:http://blog.csdn.net/u011409995/article/details/39316609