【实验名称】 实验八 类的继承(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()函数里调用了其他的三个构造函数,编译器就没有报错了。然后我在把书重新看了一遍,猛然发现我犯了书上说的极端的错:父类声明了带形参的构造函数,而且没有声明默认的不带形参的构造函数。才恍然大悟为什么编译器会报要我手动调用父类构造函数的错了,平常的编码习惯都会先加上类默认的构造函数的,但今天不知道的就怎么忘记了,看来养成良好的编码习惯,写出符合规范的代码还需要多多加油。