以下代码会报错

#include <iostream>
using namespace std; class Sofa
{
public:
Sofa();
~Sofa(); void sit() {
cout<<"sit!"<<endl;
} void setWeight(int w) {
this->weight = w;
} int getWeight() {
return this->weight;
} void showWeight() {
cout<<this->weight<<endl;
} private: int weight; }; Sofa::Sofa()
{
} Sofa::~Sofa()
{
} class Bed
{
public:
Bed();
~Bed(); void lie() {
cout<<"lie!"<<endl;
} void setWeight(int w) {
this->weight = w;
} int getWeight() {
return this->weight;
} void showWeight() {
cout<<this->weight<<endl;
} private: int weight;
}; Bed::Bed()
{
} Bed::~Bed()
{
} class Sofabed : public Bed, public Sofa
{
public:
Sofabed();
~Sofabed(); void showWeight() { Sofa::showWeight();
cout<<"&&"<<endl;
Bed::showWeight(); } private: }; Sofabed::Sofabed()
{
} Sofabed::~Sofabed()
{
} int main () { Sofabed myfur;
myfur.sit();
myfur.lie(); myfur.setWeight(12); // 不清楚是哪个函数,会报错
myfur.Sofa::setWeight(); // 要写 [obj.classname::function(12)] ;
myfur.Sofa::showWeight();
myfur.Bed::setWeight();
myfur.Bed::showWeight(); // sofa & bed 的 member 是两个不一样的。 // 也可以在 derived class 里,overload 名字冲突的函数
myfur.showWeight(); system("pause");
return ; }

修改的代码

#include <iostream>
using namespace std; class Sofa
{
public:
Sofa(){
cout<<"Sofa()"<<endl;
}
~Sofa(){
cout<<"~Sofa()"<<endl;
} void sit() {
cout<<"sit!"<<endl;
} void setWeight(int w) {
this->weight = w;
} int getWeight() {
return this->weight;
} void showWeight() {
cout<<this->weight<<endl;
} private: int weight; }; class Bed
{
public:
Bed(){
cout<<"Bed()"<<endl;
}
~Bed(){
cout<<"~Bed()"<<endl;
} void lie() {
cout<<"lie!"<<endl;
} void setWeight(int w) {
this->weight = w;
} int getWeight() {
return this->weight;
} void showWeight() {
cout<<this->weight<<"\n\n"<<endl;
} private: int weight;
}; class Sofabed : public Bed, public Sofa
{
public:
Sofabed(){
cout<<"Sofabed()\n\n"<<endl;
}
~Sofabed(){
cout<<"~Sofabed()"<<endl;
} void showWeight() {
cout<<"Sofa::showWeight()";
Sofa::showWeight();
cout<<"&";
cout<<this->weight;
cout<<"&"<<endl;
cout<<"Bed::showWeight()";
Bed::showWeight(); } void setWeight(int w) {
this->weight = w;
} private:
int weight; }; int main () { Sofabed myfur;
myfur.sit();
myfur.lie(); myfur.Sofa::setWeight(); // 要写 [obj.classname::function(12)] ;
myfur.Sofa::showWeight();
myfur.Bed::setWeight();
myfur.Bed::showWeight(); // sofa & bed 的 member 是两个不一样的。 myfur.setWeight(); // 修改后
myfur.showWeight(); return ; }

由于,继承后,编译器不清楚setWeight函数是哪个类的,所以报错了,修改后,我们调用的就是实例化的那个类的函数,所以不会报错

c++ 多继承 public的更多相关文章

  1. C++中的三种继承public,protected,private

    ( c++默认class是private继承且class内的成员默认都是private struct 默认位public 继承,struct内成员默认是public  ) 三种访问权限 public: ...

  2. C++中类继承public,protected和private关键字作用详解及派生类的访问权限

    注意:本文有时候会用Visual Studio Code里插件的自动补全功能来展示访问权限的范围(当且仅当自动补全范围等价于对象访问权限范围的时候),但是不代表只要是出现在自动补全范围内的可调用对象/ ...

  3. [C++]访问控制与继承(public,protect,private) 有时间再整理!!!

    http://www.cnblogs.com/chio/archive/2007/06/11/779408.html http://www.cnblogs.com/SelaSelah/archive/ ...

  4. [转]C++中的三种继承public,protected,private

    链接:http://www.cnblogs.com/BeyondAnyTime/archive/2012/05/23/2514964.html

  5. public private, protect. 以及继承。 草稿。

    #include <iostream>#include <thread>#include <memory> // | 父类的public成员 | 父类的protec ...

  6. 【转】C++易混知识点5:实例讲解Public Protected Private作用域,继承的区别和用意

    大学生涯,涉及到类的作用域,继承都是用的public 共有继承,当时也没想那么多,觉得共有继承多方便,多简单,反正没有太多的限制,不管是类的成员或者是基类的成员函数都可以访问.没有深究.其实这里面真是 ...

  7. c++三种继承方式public,protect,private

    C++中的三种继承public,protected,private 三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员 ...

  8. C++ public private protect 继承关系(链接)

    基础链接 总结:  public继承基类成员访问权限没有变化; protected继承基类public和protected权限变为protected,基类private不变. private继承基类p ...

  9. c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承

    公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...

随机推荐

  1. jpress-配合nginx与tomcat安装

    目录 1. 前言 2. yum安装tomcat 2. yum安装MySQL 3. 下载JPress并安装 4. 配置tomcat使其可以部署多个网站 5. 安装nginx并配置 6. 将已经安装好的j ...

  2. jquery 实现iframe 自适应高度

    转自: http://www.cnblogs.com/luluping/archive/2009/04/17/1437843.html 超级简单的方法,也不用写什么判断浏览器高度.宽度啥的.下面的两种 ...

  3. file_get_post实现post请求

    function Post($url, $post = null){     $context = array();     if (is_array($post)) {       ksort($p ...

  4. 我的sublime 插件配置

    一个插件就是一个软件 ,这就是sublime的理念 . 1.Packag control 给sublime配置插件当然少不了Package control ,首先安装 Package control ...

  5. 解读jquery.filtertable.min

    jQuery.FilterTable是一款表格搜索过滤和单元格高亮插件. 该插件允许你对任意表格进行条件过滤,并且它会将搜索到的结果单元格高亮显示,非常实用和强大. 使用方法在页面中引入jquery和 ...

  6. Linux服务器---ssh登录

    Ssh登录     Ssh是建立在应用层和传输层的安全协议,专门为远程登录回话和其他网络服务提供安全性.利用ssh可以有效的防止远程管理中的信息泄露问题,同时ssh传输的数据是经过压缩的,可以加快传输 ...

  7. Ubuntu系统下查看显卡相关信息

    查看显卡信息 root@ubuntu:/home/ubuntu# lspci |grep -i vga 02:00.0 VGA compatible controller: NVIDIA Corpor ...

  8. js将时间戳转化为日期格式

    function getLocalTime(nS) {        var date = new Date(nS);        var Y = date.getFullYear() + '-'; ...

  9. 关于webpack的path和publicPath。

    path:所有输出文件的目标路径; publicPath:输出解析文件的目录,url 相对于 HTML 页面 区别:path是webpack所有文件的输出的路径,必须是绝对路径,比如:output输出 ...

  10. Linux下useradd命令创建的用户不能登录的问题

    Linux下useradd命令创建的用户不能登录的问题  问题: 用useradd命令新创建一个用户tester 密码pwdtest mkdir -p /home/tester(创建文件夹) user ...