题目一
【问题描述】
利用给定的矩形类,编写长方体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Rect { public: Rect(float l, float w); float getArea(); protected: float mLength; float mWidth;
};
#include<iostream> using namespace std; int main() { int j, k, l; cout << "请输入矩形的长和宽:"; cin >> j >> k; Rect r1(j, k); cout <<"矩形的面积为:"<< r1.getArea() << endl; cout << "请输入立方体的高:"; cin >> l; cubiod c1(j,k,l); cout <<"立方体体积为:"<< c1.getVol() << endl; cout <<"立方体表面积为:"<< c1.Area(); return 0;
}
|
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| class Rect { public: Rect(float l, float w); float getArea(); protected: float mLength; float mWidth;
};
class cubiod { public: cubiod(float p, float o,float q ); float getVol(); float Area(); protected: float mLength; float mWidth; float mHeight; }; Rect::Rect(float l, float w) { mLength=l; mWidth=w; } float Rect::getArea(){ return mLength*mWidth; } cubiod::cubiod(float p, float o,float q){ mLength=p; mWidth=o; mHeight=q; } float cubiod::getVol(){ return mLength*mWidth*mHeight; } float cubiod::Area(){ return 2*mLength*mWidth+2*mWidth*mHeight+2*mLength*mHeight; }
#include<iostream> using namespace std; int main() { int j, k, l; cout << "请输入矩形的长和宽:"; cin >> j >> k; Rect r1(j, k); cout <<"矩形的面积为:"<< r1.getArea() << endl; cout << "请输入立方体的高:"; cin >> l; cubiod c1(j,k,l); cout <<"立方体体积为:"<< c1.getVol() << endl; cout <<"立方体表面积为:"<< c1.Area(); return 0;
}
|
题目二
【问题描述】
由圆构造圆柱
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| PI=3.14
#include<iostream> using namespace std; class Circle { public: Circle(void); Circle(float r); float getArea(); float getGirth(); static const float PI ; protected: float mR; };
#include<iostream> using namespace std; int main() { float t, m; cout << "请输入圆的半径:"; cin >> t; Circle c1(t); cout <<"圆的面积为:"<< c1.getArea() << endl; cout << "请输入圆柱的高:"; cin >> m; Cylinder c2(t, m); cout <<"圆柱的表面积为:"<< c2.getArea() << endl; cout <<"圆柱的体积为:"<< c2.getVol() << endl; }
|
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| PI=3.14
#include<iostream> using namespace std; class Circle { public: Circle(void); Circle(float r); float getArea(); float getGirth(); static const float PI ; protected: float mR; };
const float Circle::PI=3.14; Circle::Circle(void) { mR=0; } Circle::Circle(float r):mR(r) { } float Circle::getArea() { return PI*mR*mR; } float Circle::getGirth() { return 2*PI*mR; }
class Cylinder:public Circle { public: Cylinder(float t,float m):Circle(t) { mH=m; } float getArea() { return Circle::getGirth()*mH+2*Circle::getArea(); } float getVol() { return Circle::getArea()*mH; } private: float mH; };
#include<iostream> using namespace std; int main() { float t, m; cout << "请输入圆的半径:"; cin >> t; Circle c1(t); cout <<"圆的面积为:"<< c1.getArea() << endl; cout << "请输入圆柱的高:"; cin >> m; Cylinder c2(t, m); cout <<"圆柱的表面积为:"<< c2.getArea() << endl; cout <<"圆柱的体积为:"<< c2.getVol() << endl; }
|
题目三
【问题描述】
按以下提示信息,由基类的设计和测试开始,逐渐地完成各个类的设计,并且完成要求的功能。
①设计一个Point(点)类,包含数据成员x、y(坐标点)。
②以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径)。
③以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。
请设计出各类中基本的成员函数,包括构造函数、设置数据成员和获取数据成员的函数,以及计算圆的周长和面积、计算圆柱体的表面积和体积的函数,使程序正确运行。
pi=3.1415926
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int main() { double x,y; cin>>x>>y; Point p1(x,y); p1.SetPoint(x+10,y+5); cout<<fixed<<setprecision(1); p1.Show(); Circle c1(p1.GetX(),p1.GetY(),20); c1.Show(); cout<<"c1的周长是:"<<c1.Girth()<<endl; cout<<"c1的面积是:"<<c1.Area()<<endl; Cylinder cy1; cy1.SetCylinder(c1,10.5); cy1.Show(); cout<<"cy1的表面积是:"<<cy1.Area()<<endl; cout<<"cy1的体积是:"<<cy1.Volumn()<<endl; return 0; }
|
【代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| #include <iostream> #include <iomanip> using namespace std; const double pi=3.1415926; class Point { public: Point (){}; Point (double a,double b); ~Point(); void SetPoint(double a,double b); void Show(); double GetX(); double GetY(); protected: double x,y; }; Point::~Point() {} Point::Point (double a,double b) { x=a; y=b; } void Point::SetPoint(double a,double b) { x=a; y=b; } void Point::Show(){ cout<<x<<","<<y<<endl; }
double Point::GetX(){ return x; } double Point::GetY(){ return y; } class Circle: public Point { public : Circle(){}; Circle(double a,double b,double r); ~Circle(); void Show(); double Area() {return pi*R*R;} double Girth() {return pi*R*2;} protected: double R; double c; double d; }; Circle::~Circle(){}
Circle::Circle(double a,double b,double r) { c=a; d=b; R=r; } void Circle::Show(){ cout<<c<<","<<d<<endl; cout<<R<<endl; } class Cylinder:public Circle { public: Cylinder(){}; Cylinder (double x,double y,double r,double h); void SetCylinder(Circle a,double h); double Area() ; double Volumn(); void Show(); protected: double height; double q; double w; double e; Circle m; }; void Cylinder::Show(){ m.Show(); cout<<endl<<height<<endl; } Cylinder::Cylinder(double a,double b,double r,double h) { q=a; w=b; e=r; height=h; } void Cylinder::SetCylinder(Circle a,double h) { height=h; m=a; } double Cylinder::Area() { return 2*m.Area()+m.Girth()*height; } double Cylinder::Volumn() { return m.Area()*height; }
int main() { double x,y; cin>>x>>y; Point p1(x,y); p1.SetPoint(x+10,y+5); cout<<fixed<<setprecision(1); p1.Show(); Circle c1(p1.GetX(),p1.GetY(),20); c1.Show(); cout<<"c1的周长是:"<<c1.Girth()<<endl; cout<<"c1的面积是:"<<c1.Area()<<endl; Cylinder cy1; cy1.SetCylinder(c1,10.5); cy1.Show(); cout<<"cy1的表面积是:"<<cy1.Area()<<endl; cout<<"cy1的体积是:"<<cy1.Volumn()<<endl; return 0; }
|
代码可能略有不足,请大佬多多指教