學習記錄--C++繼承與派生程式設計題
阿新 • • 發佈:2020-12-27
1.設計一個圓類circle和一個桌子類table,另設計一個圓桌類roundtable,它是從前兩個類派生出來的
要求輸出一個圓桌的高度,面積與顏色等。
#include<iostream> #include<string> using namespace std; #define PI 3.14; class circle { public: circle() { //預設建構函式 } void setR(double r) { m_r = r; } double getR() {return m_r; } double getArea() { double area; area = m_r *m_r * PI; return area; } protected: double m_r;//圓的半徑 }; class table { public: table() { //預設建構函式 } void setHigh(double high) { m_high = high; } double getHigh() {return m_high; } protected: double m_high;//桌子的高度 }; class roundtable : public circle, public table { public: roundtable() { //建構函式 } void setC(string color) { m_color = color; } string getC() { return m_color; } private: string m_color;//圓桌的顏色 }; int main() { roundtable r1; r1.setR(2.0); r1.setHigh(1.3); r1.setC("紫色"); cout << "圓桌的高為:" << r1.getHigh() << "米" << endl; cout << "圓桌的面積為:" << r1.getArea() << "平方米" << endl; cout << "圓桌的顏色為:" << r1.getC() << endl; system("pause"); return 0; }
2.定義描述矩形的類~~~~(題目略)
#include<iostream> #include<string> using namespace std; class Rectangle { public: Rectangle() {} int CalArea() { int area; area = m_Length * m_Width; return area; } void setLength(int l) { m_Length = l; } int getLenght() { return m_Length; } void setWidth(int w) { m_Width = w; } int getWidth() { return m_Width; } protected: int m_Length; int m_Width; }; class Cuboid :public Rectangle { public: Cuboid(int l, int w, int h) { m_Length = l; m_Width = w; m_Chigh = h; } int CalVol() { m_Volume = m_Length * m_Chigh * m_Width; return m_Volume; } void Show() { cout << "長方體的長為:" << m_Length << endl; cout << "長方體的寬為:" << m_Width << endl; cout << "長方體的高為:" << m_Chigh << endl; cout << "長方體的體積為:" << CalVol() << endl; } private: int m_Chigh; int m_Volume; }; int main() { Cuboid c1(10,20,30); c1.Show(); system("pause"); return 0; }
執行截圖
3.定義一個車基類(題目略)
#include<iostream> #include<string> using namespace std; class Vehicle { public: Vehicle(int ms, int mw) { m_MaxSpeed = ms; m_Weight = mw; } int getMaxSpeed() { return m_MaxSpeed; } int getWeight() { return m_Weight; } void Run() { cout << "車的最大速度為:" << m_MaxSpeed << endl; cout << "車的重量為:" << m_Weight << endl; } void Stop() { cout << "車輛停下" << endl; } ~Vehicle(){} protected: int m_MaxSpeed; int m_Weight; }; //虛基類的virtual可以寫在public前面,如 //virtual public Vehicle //也可以寫在public後面 class bicycle :public virtual Vehicle { public: bicycle(int ms, int mw, int h) :Vehicle(ms, mw) { m_height = h; } int getHeight() { return m_height; } void Run() { cout << "自行車的高為:" << m_height << endl; } void Stop() { cout << "自行車已停下" << endl; } ~bicycle(){} private: int m_height; }; class Motorcar :public virtual Vehicle { public: Motorcar(int ms, int mw, int sn) :Vehicle(ms, mw) { m_SetNum = sn; } int getSetNum() { return m_SetNum; } void Run() { cout << "汽車的座位數為:" << m_SetNum << endl; } void Stop() { cout << "汽車已停下" << endl; } ~Motorcar() {} private: int m_SetNum; }; class motorcycle :public bicycle, public Motorcar { public: motorcycle(int ms, int mw, int h, int sn) :Vehicle(ms, mw), bicycle(ms, mw, h), Motorcar(ms, mw, sn) { } void Run() { cout << "這是motorcycle" << endl; cout << "速度為:" << getMaxSpeed() << endl; cout << "高度為:" << getHeight() << endl; cout << "重量為為:" << getWeight() << endl; cout << "座位數為:" << getSetNum() << endl; } void Stop() { cout << "motorcycle stopped" << endl; } ~motorcycle(){} }; int main() { Vehicle v1(10, 20); v1.Run(); v1.Stop(); }