【C++】 45_不同的继承方式

news/2024/7/4 15:23:52

被忽略的细节:
冒号 ( : ) 表示继承关系,Parent 表示被继承的类, public 的意义是什么呢?

class Parent
{
};

class Child : public Parent
{
};

有趣的问题

是否可以将继承语句中的 public 换成 protected 或者 private ? 如果可以,与 public 继承有什么区别?

编程实验: 有趣的尝试

#include <iostream>

using namespace std;

class Parent
{
};

class Child_A : public Parent
{
};

class Child_B : protected Parent
{
};

class Child_C : private Parent
{
};

int main()
{
    return 0;
}
编译无错误,无警告

不同的继承方式

  • C++ 中支持三种不同的继承方式

    • public 继承

      • 父类成员在子类中保持原有的访问级别
    • private 继承

      • 父类成员在子类中变为私有继承
    • protected 继承

      • 父类中的公有成员变为保护成员,其它成员保持不变

clipboard.png

继承成员的访问属性 :

= Max {继承方式, 父类成员访问属性}

C++ 中的默认继承方式为 private

class Child : Parent 
{
};

<-->

class Child : Private Parent 
{
};

编程实验: 继承与访问级别深度实践

#include <iostream>

using namespace std;

class Parent
{
protected:
    int m_a;
protected:
    int m_b;
public:
    int m_c;
    
    void set(int a, int b, int c)
    {
        m_a = a;
        m_b = b;
        m_c = c;
    }
};

class Child_A : public Parent
{
public:
    void print()
    {
        cout << "m_a = " << m_a << endl;
        cout << "m_b = " << m_a << endl;
        cout << "m_c = " << m_a << endl;
    }
};

class Child_B : protected Parent
{
public:
    void print()
    {
        cout << "m_a = " << m_a << endl;
        cout << "m_b = " << m_a << endl;
        cout << "m_c = " << m_a << endl;
    }
};

class Child_C : private Parent
{
public:
    void print()
    {
        cout << "m_a = " << m_a << endl;
        cout << "m_b = " << m_a << endl;
        cout << "m_c = " << m_a << endl;
    }
};

int main()
{
    Child_A a;
    Child_B b;
    Child_C c;
    
    a.m_c = 100;
    // b.m_c = 100;  // Child_B 保护继承自 Parent ,所以所有的 public 成员全部变成了 protected 成员,因此外界无法访问
    // c.m_c = 100; // Child_C 私有继承自 Parent ,所以所有的成员全部变成了 private 成员,因此外界无法访问  

    a.set(1, 1, 1);
    // b.set(2, 2, 2);
    // c.set(3, 3, 3);
    
    a.print();
    b.print();
    c.print();

    return 0;
}
输出:
m_a = 1
m_b = 1
m_c = 1
m_a = 134515136
m_b = 134515136
m_c = 134515136
m_a = 134515161
m_b = 134515161
m_c = 134515161

遗憾的事实

  • 一般而言, C++ 工程中只使用 public 继承
  • C++ 的派生语言只支持一种继承方式(public继承)
  • protected 和 private 继承带来的复杂性远大于实用性

编程实验: C++ 派生语言初探

test.d

module D_Demo;

import std.stdio;
import std.string;

class Obj
{
protected:
    string mName;
    string mInfo;
public:
    this()
    {
        mName = "Object";
        mInfo = "";
    }
    string name()
    {
        return mName;
    }
    string info()
    {
        return mInfo;
    }
}

class Point : Obj
{
private:
    int mX;
    int mY;
public:
    this(int x, int y)
    {
        mX = x;
        mY = y;
        mName = "Point";
        mInfo = format("P(%d, %d)", mX, mY);
    }
    int x()
    {
        return mX;
    }
    int y()
    {
        return mY;
    }
}

void main(string[] args)
{
    writefln("D Demo");
    
    Point p = new Point(1, 2);
    
    writefln(p.name());
    writefln(p.info());
}
输出:
D Demo
Point
P(1, 2)

test.cs

class Obj
{
    protected string mName;
    protected string mInfo;

    public Obj()
    {
        mName = "Object";
        mInfo = "";
    }
    
    public string name()
    {
        return mName;
    }
    
    public string info()
    {
        return mInfo;
    }
}

class Point : Obj
{
    private int mX;
    private int mY;

    public Point(int x, int y)
    {
        mX = x;
        mY = y;
        mName = "Point";
        mInfo = "P(" + mX + ", " + mY + ")";
    }
    public int x()
    {
        return mX;
    }
    public int y()
    {
        return mY;
    }
}

class Program
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine("C# Demo");
        
        Point p = new Point(1, 2);
        
        System.Console.WriteLine(p.name());
        System.Console.WriteLine(p.info());
    }
}
输出:
C# Demo
Point
P(1, 2)

test.java

class Obj
{
    protected String mName;
    protected String mInfo;

    public Obj()
    {
        mName = "Object";
        mInfo = "";
    }
    
    public String name()
    {
        return mName;
    }
    
    public String info()
    {
        return mInfo;
    }
}

class Point extends Obj
{
    private int mX;
    private int mY;

    public Point(int x, int y)
    {
        mX = x;
        mY = y;
        mName = "Point";
        mInfo = "P(" + mX + ", " + mY + ")";
    }
    public int x()
    {
        return mX;
    }
    public int y()
    {
        return mY;
    }
}

class Program
{
    public static void main(String[] args)
    {
        System.out.println("Java Demo");
        
        Point p = new Point(1, 2);
        
        System.out.println(p.name());
        System.out.println(p.info());
    }
}
输出:
Java Demo
Point
P(1, 2)

你有没有读懂呢?
学习 C++ 的魅力(意义)!

小结

  • C++ 中支持三种不同的继承方式
  • 继承方式直接影响父类成员在子类中的访问属性
  • 一般而言,工程中只使用 public 的继承方式
  • C++ 的派生语言只支持 public 继承方式

以上内容参考狄泰软件学院系列课程,请大家保护原创!


http://www.niftyadmin.cn/n/2574515.html

相关文章

使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试

0、preliminary 环境搭建 Setup development environment Download the latest version of MRUnit jar from Apache website: https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit/. For example if you are using the Hadoop version 1.0.…

Powershell管理系列(十四)Exchange 2013邮箱数量统计

-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包&#xff0c;QQ:185426445.电话18666943750管理Exchange的话&#xff0c;我们首先对自己管理的邮箱数量和分布情况有所了解。打开EAC我们确实很快就可以查到有多少邮箱数量&#xff0c;如果邮箱比较多的话…

还在找MCM模板?其实都内置了!用法见下

TeX live 2015及更新版本 和 MiKTeX &#xff08;以默认选项安装&#xff09;均内置了由Liam Huang维护的mcmthesis模板&#xff08;文档类&#xff09; 已被CTAN收录&#xff0c;可以说是全球认可的MCM模板了 所以&#xff0c;小伙伴们不要在找模板了&#xff0c;这个最好用 但…

LeetCode刷题 fIRST MISSING POSITIVE

Given an unsorted integer array,find missing postive integer. For example , Given [1,2,0]return 3, and [3,4,-1,1]return 2. Your algorithm should run in O(n) time and users constant space. 分析如下&#xff1a; 首先&#xff0c;要理解题解&#xff1a; 如果输入…

数据结构与算法--克鲁斯卡尔算法 最小生成树 Python详细实现克鲁斯卡尔算法 Python详细实现最小生成树

阅读目录基本概述克鲁斯卡尔算法图解克鲁斯卡尔算法分析Python代码实现补充知识点&#xff1a;如何获取int&#xff08;long&#xff09;和 float 的最值完整代码实现基本概述 先看一个应用场景&#xff1a; 克鲁斯卡尔算法介绍&#xff1a; 克鲁斯卡尔算法图解 还是以上面的…

应用 JD-Eclipse 插件实现 RFT 中 .class 文件的反向编译

概述 反编译是一个将目标代码转换成源代码的过程。而目标代码是一种用语言表示的代码 , 这种语言能通过实机或虚拟机直接执行。文本所要介绍的 JD-Eclipse 是一款反编译的开源软件&#xff0c;它是应用于 Eclipse 开发平台的插件&#xff0c;它可以帮助开发人员在调试程序的过程…

C语言作业小学生的算术题,C语言大型作业-教小学生算数.doc

..《C语言工程训练》课程设计题目&#xff1a; 教小学生算数学 生 学 号 &#xff1a;学 生 姓 名 &#xff1a;指 导 教 师 &#xff1a;需求分析通过此系统实现以下功能做个位数&#xff0c;十位数的加&#xff0c;减&#xff0c;乘和除&#xff0c;减法不能得负数&#xff0…

Linux常用功能脚本

设置定时任务crontab -e 1 0 * * * /bin/find /mnt/tomcat/logs/ -mtime 3 -type f -name "*.log" -exec /bin/rm -rf {} \; 每天凌晨一分定时清除Tomcat的日志脚本linux下Tomcat自动备份 1、页面文件在/home/edn/tomcat6/webapps目录下&#xff0c;备份文件存放在/h…