基类是派生类的抽象,派生类是基类的实现。
单继承(single inheritance):派生类从一个基类继承
多继承(multiple inheritance):派生类从多个基类继承
派生类的申明形式:
class 派生类名:继承方式 基类名
{
新增的成员
};
基类名前面有public的称为“公用继承(public inheritance)”。
private(私有的),protected(受保护的),继承方式如果不写默认为private。
不同的继承方式决定了基类成员在派生类中的访问属性。
公用继承:基类的公用成员和保护成员在派生类中保持原有访问属性,其私有成员仍为基类私有。
class Studet
{
public:
void get_value(){ cin >> num >> name >> sex; }
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
protected:
private:
int num;
int name;
char sex;
};
class Graduate :public Studet
{
public:
void displayGratuate()
{
/*企图引用基类的私有成员,错误*/
//cout << "num:" << num << endl;
//cout << "name:" << name << endl;
//cout << "sex:" << sex << endl;
cout << "age:" << age << endl;
cout << "address:" << addr << endl;
}
private:
int age;
string addr;
};
int main()
{
Graduate G1;
G1.display();//通过调用基类的公有函数来引用基类的私有成员
G1.displayGratuate();
return 0;
}
私有继承(隔代不继承):基类的公用成员和保护成员在派生类中成了私有成员。其私有成员仍为基类私有。这句话的意思是,公用成员,不能再派生类外被调用了,如果要想调用只有把成员的引用写入派生类的公用函数才能从外部间接调用。
受保护的继承:基类的公用成员和保护成员在派生类中成了保护成员,私有成员仍为基类私有
保护成员可以被派生类的成员函数引用,但无法从派生类外访问。如果基类声明了私有成员,那么任何派生类都是不能访问它们的,若希望在派生类中访问他们,应该把它们声明为保护成员。
派生类的构造函数
派生类构造函数名(总参数列表):基类构造函数名(参数列表)
{
派生类中新增数据成员初始化语句
}
class Student
{
public:
Student(int n, string nam, char s)
{
num = n;
name = nam;
sex = s;
}
~Student(){}
protected://**基类声明的保护成员可以被派生类的成员函数引用**
int num;
string name;
char sex;
};
class Graduate :public Student
{
public:
Graduate(int n, string nam, char s, int a, string ad) :Student(n, nam, s)//**如果只是再类中声明,Graduate(int n, string nam, char s, int a, string ad) 就行了,如果是定义则需要把后面的部分加上,**
{
age = a;
addr = ad;
}
//还可以写成这样
Graduate(int n, string nam, char s, int a, string ad)
:Student(n, nam, s)
,age(a)
,addr(ad)
{}
void show()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
cout << "age:" << age << endl;
cout << "address:" << addr << endl << endl;
}
private:
int age;
string addr;
};
int main()
{
Graduate G1(10010, "wangli", 'f', 19, "115 Beijing Road,Shanghai");
Graduate G2(10011, "zhnagfun", 'm', 21, "213 Shanghai Road,Beijing");
G1.show();
G2.show();
return 0;
}
有子对象的派生类的构造函数
类的数据成员中可以包含类对象
class Student
{
public:
Student(int n, string nam)
{
num = n;
name = nam;
}
void display()
{
cout << "num:" << num << endl << "name:" << name << endl;
}
protected:
int num;
string name;
};
class Graduate :public Student
{
public:
Graduate(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();
cout << "age:" << age << endl;
cout << "address:" << addr << endl;
}
void show_monitor()
{
cout << endl << "class monitor is:" << endl;
monitor.display();
}
private:
Student monitor;
int age;
string addr;
};
int main()
{
Graduate G1(10010, "Wang-Li", 10001, "Li-sun", 19, "115 Beijing Road ,shanghai");
G1.show();
G1.show_monitor();
return 0;
}
派生类构造函数的任务
1.对基类数据成员初始化
2.对子对象数据成员初始化
3.对派生类数据成员初始化
归纳起来,定义派生类函数的一般形式为
派生类构造函数名(总参数列表)
:基类构造函数名(参数列表)
,子类对象名(参数列表)
{
新增成员初始化
}
多重继承
在声明了类A、类B、类C的前提下,声明多重继承的派生类D
class D:public A,privateB,protected C
{
类D新增加的成员
}