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) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...
随机推荐
- Catch all the latest Jordan Release Dates
In case y'all missed yesterday's news, Air Jordan 13 Olive 2018 officially unveiled their 2017 Holid ...
- 【C语言】指向一维数组元素的指针
本文目录 一.用指针指向一维数组的元素 二.用指针遍历数组元素 三.指针与数组的总结 四.数组.指针与函数参数 前面我们已经学习了指针,如果指针存储了某个变量的地址,我们就可以说指针指向这个变量.数组 ...
- C# 开发圆角控件(窗体)
最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能.其中方法三既适应于控件,也适应于窗体. 先上传效果图: 方法一: 增加命名空间:using ...
- python start
由于工作关系,新学习使用了python,感觉能非常快速和方便的开发,看完<简明 Python 教程>就跃跃欲试,实际用的是发现有些和C#的理解不一样 (1)如何筛选元组 例如 recor ...
- webapi swagger学习笔记
版权声明:部分摘抄其他博主朋友的博文内容,旨在分享学习,如给您带来不便,请原谅.原文地址 http://www.cnblogs.com/yanweidie/p/5709113.html#_label3 ...
- c#获取指定时区的日期
1.首先将服务器的时间转化为utc时间,然后转换成指定时区的日期 public DateTime GetSpecificZoneNowDate(string zoneName = "Chin ...
- JSON—序列化
表单数据的序列化 用SerializeArray()将有效控件序列化为JSON对象数组? 包含name和value两个属性 SerializeArray()检测一组表单元素中的有效控件? 1.没有 ...
- MySQL从删库到跑路(六)——SQL插入、更新、删除操作
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.插入数据 1.为表的所有字段插入数据 使用基本的INSERT语句插入数据要求指定表名称和插入到新记录的值. IN ...
- Applying the Kappa architecture in the telco industry
https://www.oreilly.com/ideas/applying-the-kappa-architecture-in-the-telco-industry Kappa architectu ...
- C/C++之内存对齐
数据对齐,是指数据所在的内存地址必须是该数据长度的整数倍.DWORD数据的内存起始地址能被4除尽,WORD数据的内存起始地址能被2除尽.X86 CPU能直接访问对齐的数据,当它试图访问一个未对齐的数据 ...