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

发布时间:2019-06-29 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 45_不同的继承方式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

被忽略的细节
冒号 ( : ) 表示继承关系,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 继承

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

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

继承成员的访问属性 :

= 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 继承方式

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

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 45_不同的继承方式全部内容,希望文章能够帮你解决【C++】 45_不同的继承方式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。