1.6 继承
利用继承技术,可以减少重复代码
1.6.1 继承的基本语法
普通实现
#include <iostream>
#include <string>
using namespace std;
class Java
{
public:
void header()
{
cout << "首页、公开课、登陆、注册……(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图……(公共底部)" << endl;
}
void left()
{
cout << "Java、Python、C++、……(公共分类列表)" << endl;
}
void content()
{
cout << "Java 学科视频" << endl;
}
};
class Python
{
public:
void header()
{
cout << "首页、公开课、登陆、注册……(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图……(公共底部)" << endl;
}
void left()
{
cout << "Java、Python、C++、……(公共分类列表)" << endl;
}
void content()
{
cout << "Python 学科视频" << endl;
}
};
class CPP
{
public:
void header()
{
cout << "首页、公开课、登陆、注册……(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图……(公共底部)" << endl;
}
void left()
{
cout << "Java、Python、C++、……(公共分类列表)" << endl;
}
void content()
{
cout << "C++ 学科视频" << endl;
}
};
void test01()
{
cout << "Java 下载视频页面如下" << endl;
Java ja;
ja.header();
ja.footer();
ja.content();
ja.left();
cout << "-----------------------" << endl;
cout << "Python 下载视频页面如下" << endl;
Python py;
py.header();
py.footer();
py.content();
py.left();
cout << "-----------------------" << endl;
cout << "CPP 下载视频页面如下" << endl;
CPP cpp;
cpp.header();
cpp.footer();
cpp.content();
cpp.left();
}
int main()
{
test01();
system("pause");
return 0;
}
继承实现
优点:减少重复代码
语法:class 子类 : 继承方式 父类
子类 也称为 派生类
父类 也称为 基类
派生类中的成员包含两大部分:
一类是从基类继承过来的,一类是自己增加的成员
从基类继承过来的表现其共性,而新增的成员体现了其个性
#include <iostream>
#include <string>
using namespace std;
class BasePage
{
public:
void header()
{
cout << "首页、公开课、登陆、注册……(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图……(公共底部)" << endl;
}
void left()
{
cout << "Java、Python、C++、……(公共分类列表)" << endl;
}
};
class Java : public BasePage
{
public:
void content()
{
cout << "Java 学科视频" << endl;
}
};
class Python : public BasePage
{
public:
void content()
{
cout << "Python 学科视频" << endl;
}
};
class CPP : public BasePage
{
public:
void content()
{
cout << "C++ 学科视频" << endl;
}
};
void test01()
{
cout << "Java 下载视频页面如下" << endl;
Java ja;
ja.header();
ja.footer();
ja.content();
ja.left();
cout << "-----------------------" << endl;
cout << "Python 下载视频页面如下" << endl;
Python py;
py.header();
py.footer();
py.content();
py.left();
cout << "-----------------------" << endl;
cout << "CPP 下载视频页面如下" << endl;
CPP cpp;
cpp.header();
cpp.footer();
cpp.content();
cpp.left();
}
int main()
{
test01();
system("pause");
return 0;
}
1.6.2 继承方式
继承方式一共有三种:
#include <iostream>
#include <string>
using namespace std;
class Base1
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son1 :public Base1
{
public:
void func()
{
m_A = 10;
m_B = 10;
}
};
class Son2 :protected Base1
{
public:
void func()
{
m_A = 100;
m_B = 110;
}
};
class Son3 :private Base1
{
public:
void func()
{
m_A = 100;
m_B = 110;
}
};
class GrandSon3 :public Son3
{
public:
void func()
{
}
};
void test01()
{
Son1 s1;
s1.m_A = 100;
}
void test02()
{
Son2 s1;
}
void test03()
{
Son3 s1;
}
int main()
{
test01();
system("pause");
return 0;
}
1.6.3 继承中的对象模型
问题: 从父类继承过来的成员,哪些属于子类对象?
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son :public Base
{
public:
int m_D;
};
void test01()
{
cout << "size of Son = " << sizeof(Son) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
父类中所有非静态成员属性都会被子类继承下去
父类中私有成员经过公有继承后,在子类中存在,但不能直接访问
父类中私有成员属性是被编译器隐藏了,因此是访问不到,但是确实被继承下去了
—— 如何访问?
—— 可以通过子类从父类继承而来的父类里已实现的成员函数来间接访问
1.6.4 继承中构造和析构顺序
子类继承父类后,当创建子类对象,也会调用父类的构造函数
问题:父类和子类的构造和析构顺序是谁先谁后?
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base 构造函数" << endl;
}
~Base()
{
cout << "Base 析构函数" << endl;
}
};
class Son :public Base
{
public:
Son()
{
cout << "Son 构造函数" << endl;
}
~Son()
{
cout << "Son 析构函数" << endl;
}
};
void test01()
{
Son s;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:继承中,先调用父类构造函数,再调用子类构造函数,析构顺序与构造相反
1.6.5 继承同名成员处理方式
问题:当子类与父类出现同名的成员,如何通过子类对象,访问到子类或父类中同名的数据呢?
- 访问子类同名成员 直接访问即可
- 访问父类同名成员 需要加作用域
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
Base()
{
m_A = 100;
}
void func()
{
cout << "Base - func() 函数调用" << endl;
}
int m_A;
};
class Son :public Base
{
public:
Son()
{
m_A = 200;
}
void func()
{
cout << "Son - func() 函数调用" << endl;
}
int m_A;
};
void test01()
{
Son s1;
cout << "Son 下 m_A = " << s1.m_A << endl;
cout << "Base 下 m_A = " << s1.Base::m_A << endl;
}
void test02()
{
Son s2;
s2.func();
s2.Base::func();
}
int main()
{
test02();
system("pause");
return 0;
}
总结:
- 子类对象可以直接访问到子类中同名成员
- 子类对象加作用域可以访问到父类同名成员
- 当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数
1.6.6 继承同名静态成员处理方式
问题:继承中同名的静态成员在子类对象上如何进行访问?
静态成员和非静态成员出现同名,处理方式一致
- 访问子类同名成员 直接访问即可
- 访问父类同名成员 需要加作用域
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
static void func()
{
cout << "Base - static void func()" << endl;
}
static int m_A;
};
int Base::m_A = 100;
class Son :public Base
{
public:
static void func()
{
cout << "Son - static void func()" << endl;
}
static int m_A;
};
int Son::m_A = 200;
void test01()
{
Son s1;
cout << "Son 下 m_A = " << s1.m_A << endl;
cout << "Base 下 m_A = " << s1.Base::m_A << endl;
cout << "Son 下 m_A = " << Son::m_A << endl;
cout << "Base 下 m_A = " << Son::Base::m_A << endl;
}
void test02()
{
Son s2;
s2.func();
s2.Base::func();
Son::func();
Son::Base::func();
}
int main()
{
test02();
system("pause");
return 0;
}
1.6.7 多继承语法
C++ 允许一个类继承多个类
语法:class 子类 : 继承方式 父类1, 继承方式 父类2…
多继承可能会引发父类中有同名成员出现,需要加作用域区分
C++ 实际开发中不建议用多继承
#include <iostream>
#include <string>
using namespace std;
class Base1
{
public:
Base1()
{
m_A = 100;
}
int m_A;
};
class Base2
{
public:
Base2()
{
m_B = 100;
}
int m_B;
};
class Son :public Base1, public Base2
{
public:
Son()
{
m_C = 300;
m_D = 400;
}
int m_C;
int m_D;
};
void test01()
{
Son s;
cout << "sizeof Son = " << sizeof(s) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Base1
{
public:
Base1()
{
m_A = 100;
}
int m_A;
};
class Base2
{
public:
Base2()
{
m_A = 200;
}
int m_A;
};
class Son :public Base1, public Base2
{
public:
Son()
{
m_C = 300;
m_D = 400;
}
int m_C;
int m_D;
};
void test01()
{
Son s;
cout << "sizeof Son = " << sizeof(s) << endl;
cout << "Base1::m_A = " << s.Base1::m_A << endl;
cout << "Base2::m_A = " << s.Base2::m_A << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
1.6.8 菱形继承
菱形继承概念:
- 两个派生类继承同一个基类
- 又有某个类同时继承两给派生类
- 这种继承被称为菱形继承,或者钻石继承
class Animal
{
public:
int m_Age;
};
class Sheep :virtual public Animal{};
class Camel :virtual public Animal{};
class Cnm :public Sheep, public Camel
{
};
void test01()
{
Cnm cnm;
cnm.Sheep::m_Age = 18;
cnm.Camel::m_Age = 28;
cout << "cnm.Sheep::m_Age = " << cnm.Sheep::m_Age << endl;
cout << "cnm.Camel::m_Age = " << cnm.Camel::m_Age << endl;
}
vbptr (v - virtual; b - base; ptr - pointer) —> vbtable
(虚基类指针指向虚基类表)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)