继承(单继承,多继承,虚继承,权限问题)

一:继承分类

单继承
多继承
虚继承

二:继承方式

public
protected
private

三:继承语法

父类 基类
子类 派生类

class 子类名:继承方式 父类名
{

}

继承中权限问题:
权限限定词,只会加强父类中成员在子类中的权限体现

#include<iostream>
using namespace std;

class A
{
public:
	A(int Ap,int Bp,int Cp):Ap(Ap),Bp(Bp),Cp(Cp){}
	int Ap;
	void ppp(){}
protected:
	int Bp;
private:
	int Cp;
};
//A中成员在B中的体现
class B:public A			//public形式继承
{
public:
	int Ap;
	void ppp(){}
protected:
	int Bp;
private:
	int Cp;
};
//A中成员在C中的体现
class C:protected A			//protected形式继承
{
public:
protected:
	int Ap;
	int Bp;
	void ppp(){}
private:
	int Cp;
};
//A中成员在D中的体现
class D:private A			//private形式继承
{
public:
protected:
private:
	int Ap;
	int Bp;
	int Cp;
	void ppp(){}
};

int main()
{

}

三、继承中构造函数的写法

注意:
子类的构造函数一定会调用父类的构造函数且必须采用参数列表的方式初始化。
子类中有和父类中相同的函数,优先默认调用子类中的函数,也可以采用作用域符号去区分

#include<iostream>
#include<string>
using namespace std;


class Mom
{
public:
	Mom(string firstName,string secondName):firstName(firstName),secondName(secondName){}
	Mom()
	{

	}
	void print()
	{
		cout << firstName << secondName << endl;
	}
protected:
	string firstName;
	string secondName;
};
class son :public Mom
{
public:
	//调用了父类的Mom(firstName, secondName)构造函数
	son(string sonName, string firstName, string secondName) :Mom(firstName, secondName)
	{
		this->sonName = sonName;
	}
	//调用了父类的默认构造函数
	son(string sonName)
	{
		this->sonName = sonName;
	}
	void print()
	{
		cout << firstName << sonName << endl;
	}
protected:
	string sonName;
};
int main()
{
	son ER1("哗哗1", "PaPi", "yaya");
	ER1.print();

	son ER2("哗哗2");
	ER2.print();

	son ER3("哗哗3", "PaPi", "yaya");
	ER3.Mom::print();
	return 0;
}

四、多继承

#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
	Teacher(string name,int age,int money):name(name),age(age),money(money){}
protected:
	string name;
	int age;
	int money;
};

class Student
{
public:
	Student(string name, int age):name(name),age(age) {}
protected:
	string name;
	int age;
};
class People:public Teacher,protected Student
{
public:
	//组合类的构造函数必须调用所有父类的构造函数
	People(string name, int age, int money, string sex) :Teacher(name, age, money), Student(name, age)
	{
		this->name = name;
		this->age = age;
		this->sex = "男";
	}
	void print()
	{
		cout << name << "的性别是:" << sex << ",工资为:" << money << ",年龄为:" << age << endl;
	}
protected:
	string name;
	int age;
	string sex;
};
int main()
{
	People p1("Papiyaya", 18, 10000, "男");
	p1.print();
	return 0;
}

五、虚继承(菱形继承)

在这里插入图片描述

作用:避免产生二义性,所以把A->B,A->C的继承成为虚继承,虚继承关键字:Virtual

#include<iostream>
#include<string>
using namespace std;
class A
{
public:
	A(int a,int b):a(a),b(b){}
	int a,b;
};
class B:virtual public A
{
public:
	B(int a,int c):A(a,b),c(c){}
	int  c;
};
class C :virtual public A
{
public:
	C(int a,int d):A(a,b),d(d){}
	int d;
};
class D :public B, public C
{
//初始化需要初始祖父类和父类
public:D(int a,int b,int c,int d,int e):A(12,b),B(a,3),C(a,4),e(9){}
	  int e;
	  void print() { cout << a << "  " << b << "  " << c << "  " << d << "  " << e << endl; }
};
int main()
{
	D m(91, 92, 93, 94, 95);
	m.print();//12 92 3 4 9
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值