继承
有些类与类之间存在特殊的关系,(如猫类和加菲猫类、狗类和哈士奇类),下一级别的类除了具有上一级的共性,还有自己的特性。可以利用继承技术,减少重复代码。
1、概述
继承的基本语法
语法:class 子类 : 继承方式 父类
子类 -> 派生类
父类 -> 基类
派生类中的成员包含两部分,一部分是从基类继承来的,另一部分是自己增加的成员;从基类继承来的表现其共性,自己新增的成员表现其个性。
/*
例:我们看到很多网站中,都有公共的头部,公共的底部,甚至公共的左侧列表,只有中心内容不同。接下来我们分别利用普通写法和继承的写法来实现网页中的内容。
*/
//普通实现页面
java页面
//class Java
//{
//public:
// void header()
// {
// cout << "首页、公开课、登录、注册……(公共头部)" << endl;
// }
// void footer()
// {
// cout << "帮助中心、交流合作,站内地图……(公共底部)" << endl;
// }
// void left()
// {
// cout << "Java、Python、C++、……(公共分类列表)" << endl;
// }
// void content()//内容
// {
// cout << "Java学科视频" << endl;
// }
//};
Python页面
//class Python
//{
//public:
// void header()
// {
// cout << "首页、公开课、登录、注册……(公共头部)" << endl;
// }
// void footer()
// {
// cout << "帮助中心、交流合作,站内地图……(公共底部)" << endl;
// }
// void left()
// {
// cout << "Java、Python、C++、……(公共分类列表)" << endl;
// }
// void content()//内容
// {
// cout << "Python学科视频" << endl;
// }
//};
C++页面
//class CPP
//{
//public:
// void header()
// {
// cout << "首页、公开课、登录、注册……(公共头部)" << endl;
// }
// void footer()
// {
// cout << "帮助中心、交流合作,站内地图……(公共底部)" << endl;
// }
// void left()
// {
// cout << "Java、Python、C++、……(公共分类列表)" << endl;
// }
// void content()//内容
// {
// cout << "CPP学科视频" << endl;
// }
//};
//继承实现页面
//公共页面
class BasePage
{
public:
void header()
{
cout << "首页、公开课、登录、注册……(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作,站内地图……(公共底部)" << endl;
}
void left()
{
cout << "Java、Python、C++、……(公共分类列表)" << endl;
}
};
//Java页面
class Java : public BasePage
{
public:
void content()//内容
{
cout << "Java学科视频" << endl;
}
};
//Python页面
class Python : public BasePage
{
public:
void content()//内容
{
cout << "Python学科视频" << endl;
}
};
//C++页面
class CPP : public BasePage
{
public:
void content()//内容
{
cout << "CPP学科视频" << endl;
}
};
void test34()
{
Java j1;
j1.header();
j1.footer();
j1.left();
j1.content();
cout << "----------无情的分割线----------" << endl;
Python p1;
p1.header();
p1.footer();
p1.left();
p1.content();
cout << "----------无情的分割线----------" << endl;
CPP c1;
c1.header();
c1.footer();
c1.left();
c1.content();
}
void main39()
{
test34();
system("pause");
}
2、继承方式
3种继承方式:
- 公共继承
- 保护继承
- 私有继承
class Father
{
public:
int p_A;
protected:
int p_B;
private:
int p_C;
};
//1.公共继承
class Son1 : public Father
{
public:
void test()
{
p_A = 10;//父类中的公共权限成员,到子类依然是 公共权限
p_B = 20;//父类中的保护权限成员,到子类依然是 保护权限
//p_C = 30;//父类中的私有权限成员,到子类无法访问
}
};
void test35()
{
Son1 s11;
s11.p_A = 100;
//s11.p_B = 200;//保护权限,类外无法访问
}
//2.保护继承
class Son2 : protected Father
{
public:
void test()
{
p_A = 10;//父类中的公共权限成员,到子类是 保护权限
p_B = 20;//父类中的保护权限成员,到子类是 保护权限
//p_C = 30;//父类中的私有权限成员,到子类无法访问
}
};
void test36()
{
Son2 s21;
//s21.p_A = 100;//保护权限,类外无法访问
//s21.p_B = 200;//保护权限,类外无法访问
}
//3.私有继承
class Son3 : private Father
{
public:
void test()
{
p_A = 10;//父类中的公共权限成员,到子类是 私有权限
p_B = 20;//父类中的保护权限成员,到子类是 私有权限
//p_C = 30;//父类中的私有权限成员,到子类无法访问
}
};
void test37()
{
Son3 s31;
//s31.p_A = 100;//私有权限,类外无法访问
//s31.p_B = 200;//私有权限,类外无法访问
}
class GrandSon3 : public Son3
{
public:
void test()
{
//p_A = 10;//父类中的公共权限成员,到Son3类是 私有权限,故GrandSon类也访问不到
//p_B = 20;//父类中的保护权限成员,到Son3类是 私有权限,故GrandSon类也访问不到
//p_C = 30;//父类中的私有权限成员,到子类无法访问
}
};
3、继承中的对象模型
问题: 从父类继承过来的成员,哪些属于子类对象中?
答 : 父类中所有的(非静态)成员属性都会被子类继承下去,父类中私有权限成员 也被继承下去了,但是被编译器隐藏了,所以访问不到!!!
class Father1
{
public:
int p_A;
protected:
int p_B;
private:
int p_C;
};
class Son11 : public Father1
{
public:
int p_D;
};
void test38()
{
cout << "size of Son = " << sizeof(Son11) << endl; //16
//父类中所有的(非静态)成员属性都会被子类继承下去
//父类中私有权限成员 也被继承下去了,但是被编译器隐藏了,所以访问不到!!!
}
void main40()
{
test38();
system("pause");
}
利用开发人员命令提示工具查看对象模型
跳转盘符 -> 跳转文件路径 -> 查看命名(dir) -> cl /d1 reportSingleClassLayout类名 文件名’
例 :
cd C:\Users\machenike\Desktop\HelloChina
C:\Users\machenike\Desktop\HelloChina>dir
C:\Users\machenike\Desktop\HelloChina>cl /d1 reportSingleClassLayoutSon11 HelloChina.cpp
4、继承中构造和析构的顺序
//子类继承父类后,当创建子类对象时,也会调用父类的构造函数
//问题:父类和子类的构造、析构顺序谁先谁后?
class Father2
{
public:
int p_A;
protected:
int p_B;
private:
int p_C;
public:
Father2()
{
cout << "Father2 构造函数" << endl;
}
~Father2()
{
cout << "Father2 析构函数" << endl;
}
};
class Son12 : public Father2
{
public:
Son12()
{
cout << "Son12 构造函数" << endl;
}
~Son12()
{
cout << "Son12 析构函数" << endl;
}
};
void test39()
{
Son12 s1;
//先构造父类,再构造子类,析构顺序与构造顺序相反
}
int main41()
{
test39();
system("pause");
return 0;
}
5、继承同名成员处理方式
//问题:当子类与父类出现同名的成员,如何通过子类对象,访问到子类或者父类中的同名数据?
访问子类同名成员 直接访问
访问父类同名成员 加作用域
class Father3
{
public:
int m_A;
Father3()
{
m_A = 10;
}
void test()
{
cout << "Son test 的调用" << endl;
}
void test(int a)
{
cout << "Son test(int a) 的调用" << endl;
}
};
class Son13 : public Father3
{
public:
int m_A;
Son13()
{
m_A = 20;
}
void test()
{
cout << "Son test 的调用" << endl;
}
};
//同名成员属性处理方式
void test40()
{
Son13 s1;
cout << "Father m_A = " << s1.Father3::m_A << endl;
cout << "Son m_A = " << s1.m_A << endl;
}
//同名成员函数处理方式
void test41()
{
Son13 s2;
s2.test(); //直接调用
s2.Father3::test(); //加作用域
s2.Father3::test(10);
//如果子类中出现和父类同名的成员函数,子类会隐藏掉父类中所有同名成员函数,要想访问,必须加作用域
}
int main42()
{
test40();
test41();
system("pause");
return 0;
}
6、继承同名静态成员处理方式
问题:继承中同名的静态成员在子类对象上是如何进行访问的?
//静态成员和非静态成员出现同名,处理方式相同,唯一区别是 静态变量可以通过类名直接访问
访问子类同名成员 直接访问
访问父类同名成员 加作用域
class Father4
{
public:
static int m_A;
static void test()
{
cout << "Son test 的调用" << endl;
}
static void test(int a)
{
cout << "Son test(int a) 的调用" << endl;
}
};
int Father4::m_A = 10;
class Son14 : public Father4
{
public:
static int m_A;
static void test()
{
cout << "Son test 的调用" << endl;
}
};
int Son14::m_A = 20;
//同名成员属性处理方式
void test42()
{
//1.通过对象访问
Son14 s1;
cout << "Father m_A = " << s1.Father4::m_A << endl;
cout << "Son m_A = " << s1.m_A << endl;
//2.通过类名访问
cout << "Father m_A = " << Son14::m_A << endl;
cout << "Son m_A = " << Son14::Father4::m_A << endl;
//第一个::代表通过类名方式访问;第二个::代表访问父类作用域下
}
//同名成员函数处理方式
void test43()
{
//1.通过对象访问
Son14 s2;
s2.test(); //直接调用
s2.Father4::test(); //加作用域
//2.通过类名访问
Son14::test();
Son14::Father4::test();
Son14::Father4::test(10);
//如果子类中出现和父类同名的成员函数,子类会隐藏掉父类中所有同名成员函数,要想访问,必须加作用域
}
int main43()
{
test42();
test43();
system("pause");
return 0;
}
7、多继承语法
//C++允许一个类继承多个父类
语法:class 子类 : 继承方式 父类1, 继承方式 父类2, …
//多继承可能引发父类中有同名成员出现,需要加作用域区分。
因此,C++实际开发中不建议用多继承
8、菱形继承(钻石继承)
概念:两个派生类继承同一个基类,又有某个类同时继承这两个派生类。
//动物类
class Animal
{
public:
int m_A;
};
//熊类
class Bear : public Animal {};
//猫类
class Cat : public Animal {};
//熊猫类
class Panda : public Bear, public Cat {};
void test44()
{
Panda p1;
//p1.m_A = 10; //不明确,无法区分是哪个类继承来的
p1.Bear::m_A = 10; //可以加 作用域解决
p1.Cat::m_A = 20;
cout << "p1.Bear::m_A = " << p1.Bear::m_A << endl;
cout << "p1.Cat::m_A = " << p1.Cat::m_A << endl;
//矛盾:p1的m_A到底等于多少?
//我们知道这份数据有一份就可以了,菱形继承导致数据有两份,浪费资源
}
//解决方法: 利用虚继承 virtual
//Animal类称为虚基类
class Bear1 : virtual public Animal {};
class Cat1 : virtual public Animal {};
class Panda1 : public Bear1, public Cat1 {};
void test44_1()
{
Panda1 p1;
p1.Bear1::m_A = 10;
p1.Cat1::m_A = 20;
p1.m_A = 10;
cout << "p1.Bear::m_A = " << p1.Bear1::m_A << endl;
cout << "p1.Cat::m_A = " << p1.Cat1::m_A << endl;
cout << "p1.m_A = " << p1.m_A << endl;
}
void main44()
{
//test44();
test44_1();
system("pause");
}
(哔哩哔 哩黑马程序员 C++教程 学习笔记,如有侵权请联系删除)