YTU 2622: B 虚拟继承(虚基类)-沙发床(改错题)
2622: B 虚拟继承(虚基类)-沙发床(改错题)
时间限制: 1 Sec 内存限制: 128 MB
提交: 487 解决: 393
题目描述
有一种特殊的床,既能当床(Bed)用又能当沙发(Sofa)用,所以叫沙发床(SleeperSofa)。
同时床和沙发又是一种特殊的家具(Furniture),具有一切家具的特性。
利用虚拟继承(虚基类)建立一个类的多重继承,沙发床继承了床和沙发的特性。
下面的程序中,在begin到end部分存在语法错误。请改正错误,使程序按下面输入输出的规定运行。
注意:只提交修改过的begin到end部分的代码。
#include <iostream>
using namespace std;
//家具类Furniture
class Furniture
{
public:
Furniture(double w)
{ weight=w; }
void display()
{
cout<<"weight:"<<weight<<endl;
}
protected:
double weight; //家具重量
};
//******************** begin ********************
//床类Bed
class Bed: public Furniture
{
public:
Bed(double we,double l,double wi):Furniture(we),length(l),width(wi){}
void display()
{
cout<<"length:"<<length<<endl;
cout<<"width:"<<width<<endl;
}
protected:
double length; //床的长
double width; //床的宽
};
//沙发类Sofa
class Sofa: public Furniture
{ public:
Sofa(double w,double h):Furniture(w),height(h){}
void display()
{
cout<<"height:"<<height<<endl;
}
protected:
double height; //沙发的高度
};
//沙发床
class SleeperSofa:public Bed, public Sofa
{public:
SleeperSofa(double we,double l,double wi,double h):Bed(we,l,wi),Sofa(we,h){ }
void display()
{
cout<<"weight:"<<weight<<endl;
Bed::display();
Sofa::display();
}
};
//********************* end ********************
int main()
{
double weight,length,width,height;
cin>>weight>>length>>width>>height;
SleeperSofa ss(weight,length,width,height);
ss.display();
return 0;
}
输入
依次输入沙发床的重量、长、宽、高
输出
依次输出沙发床的重量、长、宽、高
样例输入
200 1.8 1.5 1.2
样例输出
weight:200
length:1.8
width:1.5
height:1.2
提示
改错思路有多种,只要程序能运行出正确结果,怎样改错都可以
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
using namespace std;
//家具类Furniture
class Furniture
{
public:
Furniture(double w)
{
weight=w;
}
void display()
{
cout<<"weight:"<<weight<<endl;
}
protected:
double weight; //家具重量
};
//床类Bed
class Bed: public Furniture
{
public:
Bed(double we,double l,double wi):Furniture(we),length(l),width(wi) {}
void display()
{
cout<<"length:"<<length<<endl;
cout<<"width:"<<width<<endl;
}
protected:
double length; //床的长
double width; //床的宽
};
//沙发类Sofa
class Sofa: public Furniture
{
public:
Sofa(double w,double h):Furniture(w),height(h) {}
void display()
{
cout<<"height:"<<height<<endl;
}
protected:
double height; //沙发的高度
};
//沙发床
class SleeperSofa:public Bed, public Sofa,public Furniture
{
public:
SleeperSofa(double we,double l,double wi,double h):Bed(we,l,wi),Sofa(we,h),Furniture(we) { }
void display()
{
cout<<"weight:"<<weight<<endl;
Bed::display();
Sofa::display();
}
protected:
double weight=200;
};
int main()
{
double weight,length,width,height;
cin>>weight>>length>>width>>height;
SleeperSofa ss(weight,length,width,height);
ss.display();
return 0;
}
#include <iostream>
using namespace std;
//家具类Furniture
class Furniture
{
public:
Furniture(double w)
{
weight=w;
}
void display()
{
cout<<"weight:"<<weight<<endl;
}
protected:
double weight; //家具重量
};
//床类Bed
class Bed: public Furniture
{
public:
Bed(double we,double l,double wi):Furniture(we),length(l),width(wi) {}
void display()
{
cout<<"length:"<<length<<endl;
cout<<"width:"<<width<<endl;
}
protected:
double length; //床的长
double width; //床的宽
};
//沙发类Sofa
class Sofa: public Furniture
{
public:
Sofa(double w,double h):Furniture(w),height(h) {}
void display()
{
cout<<"height:"<<height<<endl;
}
protected:
double height; //沙发的高度
};
//沙发床
class SleeperSofa:public Bed, public Sofa,public Furniture
{
public:
SleeperSofa(double we,double l,double wi,double h):Bed(we,l,wi),Sofa(we,h),Furniture(we) { }
void display()
{
cout<<"weight:"<<weight<<endl;
Bed::display();
Sofa::display();
}
protected:
double weight=200;
};
int main()
{
double weight,length,width,height;
cin>>weight>>length>>width>>height;
SleeperSofa ss(weight,length,width,height);
ss.display();
return 0;
}
YTU 2622: B 虚拟继承(虚基类)-沙发床(改错题)的更多相关文章
- C/C++ 多继承{虚基类,虚继承,构造顺序,析构顺序}
C/C++:一个基类继承和多个基类继承的区别 1.对多个基类继承会出现类之间嵌套时出现的同名问题,如果同名变量或者函数出现不在同一层次,则底层派生隐藏外层比如继承基类的同名变量和函数,不会出现二义性, ...
- C++ 由虚基类 虚继承 虚函数 到 虚函数表
//虚基类:一个类可以在一个类族中既被用作虚基类,也被用作非虚基类. class Base1{ public: Base1(){cout<<"Construct Base1!&q ...
- 【C++】继承(虚基类)
类的继承与派生 面向对象技术强调软件的可重用性,这种重用性通过继承机制来实现.而在类的继承过程中,被重用的原有类称为基类,新创建的类称为派生类.派生类定义语法格式如下: class <派生类名& ...
- C++ (P160—)多继承 二义性 虚基类 “向上转型”
1 多继承中,必须给每个基类指定一种派生类型,如果缺省,相应的基类则取私有派生类型,而不是和前一个基类取相同的派生类型 2 一个类的保护成员只能被本类的成员函数或者它的派生类成员函数访问 3 由于c+ ...
- C++学习之路—继承与派生(三):多重继承与虚基类
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 多重继承是指一个派生类有两个或多个基类.例如,有 ...
- C++ 多继承与虚基类
转载来自:CSDN insistGoGo (http://blog.csdn.net/insistgogo) 多继承的定义:派生类的基类大于一个 语法: class 派生类名:继承方式1 基类名1 ...
- C#虚基类继承与接口的区别
类:定义新的数据类型以及这些新的数据类型进行相互操作的方法 定义方式: class Cat { } class Cat:object { } C#中所有的类都是默认由object类派生来的,显示指定或 ...
- C++ 类的继承六(多继承的二义性--虚基类)
//多继承的二义性--虚基类(了解为主) #include<iostream> using namespace std; /* 多继承在现在的项目开发中一般不使用,他会增加项目的复杂度 * ...
- C++ 虚基类 派生与继承
在学习设计模式时我就有一个疑问,关联和继承除了用法上的区别,好像在内存上并没有什么区别,继承也是父类作为了子类的元素(内存上),关联也是这样.而且关联好像更占内存一些.这就是设计模式里问题了“依赖倒转 ...
随机推荐
- 动态规划----最长公共子序列(C++实现)
最长公共子序列 题目描述:给定两个字符串s1 s2 … sn和t1 t2 … tm .求出这两个字符串的最长公共子序列的长度.字符串s1 s2 … sn的子序列指可以表示为 … { i1 < i ...
- 【2019 1月集训 Day1】回文的后缀
题意: 给定 n,s,求有多少个字符集大小为 s ,长度为 n 的字符串,使得其不存在一个长度大于 1 的回文后缀. 答案对 m 取模. 分析: 考场见到计数题的链式反应,想写暴力—>暴力难写— ...
- Linux命令整理(2018/9/9-2018/9/15)
根据本周的Linux学习进度,整理了部分Linux知识及常用命令,待完善…… 1.显示默认启动方式(默认启动目标): systemctl get-default 2.设置默认启动方式(默认启动目标): ...
- linux netstat-查看Linux中网络系统状态信息
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. 语法 netstat(选项) ...
- 学习javascript中的事件——事件处理程序
事件就是用户或浏览器自身执行的某种动作.诸如 click.load 和 mouseover ,都是事件的名字.而响应某个事件的函数就叫做事件处理程序(或事件侦听器).事件处理程序的名字以“on”开头, ...
- Java Class 利用classpath来获取源文件地址
利用classpath来获取源文件地址 @author ixenos 应用场景 Properties props = new Properties(); /** * . 代表java命令运行的目录 * ...
- [K/3Cloud] 动态表单打开时传递一个自定义参数并在插件中获取
插件中在调用动态表单时,通过DynamicFormShowParameter的CustomParams,增加自定义的参数. /// <summary> /// 库存查询 /// </ ...
- codevs2597 团伙
题目描述 Description 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友 ...
- JavaScript面向对象实现
JavaScript面向对象实现 一:面向对象三大特征 继承,封装,多态! 二:JavaScript自定义对象 创建对象的方式: 方式1,对象初始化器方式: <script type=&quo ...
- springboot 关于第三方包 打包问题
第三方包: 添加library 依赖 在pom.xml中配置 <resources> <resource> <directory>lib</directory ...