A. 圆和圆柱体计算(继承)
定义一个CPoint点类,包含数据成员x,y(坐标点)。
以CPoint为基类,派生出一个圆形类CCircle,增加数据成员r(半径)和一个计算圆面积的成员函数。
再以CCircle做为直接基类,派生出一个圆柱体类CCylinder,增加数据成员h(高)和一个计算体积的成员函数。
生成圆和圆柱体对象,调用成员函数计算面积或体积并输出结果。
输入
输入圆的圆心位置、半径
输入圆柱体圆心位置、半径、高
输出
输出圆的圆心位置 半径
输出圆面积
输出圆柱体的圆心位置 半径 高
输出圆柱体体积
输入样例1
0 0 1
1 1 2 3输出样例1
Circle:(0,0),1
Area:3.14
Cylinder:(1,1),2,3
Volume:37.68
#include<iostream> #include <cstring> using namespace std; class Cpoint { protected: int x, y; public: Cpoint(int xx, int yy) :x(xx), y(yy) { } }; class Ccircle :public Cpoint { protected: int r; public: float Area() { return 3.14 * r * r; } //注意子类构造语法 Ccircle(int xx, int yy, int rr) :Cpoint(xx, yy), r(rr) { } void print() { cout << "Circle:(" << x << "," << y << ")," << r << endl; cout << "Area:" << Area() << endl; } }; class CCylinder :public Ccircle { protected: int h; public: float volume() { return 3.14 * r * r * h; } CCylinder(int xx, int yy, int rr, int hh) :Ccircle(xx, yy, rr), h(hh) { } void print() { cout << "Cylinder:(" << x << "," << y << ")," << r << "," << h << endl; cout << "Volume:" << volume() << endl; } }; int main() { int x, y, r, h; cin >> x >> y >> r; Ccircle c1(x, y, r); c1.print(); cin >> x >> y >> r >> h; CCylinder c2(x, y, r, h); c2.print(); }
B. 三维空间的点(继承)
题目描述
定义一个平面上的点C2D类,它含有一个getDistance()的成员函数,计算该点到原点的距离;从C2D类派生出三维空间的点C3D类,它的getDistance()成员函数计算该点到原点的距离。试分别生成一个C2D和C3D的对象,计算它们到原点的距离。三维空间的两点(x, y, z)和(x1, y1, z1)的距离公式如下:[(x-x1)^2+(y-y1)^2+(z-z1)^2]^(1/2)输入
第一行二维坐标点位置
第二行三维坐标点位置1
第三行三维坐标点位置2
输出
第一行二维坐标点位置到原点的距离
第二行三维坐标点位置1到原点的距离
第三行三维坐标点位置2到原点的距离
第四行三维坐标点位置2赋值给二维坐标点变量后,二维坐标点到原点的距离
输入样例1
3 4
3 4 5
6 8 8输出样例1
5
7.07107
12.8062
10
#include<iostream> #include <iomanip> #include<cmath> using namespace std; class C2d { protected: int x, y; public: float getdis() { return sqrt(x * x + y * y); } C2d(int xx, int yy) :x(xx), y(yy) { } }; class C3d :public C2d { protected: int z; public: float getdis() { return sqrt(x * x + y * y + z * z); } C3d(int xx, int yy, int zz) :C2d(xx, yy), z(zz) { } }; int main() { int x, y, z; cin >> x >> y; C2d c2(x, y); cin >> x >> y >> z; C3d c3_1(x, y, z); cin >> x >> y >> z; C3d c3_2(x, y, z); C2d c2_2(x, y); //返回的是值 函数需要cout 若为空返回,在函数里面cout cout<<c2.getdis()<<endl; cout<<c3_1.getdis()<<endl; cout<<c3_2.getdis()<<endl; cout<<c2_2.getdis()<<endl; }
C. 时钟模拟(继承)
题目描述
定义计数器类,包含保护数据成员value,公有函数increment计数加1。
定义循环计算器继承计数器类,增加私有数据成员:最小值minValue,maxValue,
重写公有函数increment,使得value在minValue~maxValue区间内循环+1。
定义时钟类,数据成员是私有循环计数器对象小时hour、分钟minute、秒second,公有函数time(int s)计算当前时间经过s秒之后的时间,即hour,minute,second的新value值。
定义时钟类对象,输入当前时间和经过的秒数,调用time函数计算新时间。
根据题目要求,增加必要的构造函数、析构函数和其他所需函数。
因为clock和time是系统内置函数,为了避免重名,请不要使用clock或者time作为类名或者函数名
输入
第一行测试次数n
2行一组,第一行为当前时间(小时 分钟 秒),第二行为经过的秒数。
输出
输出n行
每行对应每组当前时间和经过秒数后计算得到的新时间(小时:分钟:秒)。
输入样例1
2
8 19 20
20
23 30 0
1801输出样例1
8:19:40
0:0:1
#include <iostream> using namespace std; class jishu { protected: int value; public: void increment() { value++; } jishu(int v):value(v) { } int getvalue() { return value; } }; class xunhuan :public jishu { protected: int minvalue, maxvalue; public: void increment() { value++; if (value == maxvalue) { value = minvalue; } } xunhuan(int v,int min,int max):jishu(v),minvalue(min),maxvalue(max) { } }; class shizhong { protected: xunhuan hour, minute, second; public: shizhong(int h,int m,int s):hour(h,0,24),minute(m,0,60),second(s,0,60) { } void time(int s) { for (int i = 0; i < s; i++) //经过s秒 { second.increment(); if (second.getvalue() == 0) { minute.increment(); if (minute.getvalue() == 0) { hour.increment(); } } } } void print() { cout << hour.getvalue() << ":" << minute.getvalue() << ":" << second.getvalue() << endl; } }; int main() { int n; cin >> n; int hour, minute,second,s; while (n--) { cin >> hour >> minute >> second; cin >> s; shizhong s1(hour, minute, second); s1.time(s); s1.print(); } }
E. 学生成绩计算(继承)
题目描述
定义Person类具有姓名、年龄等属性,具有输出基本信息的display函数。
选修《面向对象程序设计》课程的学生在Person类的基础上,派生出子类:免听生和非免听生。子类继承父类成员,新增其他成员、改写display函数。
非免听生具有平时成绩、考试成绩和总评成绩三个属性,总评成绩根据(平时成绩*40%+考试成绩*60%)计算的结果,85分(包含)以上为A,75分(包含)-85分(不包含)为B,65分(包含)-75分(不包含)为C,60分(包含)-65分(不包含)为D,60分(不包含)以下为F。
免听生只有考试成绩和总评成绩两个属性,总评成绩100%根据考试成绩对应上述等级制成绩。
定义上述类并编写主函数,输入类型符号,若输入R,根据学生基本信息、平时成绩和考试成绩,建立非免听生对象,若输入S,根据学生基本信息、考试成绩,建立免听生对象。计算学生的总评成绩,并输出。
输入
测试次数t
随后每行输入学生类型 相关信息,姓名的最大字符长度为20
输出
每个学生基本信息和总评成绩
输入样例1
2
R cindy 18 100 100
S sandy 28 59输出样例1
cindy 18 A
sandy 28 F
#include<iostream> #include<cmath> #include<cstring> using namespace std; class person { protected: string name; int age; char level; public: void display() { cout << name << " "<<age; } person(string na, int a) :name(na),age(a) { } }; class r :public person { protected: int pingshi, kaoshi, zong; public: r(string na, int a, int p, int k):person(na,a),pingshi(p),kaoshi(k) { } void display() { zong = pingshi * 0.4 + kaoshi * 0.6; if (zong < 60) level = 'F'; if (zong >= 60) level = 'D'; if (zong >= 65) level = 'C'; if (zong >= 75) level = 'B'; if (zong >= 85) level = 'A'; cout << name << " " << age << " " << level << endl; } }; class s :public person { protected: int kaoshi, zong; public: s(string na, int a, int k) :person(na,a),kaoshi(k) { } void display() { zong = kaoshi; if (zong < 60) level = 'F'; if (zong >= 60) level = 'D'; if (zong >= 65) level = 'C'; if (zong >= 75) level = 'B'; if (zong >= 85) level = 'A'; cout << name << " " << age << " " << level << endl; } }; int main() { int t; cin >> t; char x; string name; int age, pingshi, kaoshi; while (t--) { cin >> x; if (x == 'R') { cin >> name >> age >> pingshi >> kaoshi; r r1(name, age, pingshi, kaoshi); r1.display(); } if (x == 'S') { cin >> name >> age >> kaoshi; s s1(name, age, kaoshi); s1.display(); } } }