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) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...
随机推荐
- numpy的prod()函数和pad()函数
1.np.prod()函数用来计算所有元素的乘积,对于有多个维度的数组可以指定轴,如axis=1指定计算每一行的乘积. 2.np.lib.pad()函数用来把原来的list在原来的维度上进行扩展 例1 ...
- 4.keras实现-->生成式深度学习之用变分自编码器VAE生成图像(mnist数据集和名人头像数据集)
变分自编码器(VAE,variatinal autoencoder) VS 生成式对抗网络(GAN,generative adversarial network) 两者不仅适用于图像,还可以 ...
- (转)mysql数据文件解析
一 数据文件 在 MySQL中每一个数据库都会在定义好(或者默认)的数据目录下存在一个以数据库名字命名的文件夹,用来存放该数据库中各种表数据文件.不同的 MySQL存储引擎有各自不同的数据文件,存放位 ...
- jmeter 测试websocket接口(二)
1.到https://github.com/maciejzaleski/JMeter-WebSocketSampler下载Jmeter的WebSocket协议的支持插件:JMeterWebSocket ...
- jxl(Java Excel API) 使用方法 【2】
JAVA EXCEL API简介 Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该 API非Win ...
- chkconfig添加进入服务后,出现的现象
比如在php-fpm添加服务中,一部分脚步如下 #!/bin/sh # # php-fpm - this script starts and stops the php-fpm ...
- Web前端开发推荐阅读书籍、学习课程下载
转自http://www.xuanfengge.com/fe-books.html 前言 学校里没有前端的课程,那如何学习JavaScript,又如何使自己成为一个合格的前端工程师呢? 除了在项目中学 ...
- windows中xcopy命令详解
一.格式: 二.举例说明: 1.复制文件,文件路径有空格的,那么就使用双引号括起来.如果目标路径已经有相同文件了,使用覆盖方式而不进行提示.在复制文件的同时也复制空目录或子目录 xcopy ...
- JS多重判断 / ES6 includes
Array.includes () 判断数组是否包含某个元素 直接返回true或者false表示是否包含元素,对NaN一样能有有效 const arr = ['1', '2', 'a', 'b' , ...
- Js基础知识5-函数返回值、函数参数、函数属性、函数方法
函数返回值 所有函数都有返回值,没有return语句时,默认返回内容为undefined,和其他面向对象的编程语言一样,return语句不会阻止finally子句的执行. function testF ...