C++实验八——类的继承(2)
實驗報告
- 題目1
- 題目2
【實驗名稱】 實驗八 類的繼承(2)
【實驗內容】
題目1
正確使用類的繼承和組合進行類的設計,分別表示房間、休息室、教室、投影儀,沙發,為每個類設置適當的成員變量、成員函數和構造函數,在主程序中生成對象進行測試。
源代碼:
Room.cpp:
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_cppLounge.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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python数据可视化之随机点图
- 下一篇: 分享一个通过网络链接PDF转JPG的公用