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(); }
原文:https://www.cnblogs.com/army613bts/p/14196709.html