有些类与类之间存在特殊的关系。
我们发现,定义这些类时,下级别的成员除了拥有上一级的共性,还有自己的特性。
这个时候我们就可以考虑利用继承的技术,减少重复代码。
继承的好处:
++可减少重复代码++
一、基本语法
class A : public B;
A 类称为子类 或 派生类
B 类称为父类 或 基类
例如我们看到很多网站中,都有公共的头部,公共的底部,甚至公共的左侧列表,只有中心内容不同。
接下来我们分别利用普通写法和继承的写法来实现网页中的内容,看一下继承存在的意义以及好处。
普通实现:(繁琐,难以实现,内容复杂,低级程序员)
//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 << "C++学科视频" << endl;
}
};
void test01()
{
//Java页面
cout << "Java下载视频页面如下: " << endl;
Java ja;
ja.header();
ja.footer();
ja.left();
ja.content();
cout << "--------------------" << endl;
//Python页面
cout << "Python下载视频页面如下: " << endl;
Python py;
py.header();
py.footer();
py.left();
py.content();
cout << "--------------------" << endl;
//C++页面
cout << "C++下载视频页面如下: " << endl;
CPP cp;
cp.header();
cp.footer();
cp.left();
cp.content();
}
int main() {
test01();
system("pause");
return 0;
}
但是运用继承的话:
通过继承,减少很多代码量,就很舒服。
#include<iostream>
using namespace std;
#include<string>
//函数调用()运算符重载
class Basetool
{
public:
void head()
{
cout << "页面设置,详情信息……(头部信息)" << endl;
}
void foot()
{
cout << "合作方式,快速加入……(底部信息)" << endl;
}
void left()
{
cout << "c++,python,java……(左侧信息)" << endl;
}
};
class python: public Basetool
{
public:
void content()
{
cout << "python的界面(内容信息)……" << endl;
}
};
class cpp : public Basetool
{
public:
void content()
{
cout << "c++的界面(内容信息)……" << endl;
}
};
class java : public Basetool
{
public:
void content()
{
cout << "java的界面(内容信息)……" << endl;
}
};
void test01()
{
python py;
py.head();
py.foot();
py.left();
py.content();
cout << "+++++++++++++++++++++++" << endl;
cpp cp;
cp.head();
cp.foot();
cp.left();
cp.content();
cout << "+++++++++++++++++++++++" << endl;
java y;
y.head();
y.foot();
y.left();
y.content();
cout << "+++++++++++++++++++++++" << endl;
}
int main() {
test01();
system("pause");
return 0;
}
总结:
派生类中的成员,包含两大部分:
一类是从基类继承过来的,一类是自己增加的成员。
从基类继承过过来的表现其共性,而新增的成员体现了其个性。
二、继承的方式
继承方式一共有三种:
- 公共继承
- 保护继承
- 私有继承
逻辑如图:
#include<iostream>
using namespace std;
#include<string>
//函数调用()运算符重载
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
//公共继承
class Son1 : public Base
{
public:
void func()
{
m_A=32 ; //可访问 public权限
m_B=3 ; //可访问 protected权限,类外不可访问,在继承中,儿子可以访问父亲
//m_C; //不可访问
}
};
void test01()
{
Son1 s1;
s1.m_A = 78;//其他类只能访问到公共权限
//s1.m_B=2 保护权限类外不可访问,
}
class Son2 : protected Base
{
public:
void func()
{
m_A; //可访问 private权限,父类中public成员到子类变为protected成员
m_B; //可访问 private权限,父类中protected成员到子类变为protected成员
//m_C; //不可访问
}
};
void test02()
{
Son2 s;
//s.m_A; //不可访问
}
class Son3 :private Base
{
public:
void func()
{
m_A; //可访问 private权限,父类中public成员到子类变为private成员
m_B; //可访问 private权限,父类中protected成员到子类变为private成员
//m_C; //不可访问
}
};
void test03()
{
Son3 f;
//Son3是私有继承,所以继承Son3的属性在GrandSon3中都无法访问到
//m_A;
//m_B;
//m_C;
}
class grandson : public Son3
{
public:
void func()
{
m_A = 1;//到了Son3中,m_A,m_B变成了私有,即时是儿子,也访问不到。
m_B = 2;
}
};
int main() {
test01();
system("pause");
return 0;
}
三、继承中的对象模型
使用
进行操作。首先切换到本目录
然后:
cl /d1 reportSingleClassLayoutSon hello.cpp
报告单个类的布局+类名。
(tap键可补齐)
我们发现:
继承后,父类的所有都来了包括私有,也占用空间。
>父类中所有非静态成员都被继承下去了
>父类中私有成员被编译器隐藏了,因此访问不到,但是确实被继承下去了。
#include<iostream>
using namespace std;
#include<string>
//函数调用()运算符重载
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
//公共继承
class Son : public Base
{
public:
int m_D;
};
void test01()
{
//16
//父类中所有非静态成员都被继承下去了
//父类中私有成员被编译器隐藏了,因此访问不到,但是确实被继承下去了。
cout << "子类的大小" << sizeof(Son) << endl;
}
int main() {
test01();
//test02();
system("pause");
return 0;
}
查看对象模型如图: