目录
如果继承时不显示声明是 private,protected,public 继承,则默认是 private 继承,在 struct 中默认 public 继承:
面向对象程序设计中最重要的一个概念是继承。继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易。这样做,也达到了重用代码功能和提高执行效率的效果。
当创建一个类时,您不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可。这个已有的类称为基类,新建的类称为派生类。
有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性。
1.public 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:public, protected, private
2.protected 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:protected, protected, private
3.private 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:private, private, private
但无论哪种继承方式,上面两点都没有改变:
1.private 成员只能被本类成员(类内)和友元访问,不能被派生类访问;
2.protected 成员可以被派生类访问。
- #include
- #include
- using namespace std;
-
- class A{
- public:
- int a;
- A(){
- a1 = 1;
- a2 = 2;
- a3 = 3;
- a = 4;
- }
- void fun(){
- cout << a << endl; //正确
- cout << a1 << endl; //正确
- cout << a2 << endl; //正确
- cout << a3 << endl; //正确
- }
- public:
- int a1;
- protected:
- int a2;
- private:
- int a3;
- };
- class B : public A{
- public:
- int a;
- B(int i){
- A();
- a = i;
- }
- void fun(){
- cout << a << endl; //正确,public成员
- cout << a1 << endl; //正确,基类的public成员,在派生类中仍是public成员。
- cout << a2 << endl; //正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。
- cout << a3 << endl; //错误,基类的private成员不能被派生类访问。
- }
- };
- int main(){
- B b(10);
- cout << b.a << endl;
- cout << b.a1 << endl; //正确
- cout << b.a2 << endl; //错误,类外不能访问protected成员
- cout << b.a3 << endl; //错误,类外不能访问private成员
- system("pause");
- return 0;
- }
- #include
- #include
- using namespace std;
- class A{
- public:
- int a;
- A(){
- a1 = 1;
- a2 = 2;
- a3 = 3;
- a = 4;
- }
- void fun(){
- cout << a << endl; //正确
- cout << a1 << endl; //正确
- cout << a2 << endl; //正确
- cout << a3 << endl; //正确
- }
- public:
- int a1;
- protected:
- int a2;
- private:
- int a3;
- };
- class B : protected A{
- public:
- int a;
- B(int i){
- A();
- a = i;
- }
- void fun(){
- cout << a << endl; //正确,public成员。
- cout << a1 << endl; //正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。
- cout << a2 << endl; //正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。
- cout << a3 << endl; //错误,基类的private成员不能被派生类访问。
- }
- };
- int main(){
- B b(10);
- cout << b.a << endl; //正确。public成员
- cout << b.a1 << endl; //错误,protected成员不能在类外访问。
- cout << b.a2 << endl; //错误,protected成员不能在类外访问。
- cout << b.a3 << endl; //错误,private成员不能在类外访问。
- system("pause");
- return 0;
- }
- #include
- #include
- using namespace std;
- class A{
- public:
- int a;
- A(){
- a1 = 1;
- a2 = 2;
- a3 = 3;
- a = 4;
- }
- void fun(){
- cout << a << endl; //正确
- cout << a1 << endl; //正确
- cout << a2 << endl; //正确
- cout << a3 << endl; //正确
- }
- public:
- int a1;
- protected:
- int a2;
- private:
- int a3;
- };
- class B : private A{
- public:
- int a;
- B(int i){
- A();
- a = i;
- }
- void fun(){
- cout << a << endl; //正确,public成员。
- cout << a1 << endl; //正确,基类public成员,在派生类中变成了private,可以被派生类访问。
- cout << a2 << endl; //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。
- cout << a3 << endl; //错误,基类的private成员不能被派生类访问。
- }
- };
- int main(){
- B b(10);
- cout << b.a << endl; //正确。public成员
- cout << b.a1 << endl; //错误,private成员不能在类外访问。
- cout << b.a2 << endl; //错误, private成员不能在类外访问。
- cout << b.a3 << endl; //错误,private成员不能在类外访问。
- system("pause");
- return 0;
- }
- include
- using namespace std;
- class Line{
- int a;
- };
- int main() {
- Line line;
- line.a = 5;
- cout<
- }
上面是会报错的,应该改成:
- class Line{
- public:
- int a;
- };
如果继承时不显示声明是 private,protected,public 继承,则默认是 private 继承,在 struct 中默认 public 继承:
- class B : A {};
- B b;
- b.a; //错误
- b.a1; //错误
- b.a2; //错误
- b.a3; //错误
总结一下三种继承方式:
继承方式 基类的public成员 基类的protected成员 基类的private成员 继承引起的访问控制关系变化概括 public继承 仍为public成员 仍为protected成员 不可见 基类的非私有成员在子类的访问属性不变 protected继承 变为protected成员 变为protected成员 不可见 基类的非私有成员都为子类的保护成员 private继承 变为private成员 变为private成员 不可见 基类中的非私有成员都称为子类的私有成员
多继承
多继承即一个子类可以有多个父类,它继承了多个父类的特性。
C++ 类可以从多个类继承成员
其中,访问修饰符继承方式是 public、protected 或 private 其中的一个,用来修饰每个基类,各个基类之间用逗号分隔
示例:
- #include
-
- using namespace std;
-
- // 基类 Shape
- class Shape
- {
- public:
- void setWidth(int w)
- {
- width = w;
- }
- void setHeight(int h)
- {
- height = h;
- }
- protected:
- int width;
- int height;
- };
-
- // 基类 PaintCost
- class PaintCost
- {
- public:
- int getCost(int area)
- {
- return area * 70;
- }
- };
-
- // 派生类
- class Rectangle: public Shape, public PaintCost
- {
- public:
- int getArea()
- {
- return (width * height);
- }
- };
-
- int main(void)
- {
- Rectangle Rect;
- int area;
-
- Rect.setWidth(5);
- Rect.setHeight(7);
-
- area = Rect.getArea();
-
- // 输出对象的面积
- cout << "Total area: " << Rect.getArea() << endl;
-
- // 输出总花费
- cout << "Total paint cost: $" << Rect.getCost(area) << endl;
-
- return 0;
- }
编译结果:
- Total area: 35
- Total paint cost: $2450
多继承(环状继承)
多继承(环状继承),A->D, B->D, C->(A,B),例如:
- class D{......};
- class B: public D{......};
- class A: public D{......};
- class C: public B, public A{.....};
这个继承会使D创建两个对象,要解决上面问题就要用虚拟继承格式
格式:class 类名: virtual 继承方式 父类名
- class D{......};
- class B: virtual public D{......};
- class A: virtual public D{......};
- class C: public B, public A{.....};
虚继承--(在创建对象的时候会创建一个虚表)在创建父类对象的时候
- A:virtual public D
- B:virtual public D
示例:
- #include
-
- using namespace std;
- //基类
-
- class D
- {
- public:
- D(){cout<<"D()"<
- ~D(){cout<<"~D()"<
- protected:
- int d;
- };
-
- class B:virtual public D
- {
- public:
- B(){cout<<"B()"<
- ~B(){cout<<"~B()"<
- protected:
- int b;
- };
-
- class A:virtual public D
- {
- public:
- A(){cout<<"A()"<
- ~A(){cout<<"~A()"<
- protected:
- int a;
- };
-
- class C:public B, public A
- {
- public:
- C(){cout<<"C()"<
- ~C(){cout<<"~C()"<
- protected:
- int c;
- };
-
- int main()
- {
- cout << "Hello World!" << endl;
- C c; //D, B, A ,C
- cout<<sizeof(c)<
- return 0;
- }
编译结果:
-
相关阅读:
CTF-sql注入(X-Forwarded-For)【简单易懂】
【王道计算机组成原理】第三章 存储系统
Windows 图像处理组件(WIC)读写位深度24位的 bmp 文件
Python BeautifulSoup4 入门使用
JavaScript -- 多种类型转换方法总结
【redis-01】redis整合springboot实现简单的增删改查
如何防止服务器网站被黑
好消息!北京、珠海PMP考试时间来啦
2024绿色能源、城市规划与环境国际会议(ICGESCE 2024)
浏览器绑定快捷键KeyboardEvent
-
原文地址:https://blog.csdn.net/m0_74712453/article/details/133078926