1、简单的派生类构造函数
声明形式:派生类构造函数名(总参数列表):基类构造函数名(参数列表){派生类中新增加成员初始化语句}
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int n=0,string nam="null",char s='F'):num(n),name(nam),sex(s){}
~student(){}
void show();
protected:
int num;
string name;
char sex;
};
void student::show()
{
cout<<num<<" "<<name<<" "<<sex<<endl;
}
class student1:public student
{
public:
student1(int n,string nam,char s,int a,string ad):student(n,nam,s),age(a),add(ad){}
~student1(){}
void show1();
private:
int age;
string add;
};
void student1::show1()
{ cout<<"==========================================="<<endl;
cout<<" num:"<<num<<endl;
cout<<" name:"<<name<<endl;
cout<<" sex:"<<sex<<endl;
cout<<" age:"<<age<<endl;
cout<<"address:"<<add<<endl;
}
int main()
{
student1 s(10001,"zhangtong",'M',25,"Wuxi NewDistrict ZhongShanRoad 8#");
s.show();
s.show1();
return 0;
}
2、含子对象的派生类构造函数
声明形式:派生类构造函数名(总参数列表):基类构造函数名(参数列表),子对象名(参数列表){派生类中新增加成员初始化语名句}
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int n=0,string nam="null",char s='F'):num(n),name(nam),sex(s){}
~student(){}
void show();
protected:
int num;
string name;
char sex;
};
void student::show()
{
cout<<" num:"<<num<<endl;
cout<<" name:"<<name<<endl;
cout<<" sex:"<<sex<<endl;
}
class student1:public student
{
public:
student1(int n,string nam,char s,int n1,string nam1,char s1,int a,string ad):student(n,nam,s),monitor(n1,nam1,s1),age(a),add(ad){}
~student1(){}
void show1();
private:
student monitor;
int age;
string add;
};
void student1::show1()
{ cout<<"This Student is:"<<endl;
show();
cout<<" age:"<<age<<endl;
cout<<"address:"<<add<<endl<<endl;
cout<<"The Monitor is:"<<endl;
monitor.show();
}
int main()
{
student1 s(10001,"zhangtong",'M',10008,"limeng",'F',25,"Wuxi NewDistrict ZhongShanRoad 8#");
s.show1();
return 0;
}
运行结果:
This Student is:
num:10001
name:zhangtong
sex:M
age:25
address:Wuxi NewDistrict ZhongShanRoad 8#
The Monitor is:
num:10008
name:limeng
sex:F
Press any key to continue...
3、多层派生时的构造函数
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int n=0,string nam="null"):num(n),name(nam){}
~student(){}
void show();
protected:
int num;
string name;
};
void student::show()
{
cout<<"num : "<<num<<endl;
cout<<"name: "<<name<<endl;
}
class student1:public student
{
public:
student1(int n,string nam,int a):student(n,nam),age(a){}
~student1(){}
void show1();
protected:
int age;
};
void student1::show1()
{ cout<<"This Student is:"<<endl;
show();
cout<<"age : "<<age<<endl;
}
class student2:public student1
{
public:
student2(int n,string nam,int a,float s):student1(n,nam,a),score(s){}
void show2();
private:
float score;
};
void student2::show2()
{
show1();
cout<<"score: "<<score<<endl;
}
int main()
{
student2 s2(10001,"zhangtong",25,98.52);
s2.show2();
return 0;
}
说明:如果在基类或子对象中定义了带参数的构造函数,则在派生类中必须显式地定义派生类构造函数,并写出基类或子对象
的构造函数及参数表。
习题:
有以下程序,请写出运行结果:(注意构造函数的用法!)
#include<iostream>
using namespace std;
class A
{
public:
A(){a=0;b=0;}
A(int i){a=i;b=0;}
A(int i,int j){a=i;b=j;}
void display(){cout<<"a="<<a<<" "<<"b="<<b<<" ";}
private:
int a,b;
};
class B:public A
{
public:
B(){c=0;}
B(int i):A(i){c=0;}
B(int i,int j):A(i,j){c=0;}
B(int i,int j,int k):A(i,j),c(k){}
void display1()
{
display();
cout<<"c="<<c<<endl;
}
private:
int c;
};
int main()
{
B b1;
B b2(1);
B b3(1,3);
B b4(1,3,5);
b1.display1();
b2.display1();
b3.display1();
b4.display1();
return 0;
}
运行结果:
a=0 b=0 c=0
a=1 b=0 c=0
a=1 b=3 c=0
a=1 b=3 c=5
Press any key to continue...