文章目录
4 类和对象
4.6 继承
继承是面向对象三大特性之一
我们发现,定义这些类时,下级别的成员除了拥有上一级的共性,还有自己的特性。
这个时候我们就可以考虑利用继承的技术,减少重复代码。
4.6.1 继承的基本语法
class 子类 : 继承方式 父类
{
...
}
继承的好处
1.可以减少重复的代码。
2.在父类中体现共性,在子类中体现特性。
例4.6.1.1 普通实现
#include <iostream>
using namespace std;
class Java
{
public:
void header()
{
cout << "首页,公开课,登录,注册...(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
}
void left()
{
cout << "Java, Python, C++, ...(公共分类列表)" << endl;
}
void content()
{
cout << '\n' << "Java学习资料" << '\n' << endl;
}
};
class CPP
{
public:
void header()
{
cout << "首页,公开课,登录,注册...(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
}
void left()
{
cout << "Java, Python, C++, ...(公共分类列表)" << endl;
}
void content()
{
cout << '\n' << "C++学习资料" << '\n' << endl;
}
};
class Python
{
public:
void header()
{
cout << "首页,公开课,登录,注册...(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
}
void left()
{
cout << "Java, Python, C++, ...(公共分类列表)" << endl;
}
void content()
{
cout << '\n' << "Python学习资料" << '\n' << endl;
}
};
void test01()
{
Java ja;
ja.header();
ja.left();
ja.content();
ja.footer();
}
void test02()
{
Python py;
py.header();
py.left();
py.content();
py.footer();
}
void test03()
{
CPP cpp;
cpp.header();
cpp.left();
cpp.content();
cpp.footer();
}
int main()
{
test01();
test02();
test03();
return 0;
}
例4.6.1.2 继承实现
#include <iostream>
using namespace std;
class BasePage
{
public:
void header()
{
cout << "首页,公开课,登录,注册...(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
}
void left()
{
cout << "Java, Python, C++, ...(公共分类列表)" << endl;
}
};
class Java : public BasePage
{
public:
void content()
{
cout << '\n' << "Java学习资料" << '\n' << endl;
}
};
class CPP : public BasePage
{
public:
void content()
{
cout << '\n' << "C++学习资料" << '\n' << endl;
}
};
class Python : public BasePage
{
public:
void content()
{
cout << '\n' << "Python学习资料" << '\n' << endl;
}
};
void test01()
{
Java ja;
ja.header();
ja.left();
ja.content();
ja.footer();
}
void test02()
{
Python py;
py.header();
py.left();
py.content();
py.footer();
}
void test03()
{
CPP cpp;
cpp.header();
cpp.left();
cpp.content();
cpp.footer();
}
int main()
{
test01();
test02();
test03();
return 0;
}
结果如下:
首页,公开课,登录,注册...(公共头部)
Java, Python, C++, ...(公共分类列表)
Java学习资料
帮助中心,交流合作,站内地图...(公共底部)
首页,公开课,登录,注册...(公共头部)
Java, Python, C++, ...(公共分类列表)
Python学习资料
帮助中心,交流合作,站内地图...(公共底部)
首页,公开课,登录,注册...(公共头部)
Java, Python, C++, ...(公共分类列表)
C++学习资料
帮助中心,交流合作,站内地图...(公共底部)
Process returned 0 (0x0) execution time : 0.201 s
Press any key to continue.
4.6.2 继承方式
- 公共继承
- 保护继承
- 私有继承
其都不能访问父类中 private 的信息。
例4.6.2
#include <iostream>
using namespace std;
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
//公共继承
class Son1 :public Base
{
public:
void func()
{
m_A = 10; // 父类中的公共权限成员 到子类中仍然是公共权限
m_B = 10; // 父类中的保护权限成员 到子类中仍然是保护权限
//m_C = 10; // 父类中的私有权限成员 子类访问不到
}
};
void test01()
{
Son1 s1;
s1.m_A = 100;
// s1.m_B = 100; // 到Son1中 m_B是保护权限 类外访问不到
}
//保护继承
class Son2 :protected Base
{
public:
void func()
{
m_A = 10; // 父类中的公共权限成员 到子类中变为保护权限
m_B = 10; // 父类中的保护权限成员 到子类中仍然是保护权限
//m_C = 10; // 父类中的私有权限成员 子类访问不到
}
};
void test02()
{
Son2 s2;
//s2.m_A = 100; // 在Son2中 m_A变为保护权限,因此访问不到。
}
//私有继承
class Son3 :private Base
{
public:
void func()
{
m_A = 10; // 父类中的公共权限成员 到子类中变为私有权限
m_B = 10; // 父类中的保护权限成员 到子类中变为私有权限
//m_C = 10; // 父类中的私有权限成员 子类访问不到
}
};
void test03()
{
Son3 s3;
//s3.m_A = 100; // 在Son3中 m_A变为私有权限,因此访问不到。
}
int main()
{
test01();
test02();
test03();
return 0;
}
4.6.3 继承中的对象模型
父类中私有成员也是被子类继承下去了,只是由编译器给隐藏后访问不到。
例4.6.3
#include <iostream>
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 << "sizeof Son = " << sizeof(Son) << endl;
}
int main()
{
test01();
return 0;
}
结果如下:
sizeof Son = 16
Process returned 0 (0x0) execution time : 0.092 s
Press any key to continue.
4.6.4 继承中构造和析构顺序
继承中 先调用父类构造函数,再调用子类构造函数,析构顺序与构造相反。
例4.6.4
#include <iostream>
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();
return 0;
}
结果如下:
Base构造函数!
Son构造函数!
Son析构函数!
Base析构函数!
Process returned 0 (0x0) execution time : 0.084 s
Press any key to continue.
4.6.5 继承同名成员处理方式
- 子类对象可以直接访问到子类中同名成员。
- 子类对象加作用域可以访问到父类同名成员。
- 当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数。
例4.6.5.1 如果子类和父类成员同名,直接调用会调用到子类的数据。
#include <iostream>
using namespace std;
//继承中同名成员处理
class Base
{
public:
Base()
{
m_A = 100;
}
int m_A;
};
class Son :public Base
{
public:
Son()
{
m_A = 200;
}
int m_A;
};
void test01()
{
Son s;
cout << "m_A = " << s.m_A << endl;
}
int main()
{
test01();
return 0;
}
结果如下:
m_A = 200
Process returned 0 (0x0) execution time : 0.061 s
Press any key to continue.
例4.6.5.2 如果子类和父类成员同名,前面加作用域( :: )调用会调用到父类的数据。
#include <iostream>
using namespace std;
//继承中同名成员处理
class Base
{
public:
Base()
{
m_A = 100;
}
int m_A;
};
class Son :public Base
{
public:
Son()
{
m_A = 200;
}
int m_A;
};
void test01()
{
Son s;
cout << "Son 下的 m_A = " << s.m_A << endl;
cout << "Base 下的 m_A = " << s.Base::m_A << endl;
}
int main()
{
test01();
return 0;
}
结果如下:
Son 下的 m_A = 200
Base 下的 m_A = 100
Process returned 0 (0x0) execution time : 0.081 s
Press any key to continue.
4.6.6 继承同名静态成员处理方式
- 访问子类同名成员 直接访问即可。
- 访问父类同名成员 需要加作用域。
例4.6.6
#include <iostream>
using namespace std;
class Base
{
public:
static void func()
{
cout << "Base - static void func()" << endl;
}
static void func(int a)
{
cout << "Base - static void func(int a)" << 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()
{
//通过对象访问
cout << "通过对象访问: " << endl;
Son s;
cout << "Son 下 m_A = " << s.m_A << endl;
cout << "Base 下 m_A = " << s.Base::m_A << endl;
//通过类名访问
cout << "通过类名访问: " << endl;
cout << "Son 下 m_A = " << Son::m_A << endl;
cout << "Base 下 m_A = " << Son::Base::m_A << endl;
}
//同名成员函数
void test02()
{
//通过对象访问
cout << "通过对象访问: " << endl;
Son s;
s.func();
s.Base::func();
cout << "通过类名访问: " << endl;
Son::func();
Son::Base::func();
//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作作用域访问
Son::Base::func(100);
}
int main()
{
//test01();
test02();
return 0;
}
结果如下:
通过对象访问:
Son - static void func()
Base - static void func()
通过类名访问:
Son - static void func()
Base - static void func()
Base - static void func(int a)
Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.
4.6.7 多继承语法
C++允许一个类继承多个类。
语法如下:
class 子类 : 继承方式 父类1, 继承方式 父类2, ...
{
...
}
例4.6.7
#include <iostream>
using namespace std;
class Base1
{
public:
Base1()
{
m_A = 100;
}
int m_A;
};
class Base2
{
public:
Base2()
{
m_B = 200;
}
int m_B;
};
// 子类 继承Base1和Base2
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 << s.Base1::m_A << endl;
cout << s.Base2::m_B << endl;
}
int main()
{
test01();
return 0;
}
结果如下:
sizeof Son = 16
100
200
Process returned 0 (0x0) execution time : 0.039 s
Press any key to continue.
4.6.8 菱形继承
菱形继承概念:
两个派生类继承同一个基类。
又有某个类同时继承者两个派生类。
菱形继承问题:
羊继承了动物的数据,驼同样继承了动物的数据,当草泥马使用数据时,就会产生二义性。
草泥马继承自动物的数据继承了两份,其实我们应该清楚,这份数据我们只需要一份就可以。
解决方法:
- 虚继承
例4.6.8
#include <iostream>
using namespace std;
class Animal
{
public:
int m_Age;
};
//继承前加virtual关键字后,变为虚继承
//此时公共的父类Animal称为虚基类
class Sheep : virtual public Animal {
};
class Tuo : virtual public Animal {
};
class SheepTuo : public Sheep, public Tuo {
};
void test01()
{
SheepTuo st;
st.Sheep::m_Age = 100;
st.Tuo::m_Age = 200;
cout << "st.Sheep::m_Age = " << st.Sheep::m_Age << endl;
cout << "st.Tuo::m_Age = " << st.Tuo::m_Age << endl;
cout << "st.m_Age = " << st.m_Age << endl;
}
int main()
{
test01();
return 0;
}
结果如下:
st.Sheep::m_Age = 200
st.Tuo::m_Age = 200
st.m_Age = 200
Process returned 0 (0x0) execution time : 0.129 s
Press any key to continue.