• 类的继承(1)


    /*类的继承(1)*/

    #include "stdafx.h"
    #include <iostream.h>
    #include <string.h>

    class A
    {
    public:
        A()
        {
            m_nData = 10;
            m_nPublic_A = 1;
            m_nProtected_A = 2;
            m_nPrivate_A = 3;
        }
        //
    成员函数是在类内
        void FunA()
        {
            int nTemp = m_nProtected_A;
        }
        void Show(int i)
        {
            cout << "
    基类的SHOW" << endl;
        }
    public:
        int m_nData;
        int m_nPublic_A;
    protected:
        int m_nProtected_A;
    private:
        int m_nPrivate_A;
    };

    //
    继承方式 
    //
    影响的是 A的成员 在派生类C中或则是全局函数中的 访问方式
    //
    派生类的成员函数可以访问基类的公有成员和保护类成员
    //
    派生类拥有基类的方法和数据成员

    class B : public A
    {
    public:
        B()
        {
            m_nData = 20;
        }
        void FunB()
        {
            m_nPublic_A = 1;
            m_nProtected_A = 2;
            //m_nPrivate_A = 3;
           
            m_nPublic_B = 4;
            m_nProtected_B = 5;
            m_nPrivate_B = 6;
        }
       
        //
    如果派生类的函数名和基类的函数名同名,参数无所谓
        //
    两个函数构成隐藏关系

        void Show()
        {
            A::Show(1);
            cout << "
    派生类的SHOW" << endl;
        }
        //
    在一个作用域中,两个函数函数名一样,参数类型,个数,顺序不一样,
        //
    两个函数构成重载关系

        void Show(char*)
        {
            cout << "
    派生类的SHOW(char*)" << endl;
        }
    public:
        int m_nData;
        int m_nPublic_B;
    protected:
        int m_nProtected_B;
    private:
        int m_nPrivate_B;
    };


    class C : private B
    {
    public:
        void FunC()
        {
            //m_nPublic_A = 1;
            //m_nProtected_A = 2;
            //m_nPrivate_A = 3;
        }
    };

    int main(int argc, char* argv[])
    {
        //
    在派生类中可隐藏基类的任何函数,成员
        B theB;
        theB.Show();
        theB.Show("Hello");
        theB.A::Show(1);
        int nData = theB.A::m_nData;
        int nData1 = theB.m_nPublic_A;
        return 0;
    }

  • 相关阅读:
    前端常用插件收藏文章
    vue+ts修改父组件属性的写法。
    JS new date在IOS出现的问题
    js 和各种屏幕高度的写法
    react 配置ant时遇见的一个Error: Multiple configuration files found. Please remove one: – package.json#babel – .babelrc 解决方案
    vue 的sync用法
    VUE Right-hand side of ‘instanceof’ is not an object 解决方案
    记录一下navicat的快捷键
    什么是servlet(转)
    Java位运算在程序设计中的使用:位掩码(BitMask)
  • 原文地址:https://www.cnblogs.com/w413133157/p/1657044.html
Copyright © 2020-2023  润新知