c++ 多继承 public
以下代码会报错
#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的更多相关文章
- C++中的三种继承public,protected,private
( c++默认class是private继承且class内的成员默认都是private struct 默认位public 继承,struct内成员默认是public ) 三种访问权限 public: ...
- C++中类继承public,protected和private关键字作用详解及派生类的访问权限
注意:本文有时候会用Visual Studio Code里插件的自动补全功能来展示访问权限的范围(当且仅当自动补全范围等价于对象访问权限范围的时候),但是不代表只要是出现在自动补全范围内的可调用对象/ ...
- [C++]访问控制与继承(public,protect,private) 有时间再整理!!!
http://www.cnblogs.com/chio/archive/2007/06/11/779408.html http://www.cnblogs.com/SelaSelah/archive/ ...
- [转]C++中的三种继承public,protected,private
链接:http://www.cnblogs.com/BeyondAnyTime/archive/2012/05/23/2514964.html
- public private, protect. 以及继承。 草稿。
#include <iostream>#include <thread>#include <memory> // | 父类的public成员 | 父类的protec ...
- 【转】C++易混知识点5:实例讲解Public Protected Private作用域,继承的区别和用意
大学生涯,涉及到类的作用域,继承都是用的public 共有继承,当时也没想那么多,觉得共有继承多方便,多简单,反正没有太多的限制,不管是类的成员或者是基类的成员函数都可以访问.没有深究.其实这里面真是 ...
- c++三种继承方式public,protect,private
C++中的三种继承public,protected,private 三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员 ...
- C++ public private protect 继承关系(链接)
基础链接 总结: public继承基类成员访问权限没有变化; protected继承基类public和protected权限变为protected,基类private不变. private继承基类p ...
- c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承
公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...
随机推荐
- container / pull-left
<div class="container"> <h2>实例</h2> <div class="pull-left"& ...
- Django初级手册4-表单与通用视图
表单的编写 1. detail.html模版的编写 <h1>{{ poll.question }}</h1> {% if error_message %}<p>&l ...
- windows 7 中使用命令行创建WiFi热点
就是让你的电脑可以作为WiFi热点,然后供其它支持WiFi的设备上网 首先你的电脑中必须有正常使用的无线网卡 幺幺幺切克闹,开始命令吧,(注:命令是在windows中的命令提示符中运行的) 禁用承载网 ...
- 4:7 Struts实现Ajax
不使用插件: 返回数据: 使用插件: Action里面直接给User赋值,然后在前台拿值. type="json":表示返回json对象: root:表示作为跟对象 include ...
- apache源码安装
1.apr和apr-util,下载地址: http://apr.apache.org/download.cgi yum install gcc yum install libtool yum inst ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON MaxImage1
zw版[转发·台湾nvp系列Delphi例程]HALCON MaxImage1 procedure TForm1.FormShow(Sender: TObject);begin Set8087CW($ ...
- Python: 字符串开头或结尾匹配str.startswith(),str.endswith()
问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...
- python 文件操作,os.path.walk()的回调函数打印文件名
#coding=utf-8 import osdef find_file(arg,dirname,files): #for i in arg: #print i for file ...
- Python中文件的读写操作的几种方法
对文件的操作,步骤为:打开一个文件-->读取/写入内容-->保存文件 文件读写的3中模式 # 1.w 写模式,它是不能读的,如果用w模式打开一个已经存在的文件,会清空以前的文件内容,重新写 ...
- js如何模拟multipart/form-data类型的请求
var temp = document.createElement('form'); temp.action = this.data.testURL; temp.method = 'post'; te ...