构造函数的主要作用是对数据成员的初始化。
Case1
class A
{
int x;
char m ;
// A ( ) { }
A (int x , char m ) {
this. x = x ;
this. m = m ;
}
}
这个构造函数实际上是一个空函数,不执行任何操作。如果需要对类中的数据成员初始化,应自己定义构造函数。
基类的构造函数是不能继承的,所以对继承过来的基类成员初始化的工作也要由派生类的构造函数来承担。
解决方法在执行派生类的构造函数时,调用基类的构造函数。
Case1
#include<iostream>
using namespace std;
//基类
class People{
protected:
char *name;
int age;
public:
People(char*, int);
};
People::People(char *name, int age): name(name), age(age){}
//派生类
class Student: public People{
private:
float score;
public:
Student(char*, int, float);
void display();
};
//调用了基类的构造函数
Student::Student(char *name, int age, float score): People(name, age){
this->score = score;
}
void Student::display(){
cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
}
int main(){
Student stu("小明", 16, 90.5);
stu.display();
return 0;
}
运行结果为:
小明的年龄是16,成绩是90.5
请注意代码第23行:
Student::Student(char *name, int age, float score): People(name, age)
派生类 Student 的构造函数的写法。冒号前面是派生类构造函数的头部,它的形参列表包括了初始化基类和派生类的成员变量所需的数据;冒号后面是对基类构造函数的调用。
可以将对基类构造函数的调用和参数初始化表放在一起:
case2:
Student::Student(char *name, int age, float score): People(name, age), score(score){}
基类构造函数和初始化表用逗号隔开。
★冒号后面是对基类构造函数的调用,而不是声明,所以括号里的参数是实参,它们不但可以是派生类构造函数总参数表中的参数,还可以是局部变量、常量等。
Case3:
Student::Student(char *name, int age, float score): People("李磊", 20)
基类构造函数调用规则
通过派生类创建对象时必须要调用基类的构造函数。定义派生类构造函数时最好指明基类构造函数;如果不指明,就调用基类的默认构造函数(不带参数的构造函数);如果没有默认构造函数,那么编译失败。
Case4:
#include<iostream>
using namespace std;
//基类
class People{
protected:
char *name;
int age;
public:
People();
People(char*, int);
};
People::People(){
this->name = "xxx";
this->age = 0;
}
People::People(char *name, int age): name(name), age(age){}
//派生类
class Student: public People{
private:
float score;
public:
Student();
Student(char*, int, float);
void display();
};
Student::Student(){
this->score = 0.0;
}
Student::Student(char *name, int age, float score): People(name, age){
this->score = score;
}
void Student::display(){
cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
}
int main(){
Student stu1;
stu1.display();
Student stu2("小明", 16, 90.5);
stu2.display();
return 0;
}
运行结果:
xxx的年龄是0,成绩是0
小明的年龄是16,成绩是90.5
创建对象 stu1 时,执行派生类的构造函数 Student::Student(),系统默认调用了不带参数的构造函数,也就是 People::People()。
创建对象 stu2 时,执行派生类的构造函数 Student::Student(char *name, int age, float score),它指明了基类的构造函数。
在第31行代码中,如果将 People(name, age) 去掉,也会调用默认构造函数,stu2.display() 的输出结果将变为:
xxx的年龄是0,成绩是90.5
如果将基类 People 中不带参数的构造函数删除,那么会发生编译错误,因为创建对象 stu1 时没有调用基类构造函数。
总结:如果基类有默认构造函数,那么在派生类构造函数中可以不指明,系统会默认调用;如果没有,那么必须要指明,否则系统不知道如何调用基类的构造函数。
构造函数的调用顺序
Case5:
#include<iostream>
using namespace std;
//基类
class People{
protected:
char *name;
int age;
public:
People();
People(char*, int);
};
People::People(): name("xxx"), age(0){
cout<<"PeoPle::People()"<<endl;
}
People::People(char *name, int age): name(name), age(age){
cout<<"PeoPle::People(char *, int)"<<endl;
}
//派生类
class Student: public People{
private:
float score;
public:
Student();
Student(char*, int, float);
};
Student::Student(): score(0.0){
cout<<"Student::Student()"<<endl;
}
Student::Student(char *name, int age, float score): People(name, age), score(score){
cout<<"Student::Student(char*, int, float)"<<endl;
}
int main(){
Student stu1;
cout<<"--------------------"<<endl;
Student stu2("小明", 16, 90.5);
return 0;
}
运行结果:
PeoPle::People()
Student::Student()
PeoPle::People(char *, int)
Student::Student(char*, int, float)
当创建派生类对象时,先调用基类构造函数,再调用派生类构造函数。如果继承关系有好几层的话,例如:
A --> B --> C
那么则创建C类对象时,构造函数的执行顺序为:
A类构造函数 --> B类构造函数 --> C类构造函数
构造函数的调用顺序是按照继承的层次自顶向下、从基类再到派生类的。
C++有子对象的派生类的构造函数
类的数据成员不但可以是标准型(如int、char)或系统提供的类型(如string),还可以包含类对象,如可以在声明一个类时包含这样的数据成员:
Student s1; //Student是已声明的类名,s1是Student类的对象
★s1就是类对象中的内嵌对象,称为子对象(subobject),即对象中的对象。
在对数据成员初始化时怎样对子对象初始化呢?
Case6: 包含子对象的派生类的构造函数。
#include <iostream>
#include <string>
using namespace std;
class Student//声明基类
{
public: //公用部分
Student(int n, string nam ) //基类构造函数,与例11.5相同
{
num=n;
name=nam;
}
void display( ) //成员函数,输出基类数据成员
{
cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
}
protected: //保护部分
int num;
string name;
};
class Student1: public Student //声明公用派生类Student1
{
public:
Student1(int n, string nam,int n1, string nam1,int a, string ad):Student(n,nam),monitor(n1,nam1) //派生类构造函数
{
age=a;
addr=ad;
}
void show( )
{
cout<<"This student is:"<<endl;
display(); //输出num和name
cout<<"age: "<<age<<endl; //输出age
cout<<"address: "<<addr<<endl<