设为首页 加入收藏

TOP

二级C++类的继承与派生:派生类的构造
2014-11-19 07:37:18 】 浏览:6202
Tags:二级 继承 派生 构造

1、派生类的构造函数:


派生类的对象的数据结构是由基类中说明的数据成员和派生类中说明的数据成员共同构成。


构造函数不能被继承,因此派生类的构造函数必须通过调用基类的构造函数来初始化基类对象。所以在定义派生类的构造函数时除了对自己的数据成员进行初始化外,还必须负责调用基类构造函数使基类的数据成员得以初始化。如果派生类中还有子类对象,还应包含对子类 对象初始化的构造函数。


一般格式:


< 派生类名 > ( < 派生类构造函数总参数表 > ): < 基类构造函数 >(< 参数表 1>),< 子对象名 >(< 参数表 2>)


{


< 派生类汇总数据成员初始化 >


};


派生类构造函数的调用顺序:


基类的构造函数


子对象类的构造函数


派生类的构造函数


例如:分析下列程序的输出结果:


#include


class A


{


public:


A()


{a=0;cout<<”A's default constructor called.\n”;}


A(int I)


{a=I;cout<<”A's constructor called.\n”;}


~A()


{cout<<”A's destructor called.\n”;}


void Print()


{ cout<


int Geta(){return a;}


private:


int a;


};


class B:public A


{


public:


B()


{b=0;cout<<”B's default constructor called.\n”;}


B(int I,int j,int k);


~B()


{cout<<”B's destructor called.\n”;}


void Print();


Private:


int b;


A aa;


};


B::B(int I,int j,int k):A(i),aa(j)


{


b=k;


cout<<”B's constructor called.\n”;


}


void B::Print()


{


A::Print();


cout<


}


void main()


{


B bb[2];


bb[0]=B(1,2,5); // 注:给已建立的两个对象赋值,系统要建立两个


bb[1]=B(3,4,7); 匿名对象,通过构造函数对它初始化,并将


for (int I=0;I<2;I++) 其赋值给已定义的左值对象,再调用析构


bb[I].Print(); 函数将匿名对象删除。


}


执行该程序的输出结果:


A'default constructor called.


A'default constructor called.


B'default constructor called.


A'default constructor called.


A'default constructor called.


B'default constructor called.


A' constructor called. // 匿名对象的构造


A' constructor called.


B' constructor called.


A'destructor called. // 匿名对象的析构


A'destructor called.


B'destructor called.


A' constructor called. // 匿名对象的构造


A' constructor called.


B' constructor called.


A'destructor called. // 匿名对象的析构


A'destructor called.


B'destructor called.


1 , 5 , 2


3 , 7 , 4


B'destructor called.


A'destructor called.


A'destructor called.


B'destructor called.


A'destructor called.


A'destructor called.


2、析构函数:当对象被删除时,派生类的析构函数被执行。由于析构函数也不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其执行顺序与执行构造函数时的顺序相反。


例如:分析下列程序的输出结果:


#include


class M


{


public:


M(){m1=m2=0;}


M(int I,int j){m1=I;m2=j;}


Void Print(){cout<


~M(){cout<<”M's destructor called.\n”;}


private:


int m1,m2;


}


class N: public M


{


public:


N(){n=0;}


N(int I,int j,int k);


Void Print()


{


M::Print();


Cout<


~N(){cout<<”N's Destructor called.\n”;}


private:


int n;


};


N::N(int I,int j,int k):M(I,j),n(k)


{}


void main()


{


N n1(5,6,7),n2(-2,-3,-4);


n1.Print();


n2.Print();


}


运行结果: 5,6,7


-2,-3,-4


N's destructor called.


M's destructor called.


N's destructor called.


M's destructor called.


3、 派生类构造函数使用中应注意的问题:


派生类构造函数的定义中可以省略对其基类构造函数的调用,其条件是在基类中必须有缺省的构造函数,或者根本没有定义构造函数。


例如:分析下列程序的输出结果:


#include


class A


{


public:


A(){a=0;}


Void print() {cout<


private:


int a;


};


class B:public A


{


public:


B(){b1=b2=0;}


B(int I){b1=I;b2=0;}


B(int I,int j,int k):A(i),b1(j),b2(k){}


Void print()


{A::print();


cout<


}


private:


int b1,b2;


};


void main()


{


B d1;


B d2(5);


B d3(4,5,6);


d1.print();


d2.print();


d3.print();


}


结果: 0,0,0


0,5,0


4,5,6


当基类的构造函数使用一个或多个参数时,则派生类必须定义构造函数,提供将参数传递给基类构造函数的途径。在有的情况下,派生类构造函数的函数体可能为空,仅起到参数传递的作用。


Class B


{


public:


B(int I,int j){b1=I;b2=j;}


Privte:


Int b1,b2;


};


class D:public B


{


public:


D(int I,int j,int k,int l,int m);


Private:


Int d1;


B bb;


};


D::D(int I,int j,int k,int l,int m):B(I,j),bb(k,l)


{d1=m;}


分析:派生类的 构造函数有 5 个参数,其中前两个参数传递给基类 B 的构造函数,接着两个参数传递给子对象 bb 的类 B 的构造函数,最后一个参数是传递给派生类 D 的数据成员 d1 。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇二级C++类与对象:静态成员函数 下一篇二级C++类的继承与派生:继承的工..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目