zoukankan      html  css  js  c++  java
  • 3.3 C++改变基类成员在派生类中的访问属性

    参考:http://www.weixueyuan.net/view/6360.html

    总结:

      使用using声明可以改变基类成员在派生类中的访问属性。  

    private:
       using book::setprice;

    使用using声明可以改变基类成员在派生类中的访问属性。我们知道基类的公有成员经过公有继承,在派生类中其属性为public的,但是通过using 声明,我们可以将其改为private或protected属性。

    例1:

    enum language{cpp, java, python,javascript, php, ruby};
    
    class book
    {
    public:
        void setprice(double a);
        double getprice()const;
        void settitle(char* a);
        char * gettitle()const;
        void display();
    private:
        double price;
        char * title;
    };
    
    class codingbook: public book
    {
    public :
        void setlang(language lang);
        language getlang(){return lang;}
    private:
        language lang;
            using book::setprice;
    };


    通过例1这样的定义,则下面的主函数就会编译错误,在think类对象调用setlang和settitle函数时都不会有问题,因为这两个函数的属性为public,可以访问。唯独setprice函数通过using声明后,由public属性变为了private属性了。

    复制格式化新窗口
     
    int main()
    {
        codingbook think;
        think.setlang(cpp);
        think.settitle("Thinking in C++");
        think.setprice(78.9);  //compile error
        return 0;
    }
    
    
  • 相关阅读:
    验证SMTP工作过程
    FileZilla FTP服务器的安装和配置
    最后一块石头的重量
    不用加号的加法
    同构字符串
    最长公共子序列
    Telnet 验证HTTP工作过程
    矩阵的最小路径和
    子数组的最大累加和问题
    海思开发板——YOLOv3模型移植(4)
  • 原文地址:https://www.cnblogs.com/yongpan/p/7551165.html
Copyright © 2011-2022 走看看