日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++实验八——类的继承(2)

發(fā)布時(shí)間:2024/3/13 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++实验八——类的继承(2) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

實(shí)驗(yàn)報(bào)告

  • 題目1
  • 題目2

【實(shí)驗(yàn)名稱】 實(shí)驗(yàn)八 類的繼承(2)
【實(shí)驗(yàn)內(nèi)容】

題目1

正確使用類的繼承和組合進(jìn)行類的設(shè)計(jì),分別表示房間、休息室、教室、投影儀,沙發(fā),為每個(gè)類設(shè)置適當(dāng)?shù)某蓡T變量、成員函數(shù)和構(gòu)造函數(shù),在主程序中生成對(duì)象進(jìn)行測(cè)試。

源代碼:
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(); ///顯示房間成員變量函數(shù)的聲明double decorateRoom(); ///房間裝修函數(shù)的聲明string name; /**名字*/double length; /**長(zhǎng)度*/double width; /**寬度*/double height; /**高度*/enum Color color; /**顏色*/ };///Room類帶參數(shù)的構(gòu)造函數(shù)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;}///房間裝修函數(shù)的實(shí)現(xiàn)double Room::decorateRoom(){cout<<"進(jìn)行"<<name<<"刷墻漆裝修:"<<endl;double paintPrice; ///油漆價(jià)格double area; ///房間四周面積area = 2* length * height + 2* width * height + length * width;cout<<"輸出長(zhǎng)寬乘積:"<<length*width<<endl;paintPrice = area * 20;cout<<name<<"四周的面積為:"<<area<<"平方米,最后刷油漆所花的錢為:"<<paintPrice<<endl;return paintPrice;}///顯示房間成員變量函數(shù)的實(shí)現(xiàn)void Room::showRoom(){cout<<name<<"的長(zhǎng)度是:"<<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<<"藍(lán)色"<<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<<"投影儀的價(jià)格是:"<<projectorPrice<<endl;cout<<"投影儀的品牌是:"<<brand<<"元"<<endl;}double projectorPrice; /**投影儀價(jià)格*/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<<"投影儀的價(jià)格是:"<<projector.projectorPrice<<"元"<<endl;cout<<name<<"裝修的總價(jià)格是:"<<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 };/**沙發(fā)類*/ 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(){ ///顯示沙發(fā)的成員變量信息cout<<"沙發(fā)的長(zhǎng)度是:"<<length<<"米"<<endl;cout<<"沙發(fā)的寬度是:"<<width<<"米"<<endl;cout<<"沙發(fā)的長(zhǎng)度是:"<<height<<"米"<<endl;cout<<"沙發(fā)的價(jià)格是:"<<sofaPrice<<"元"<<endl;cout<<"沙發(fā)的顏色是:";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<<"藍(lán)色"<<endl; break;case 6: cout<<"紫色"<<endl; break;case 7: cout<<"白色"<<endl; break;case 8: cout<<"黑色"<<endl; break;default:cout<<"輸入的顏色不正確"<<endl; break;}cout<<"沙發(fā)的品牌是:"<<brand<<endl;cout<<"沙發(fā)的材質(zhì)是:"<<texture<<endl;}double length; /**長(zhǎng)度*/double width; /**寬度*/double height; /**高度*/double sofaPrice; ///沙發(fā)價(jià)格enum Color2 color; /**顏色*/string brand; /**品牌*/string texture; /**材質(zhì)*/ };#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<<"沙發(fā)的價(jià)格是:"<<sofa.sofaPrice<<"元"<<endl;cout<<name<<"裝修的總價(jià)格是:"<<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(/**價(jià)格*/2000,"索尼");Sofa s(/**長(zhǎng)*/ 2 , /**寬*/ 0.5 ,/**高*/ 0.4 , /**價(jià)格*/ 1000 ,/**顏色*/ red2 ,/**品牌*/ "全友" ,/**材質(zhì)*/ "真皮");Classroom classroom("教室",/**長(zhǎng)*/ 8 ,/**寬*/ 6.5,/**高*/ 3,/**顏色*/ white ,/**投影儀*/ pro);Lounge lounge("休息室",/**長(zhǎng)*/ 6.5,/**寬*/ 5.5,/**高*/ 2.5,/**紫色*/ purple,/**沙發(fā)*/ s);classroom.showClassroom();classroom.decorateClassroom();cout<<endl;lounge.showLounge();lounge.decorateLounge();return 0; }

【實(shí)驗(yàn)結(jié)果】

題目2

正確使用類的繼承進(jìn)行類的設(shè)計(jì),分別表示家具、沙發(fā)、床、沙發(fā)床,為每個(gè)類設(shè)置適當(dāng)?shù)某蓡T變量、成員函數(shù)和構(gòu)造函數(shù),正確使用虛基類,在主程序中生成對(duì)象進(jìn)行測(cè)試。

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<<"的長(zhǎng)度是"<<length<<"厘米"<<endl;cout<<type<<"的寬度是"<<width<<"厘米"<<endl;cout<<type<<"的高度是"<<height<<"厘米"<<endl;cout<<type<<"的價(jià)格是"<<length<<"元"<<endl;cout<<type<<"的材質(zhì)是"<<texture<<endl;}float length; ///長(zhǎng)度float width; ///寬度float height; ///高度string type; ///類型int price; ///價(jià)格string texture; ///材質(zhì) };#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;}///顯示沙發(fā)屬性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<<"隨機(jī)產(chǎn)生的睡覺時(shí)間:"<<sleepTime<<"(小時(shí))"<<endl;Sleep(sleepTime * 1000);cout << sleepTime <<"小時(shí)過后,睡醒了" <<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;}///顯示沙發(fā)床屬性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(/**沙發(fā)的功能*/ "坐",/**長(zhǎng)*/ 1.8,/**寬*/ 0.8,/**高*/ 0.5,/**類型*/ "沙發(fā)",/**價(jià)格*/ 1500,/**材質(zhì)*/ "牛皮");sofa.showSofa(); ///顯示沙發(fā)屬性Bed bed(/**床的功能*/ "睡",/**長(zhǎng)*/ 2.2,/**寬*/ 1.4,/**高*/ 0.7,/**類型*/ "床",/**價(jià)格*/ 2000,/**材質(zhì)*/ "席夢(mèng)思彈簧");bed.showBed(); ///顯示床的屬性SofaBed sofaBed("坐","睡",/**長(zhǎng)*/ 2.5,/**寬*/ 1.7,/**高*/ 0.6,/**類型*/"沙發(fā)床",/**價(jià)格*/ 3000,/**材質(zhì)*/ "海綿");sofaBed.showSofaBed(); ///顯示沙發(fā)床屬性sofaBed.beginSleep(); ///床的睡覺功能return 0; }

【實(shí)驗(yàn)結(jié)果】

【小結(jié)或討論】
本次實(shí)驗(yàn)是實(shí)驗(yàn)八 類的繼承(2),主要目的是正確使用類的繼承和組合進(jìn)行類的設(shè)計(jì),依舊采用的是多文件結(jié)構(gòu),
第一題正確使用類的繼承和組合進(jìn)行類的設(shè)計(jì),分別表示房間Room.cpp、休息室Lounge.cpp、教室Classroom.cpp、投影儀Projector.cpp,沙發(fā)Sofa.cpp,為房間類設(shè)計(jì)的屬性有長(zhǎng)、寬、高、名字、顏色等,其中顏色用枚舉表Color聲明的變量來指定房間的顏色,之后主要的功能函數(shù)就是為房間進(jìn)行裝修:包括給墻的四周刷墻漆、購(gòu)買投影儀和沙發(fā)等。
第二題,正確使用類的繼承進(jìn)行類的設(shè)計(jì),分別表示家具Furniture.cpp、沙發(fā)Sofa.cpp、床Bed.cpp、沙發(fā)床SofaBed.cpp,四個(gè)類的設(shè)計(jì)和代碼編寫倒是沒有什么困難,但是在聲明沙發(fā)床的構(gòu)造函數(shù)的時(shí)候,CodeBlocks報(bào)了error: no matching function for call to 'Sofa::Sofa()'的錯(cuò),開始一直百思不得其解:編譯器為什么一定要我去調(diào)用父類的構(gòu)造函數(shù),這不是必須的呀,后來只能在SofaBed()函數(shù)里調(diào)用了其他的三個(gè)構(gòu)造函數(shù),編譯器就沒有報(bào)錯(cuò)了。然后我在把書重新看了一遍,猛然發(fā)現(xiàn)我犯了書上說的極端的錯(cuò):父類聲明了帶形參的構(gòu)造函數(shù),而且沒有聲明默認(rèn)的不帶形參的構(gòu)造函數(shù)。才恍然大悟?yàn)槭裁淳幾g器會(huì)報(bào)要我手動(dòng)調(diào)用父類構(gòu)造函數(shù)的錯(cuò)了,平常的編碼習(xí)慣都會(huì)先加上類默認(rèn)的構(gòu)造函數(shù)的,但今天不知道的就怎么忘記了,看來養(yǎng)成良好的編碼習(xí)慣,寫出符合規(guī)范的代碼還需要多多加油。

總結(jié)

以上是生活随笔為你收集整理的C++实验八——类的继承(2)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。