以下代码会报错

#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. 什么是anaconda【转载】

    转自:https://zhidao.baidu.com/question/525102108723657245.html https://zhidao.baidu.com/question/62475 ...

  2. 获取多达 16GB 的 Dropbox 免费空间!

    Dropbox官网

  3. CentOS.56安装Redis监控工具RedisLive

    RedisLive是一款开源的基于WEB的reids的监控工具,以WEB的形式展现出redis中的key的情况,实例数据等信息! RedisLive在github上的地址:https://github ...

  4. EXTJS4扩展实例:如何使用filter查询treepanel

    我们在使用普通的store时,extjs提供了filterBy,filter等多种方法来过滤数据达到查询效果,但在treepanel中的streeStore却没有实现这个查询,于是,就有了这篇文章. ...

  5. python图片处理(二)

    未经允许,请勿转载!!!! 这次打算先写处理图片的方法,然后再调用方法来运行 下面先写的是处理图片的方法: # -*- coding: utf-8 -*- import os import matpl ...

  6. 【最大连接数】Linux的文件最大连接数

    Too many open files ==================================== 查看当前操作系统连接数设置 ulimit -a =================== ...

  7. VS添加节点

    很喜欢添加节点来减少代码的长度,方便阅读:VS快捷键和相关设置

  8. Devenv 命令行开关

    Devenv 可用来设置集成开发环境 (IDE) 的各个选项,以及从命令行生成.调试和部署项目.使用这些开关从脚本或 .bat 文件(例如每夜生成的脚本)运行 IDE,或以特定配置启动 IDE. 说明 ...

  9. Java overload和override的区别分析

    Java overload和override的区别分析 方法的重写(Overriding)和重载(Overloading)是Java多态性的不同表现.重写(Overriding)是父类与子类之间多态性 ...

  10. Python: 合并多个字典

    现在有多个字典或者映射,想将它们从逻辑上合并为一个单一的映射后执行某些操作,比如查找值或者检查某些键是否存在. eg1: a = {'x': 1, 'z': 3 }b = {'y': 2, 'z': ...