实验报告

  • 题目1
  • 题目2

【实验名称】 实验八 类的继承(2)
【实验内容】

题目1

正确使用类的继承和组合进行类的设计,分别表示房间、休息室、教室、投影仪,沙发,为每个类设置适当的成员变量、成员函数和构造函数,在主程序中生成对象进行测试。

源代码:
Room.cpp:

#include<iostream>
using namespace std;
enum Color{ red,orange,yellow,green,cyan,blue,purple,white,black };/**房间类*/
class Room{public:Room(){}Room(string name , double length , double width , double height , enum Color color);void showRoom();  ///显示房间成员变量函数的声明double decorateRoom();   ///房间装修函数的声明string name;  /**名字*/double length; /**长度*/double width;  /**宽度*/double height; /**高度*/enum Color color;  /**颜色*/
};///Room类带参数的构造函数Room::Room(string name , double length , double width , double height , enum Color color){this->name = name;this->length = length;this->width = width;this->height = height;this->color = color;}///房间装修函数的实现double Room::decorateRoom(){cout<<"进行"<<name<<"刷墙漆装修:"<<endl;double paintPrice;  ///油漆价格double area;  ///房间四周面积area = 2* length * height + 2* width * height + length * width;cout<<"输出长宽乘积:"<<length*width<<endl;paintPrice = area * 20;cout<<name<<"四周的面积为:"<<area<<"平方米,最后刷油漆所花的钱为:"<<paintPrice<<endl;return paintPrice;}///显示房间成员变量函数的实现void Room::showRoom(){cout<<name<<"的长度是:"<<length<<"米"<<endl;cout<<name<<"的宽度是:"<<width<<"米"<<endl;cout<<name<<"的高度是:"<<height<<"米"<<endl;cout<<name<<"的颜色是:";switch(color){case 0: cout<<"红色"<<endl; break;case 1: cout<<"橙色"<<endl; break;case 2: cout<<"黄色"<<endl; break;case 3: cout<<"绿色"<<endl; break;case 4: cout<<"青色"<<endl; break;case 5: cout<<"蓝色"<<endl; break;case 6: cout<<"紫色"<<endl; break;case 7: cout<<"白色"<<endl; break;case 8: cout<<"黑色"<<endl; break;default:cout<<"输入的颜色不正确"<<endl; break;}
}

Projector.cpp:

#include<iostream>
using namespace std;
/**投影仪类*/
class Projector{public:Projector(){}Projector(double projectorPrice , string brand){this->projectorPrice = projectorPrice;this->brand = brand;}void showProjector(){cout<<"投影仪的价格是:"<<projectorPrice<<endl;cout<<"投影仪的品牌是:"<<brand<<"元"<<endl;}double projectorPrice; /**投影仪价格*/string brand;  /**品牌*/
};

Classroom.cpp:

#include<iostream>using namespace std;/**教室类*/
class Classroom:public Room{public:Classroom(){}Classroom(string name , double length , double width , double height , enum Color color , Projector projector):Room(name,length,width,height,color){this->projector.projectorPrice = projector.projectorPrice;this->projector.brand = projector.brand;}void decorateClassroom();  ///装修教室void showClassroom();  ///显示教室信息Projector projector;  ///投影仪
};///装修教室void Classroom::decorateClassroom(){double paintPrice = decorateRoom();cout<<"投影仪的价格是:"<<projector.projectorPrice<<"元"<<endl;cout<<name<<"装修的总价格是:"<<paintPrice + projector.projectorPrice<<"元"<<endl;}///显示教室信息void Classroom::showClassroom(){showRoom();projector.showProjector();}

Sofa.cpp:

#include<iostream>
#ifndef Sofa_cpp
#define Sofa_cpp
using namespace std;enum Color2{ red2,orange2,yellow2,green2,cyan2,blue2,purple2,white2,black2 };/**沙发类*/
class Sofa{public:Sofa(){}Sofa(double length , double width , double height , double sofaPrice , enum Color2 color  , string brand , string texture){this->length = length;this->width = width;this->height = height;this->sofaPrice = sofaPrice;this->color = color;this->brand = brand;this->texture = texture;}void showSofa(){    ///显示沙发的成员变量信息cout<<"沙发的长度是:"<<length<<"米"<<endl;cout<<"沙发的宽度是:"<<width<<"米"<<endl;cout<<"沙发的长度是:"<<height<<"米"<<endl;cout<<"沙发的价格是:"<<sofaPrice<<"元"<<endl;cout<<"沙发的颜色是:";switch(color){case 0: cout<<"红色"<<endl; break;case 1: cout<<"橙色"<<endl; break;case 2: cout<<"黄色"<<endl; break;case 3: cout<<"绿色"<<endl; break;case 4: cout<<"青色"<<endl; break;case 5: cout<<"蓝色"<<endl; break;case 6: cout<<"紫色"<<endl; break;case 7: cout<<"白色"<<endl; break;case 8: cout<<"黑色"<<endl; break;default:cout<<"输入的颜色不正确"<<endl; break;}cout<<"沙发的品牌是:"<<brand<<endl;cout<<"沙发的材质是:"<<texture<<endl;}double length; /**长度*/double width;  /**宽度*/double height; /**高度*/double sofaPrice; ///沙发价格enum Color2 color;  /**颜色*/string brand;  /**品牌*/string texture; /**材质*/
};#endif // Sofa_cpp

Lounge.cpp:

#include<iostream>
#include"Sofa.cpp"
#include"Room.cpp"
using namespace std;/**休息室类*/
class Lounge:public Room{public:Lounge(){}Lounge(string name , double length , double width , double height , enum Color color , Sofa &s):Room(name,length,width,height,color){sofa.length = s.length;sofa.width = s.width;sofa.height = s.height;sofa.sofaPrice = s.sofaPrice;sofa.color = s.color;sofa.brand = s.brand;sofa.texture = s.texture;}void decorateLounge(){double paintPrice = decorateRoom();cout<<"沙发的价格是:"<<sofa.sofaPrice<<"元"<<endl;cout<<name<<"装修的总价格是:"<<paintPrice + sofa.sofaPrice<<"元"<<endl;}void showLounge(){  ///显示休息室的成员变量信息showRoom();sofa.showSofa();}Sofa sofa;
};

Main.cpp:

#include<iostream>
#include"Lounge.cpp"
#include"Sofa.cpp"
#include"Projector.cpp"
#include"Classroom.cpp"
using namespace std;int main(){Projector pro(/**价格*/2000,"索尼");Sofa s(/**长*/ 2 , /**宽*/ 0.5 ,/**高*/ 0.4 , /**价格*/ 1000 ,/**颜色*/ red2 ,/**品牌*/ "全友" ,/**材质*/ "真皮");Classroom classroom("教室",/**长*/ 8 ,/**宽*/ 6.5,/**高*/ 3,/**颜色*/ white ,/**投影仪*/ pro);Lounge lounge("休息室",/**长*/ 6.5,/**宽*/ 5.5,/**高*/ 2.5,/**紫色*/ purple,/**沙发*/ s);classroom.showClassroom();classroom.decorateClassroom();cout<<endl;lounge.showLounge();lounge.decorateLounge();return 0;
}

【实验结果】

题目2

正确使用类的继承进行类的设计,分别表示家具、沙发、床、沙发床,为每个类设置适当的成员变量、成员函数和构造函数,正确使用虚基类,在主程序中生成对象进行测试。

Furniture.cpp

#ifndef Furniture_
#define Furniture_#include <iostream>using namespace std;class Furniture{public:Furniture(){}Furniture(float length,float width,float height,string type,int price,string texture):length(length),width(width),height(height),price(price),type(type),texture(texture){}/**Furniture1(float length,float width,float height,string type,int price,string texture){this->length=length;this->width=width;this->height=height;this->price=price;this->type=type;this->texture=texture;}*/string getType(){ return type; }float getLength(){ return length; }float getWidth(){ return width; }float getHeight(){ return height; }int getPrice(){ return price; }string getTexture(){ return texture; }void virtual showFurniture()  ///显示家具属性{cout<<type<<"的长度是"<<length<<"厘米"<<endl;cout<<type<<"的宽度是"<<width<<"厘米"<<endl;cout<<type<<"的高度是"<<height<<"厘米"<<endl;cout<<type<<"的价格是"<<length<<"元"<<endl;cout<<type<<"的材质是"<<texture<<endl;}float length;  ///长度float width;   ///宽度float height;  ///高度string type; ///类型int price;   ///价格string texture;  ///材质
};#endif // Furniture_

Sofa.cpp

#ifndef Sofa_
#define Sofa_#include <iostream>
#include"Furniture.cpp"
using namespace std;class Sofa:virtual public Furniture{public:Sofa(){}Sofa(string seat,float length,float width,float height,string type,int price,string texture):Furniture(length,width,height,type,price,texture),seat(seat){}string getSeat(){return seat;}///显示沙发属性void showSofa(){showFurniture();cout<<type<<"能用来"<<seat<<endl;}string seat;
};#endif // Sofa_

Bed.cpp

#ifndef Bed_
#define Bed_
#include <iostream>
#include<windows.h>
#include"Furniture.cpp"
using namespace std;class Bed:virtual public Furniture{public:Bed(){}Bed(string sleep,float length,float width,float height,string type,int price,string texture):Furniture(length,width,height,type,price,texture),sleep(sleep){}string getsleep(){ return sleep; }///显示床的属性void showBed(){showFurniture();cout<<type<<"能用来"<<sleep<<endl;}///床的睡觉功能void beginSleep(){  //睡觉cout <<"床能用来睡觉"<<endl;int sleepTime = rand()%4;cout<<"随机产生的睡觉时间:"<<sleepTime<<"(小时)"<<endl;Sleep(sleepTime * 1000);cout << sleepTime <<"小时过后,睡醒了" <<endl;}string sleep;
};#endif // Bed_

Sofa.cpp

#include <iostream>
#include"Sofa.cpp"
#include"Bed.cpp"
#include"Furniture.cpp"
using namespace std;class SofaBed:public Sofa,public Bed{public:SofaBed(string seat,string sleep,float length,float width,float height,string type,int price,string texture):Furniture(length,width,height,type,price,texture),Sofa(seat,length,width,height,type,price,texture),Bed(sleep,length,width,height,type,price,texture){this->sleep=sleep;this->seat=seat;this->length=length;this->width=width;this->height=height;this->type=type;this->price=price;this->texture=texture;}///显示沙发床属性void showSofaBed(){showFurniture();cout<<type<<"不仅能用来"<<seat<<"睡,还能用来"<<sleep<<endl;}
};

main.cpp

#include <iostream>
#include"Sofa.cpp"
#include"Bed.cpp"
#include"SofaBed.cpp"
using namespace std;int main()
{Sofa sofa(/**沙发的功能*/ "坐",/**长*/ 1.8,/**宽*/ 0.8,/**高*/ 0.5,/**类型*/ "沙发",/**价格*/ 1500,/**材质*/ "牛皮");sofa.showSofa();   ///显示沙发属性Bed bed(/**床的功能*/ "睡",/**长*/ 2.2,/**宽*/ 1.4,/**高*/ 0.7,/**类型*/ "床",/**价格*/ 2000,/**材质*/ "席梦思弹簧");bed.showBed();    ///显示床的属性SofaBed sofaBed("坐","睡",/**长*/ 2.5,/**宽*/ 1.7,/**高*/ 0.6,/**类型*/"沙发床",/**价格*/ 3000,/**材质*/ "海绵");sofaBed.showSofaBed();   ///显示沙发床属性sofaBed.beginSleep();    ///床的睡觉功能return 0;
}

【实验结果】

【小结或讨论】
本次实验是实验八 类的继承(2),主要目的是正确使用类的继承和组合进行类的设计,依旧采用的是多文件结构,
第一题正确使用类的继承和组合进行类的设计,分别表示房间Room.cpp、休息室Lounge.cpp、教室Classroom.cpp、投影仪Projector.cpp,沙发Sofa.cpp,为房间类设计的属性有长、宽、高、名字、颜色等,其中颜色用枚举表Color声明的变量来指定房间的颜色,之后主要的功能函数就是为房间进行装修:包括给墙的四周刷墙漆、购买投影仪和沙发等。
第二题,正确使用类的继承进行类的设计,分别表示家具Furniture.cpp、沙发Sofa.cpp、床Bed.cpp、沙发床SofaBed.cpp,四个类的设计和代码编写倒是没有什么困难,但是在声明沙发床的构造函数的时候,CodeBlocks报了error: no matching function for call to 'Sofa::Sofa()'的错,开始一直百思不得其解:编译器为什么一定要我去调用父类的构造函数,这不是必须的呀,后来只能在SofaBed()函数里调用了其他的三个构造函数,编译器就没有报错了。然后我在把书重新看了一遍,猛然发现我犯了书上说的极端的错:父类声明了带形参的构造函数,而且没有声明默认的不带形参的构造函数。才恍然大悟为什么编译器会报要我手动调用父类构造函数的错了,平常的编码习惯都会先加上类默认的构造函数的,但今天不知道的就怎么忘记了,看来养成良好的编码习惯,写出符合规范的代码还需要多多加油。

C++实验八——类的继承(2)相关推荐

  1. 实验三 类的继承和多态性

    实验三 类的继承和多态性 1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积.int getCircumference():获得图形的周长 ( ...

  2. C++实验七——类的继承(1)

    实验报告 题目1 题目2 [实验名称] 实验七 类的继承(1) [实验内容] 题目1 以动物类为父类进行派生,设计可行的派生类,为派生类增加必要的成员,并对父类中的成员做适当调整,在主程序中对派生类的 ...

  3. java实验33 类的继承2_java实验2 类的继承性

    实验2 类的继承性 一.实验目的 掌握面向对象的继承性在Java中的实现方法:掌握super关键字的用法,体会抽象类和抽象方法的作用. 二.实验内容 1.程序理解: 1)类的继承 2)第4章课后编程第 ...

  4. java类接口实验_实验3_Java类的继承和接口的定义和使用

    本次有三题:学会Java类的继承.接口的定义和使用 // Ex3_1.java /** * 题目要求: * 修改例5.7(P95)实现在页面中拖动鼠标画出矩形,矩形的对角线为点击并拖动鼠标形成的直线线 ...

  5. 实验5 类的继承、派生和多态(2)

    1.设计并实现一个机器宠物类MachinePets. #include<iostream> #include<string> using namespace std; clas ...

  6. java人学生大学生类的继承,java实验报告7.doc

    java实验报告7.doc 实 验 报 告( 2014 / 2015学年 第2学期)课程名称JAVA程序设计实验名称 类的继承实验时间2015年4月30日指导单位计算机学院/软件学院软件工程系指导教师 ...

  7. 实验八 接口与实现接口的类

    实验八 接口与实现接口的类 一.程序代码 public class yuanzhui extends Rectangle implements Area,Volume { private double ...

  8. 实验四 类和对象;类的继承和派生;多态性; 接口;构造器应用

    实验四 类和对象:类的继承和派生:多态性: 接口:构造器应用 一.实验目的 1. 掌握类与对象的关系: 2. 掌握类的定义: 3. 掌握对象的声明及使用: 4. 掌握构造方法的概念及调用时机: 5. ...

  9. java实验八 内部类,匿名类

    实验八 内部类,匿名类 实验内容: (一)内部类.匿名类(必做) 1.三大体育媒体互联网平台,由于资源独占,有些类别的运动只能在某个平台上进行独播: 2.足球赛事只能在平台A上进行独播,篮球赛事只能在 ...

最新文章

  1. 【JavaScript】AJAX教程
  2. -【Java FTP及FTP服务器搭建】
  3. 执行cmd并获得结果_MySQL 服务无法启动 请键入 NET HELPMSG 3523 以获得更多的帮助...
  4. java创建文件和目录
  5. DHCP Client 无法启动 拒绝访问
  6. PHP条件语句总结,PHP 条件语句基本语法结构
  7. 杭电1437 天气情况
  8. 数字图像处理(三) 图像的变换
  9. mkcert在windows系统上制作SSL证书
  10. c语言城市交通灯优化,城市智能交通灯系统(本科)毕业论文.doc
  11. 基于PHP的SQL注入防御
  12. php screw.so,php_screw
  13. python you-get 下载视频
  14. Quality-Estimation0 (翻译质量评价-使用 BERT 特征训练 QE 模型)
  15. 解决:Unknown column ‘字段名‘ in ‘field list‘报错
  16. 关于浏览器兼容性的问题
  17. hi3559av100的启动和升级
  18. FPGA 之 SOPC 系列(四)NIOS II 外围设备--标准系统搭建
  19. 《标题党》自我修炼的10个秘籍
  20. springCloud运行主类,错误: 找不到或无法加载主类 com.kuang.springcloud.DeptConsumer_80

热门文章

  1. 小学计算机教案2018六年级,2017年小学六年级下册信息技术教学计划
  2. 方面级情感分析论文泛读02:Syntax-Aware Aspect-Level Sentiment Classification with Proximity-Weighted Convolution
  3. python scipy.optimize 非线性规划 求解局部最优和全局最优
  4. php 生成300dpi图片,canvas生成图片只有96dpi,打印需要300dpi, 请问如何修改这个信息....
  5. C# 中DataGridView 表头设置
  6. 码率,帧率,分辨率_详解
  7. 004 鸿蒙应用开发-通知栏
  8. calamari项目结构解析
  9. 汇编语言笔记-keil5软件仿真及调试
  10. java浮点数计算程序_计算器程序.可以准确进行浮点数运算