首页 百科知识 ++改变基类成员在派生类中的访问属性

++改变基类成员在派生类中的访问属性

时间:2022-09-22 百科知识 版权反馈
【摘要】:enum language{cpp, java, python,javascript, php, ruby};通过例1这样的定义,则下面的主函数就会编译错误,在think类对象调用setlang和settitle函数时都不会有问题,因为这两个函数的属性为public,可以访问。唯独setprice函数通过using声明后,由public属性变为了private属性了。


例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;

}


免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

我要反馈