A. 对象数组(类和对象)
题目描述
课堂上我们谈到类这个概念,比如第一题我们有学生类这个抽象的概念,成千上万个学生都具有同样的属性,但针对某个具体学生来说,他/她具有自己的鲜明个性,比如计算机专业的王海同学,信息工程学院的李慧同学,那么我们是定义一个该类的变量:Student student; 假设该类包含姓名、学号、性别、所属学院、联系电话等;在程序运行过程,把变量student赋予不同值就可以让它表示是王海还是李慧,尝试定义一个学生数组(比如四个元素大小,请思考此时四个对象是如何初始化的呢?),然后输入四个不同学生各项属性给数组里学生初始化(最好定义一个输入函数),然后输出该学生对象数组的各对象的姓名。
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 #include <iostream> #include <string> using namespace std;class Student { public : void Set () ; void Print () ; private : string name; string number; string sex; string college; string phone; }; void Student::Set () { cin >> name >> number >> sex >> college >> phone; } void Student::Print () { cout << name << endl; } int main () { int n; cin >> n; Student* p = new Student[n]; for (int i = 0 ; i < n; i++) { p[i].Set (); p[i].Print (); } delete []p; }
B.存折类定义(类与对象)
题目描述
定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。
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 #include <iostream> #include <iostream> using namespace std;class CAccount { public : void input () ; void deposit () ; void withdraw () ; void check () ; private : long account; string name; float balance; double deposit_amount; double withdrawal_amount; }; void CAccount::input () { cin >> account >> name >> balance >> deposit_amount >> withdrawal_amount; } void CAccount::deposit () { balance += deposit_amount; cout << "saving ok!" << endl; } void CAccount::withdraw () { if (withdrawal_amount > balance) { cout << "sorry! over limit!" << endl; } else { balance -= withdrawal_amount; cout << "withdraw ok!" << endl; } } void CAccount::check () { cout << name << "'s balance is " << balance << endl; } int main () { CAccount a[10 ]; for (int i = 0 ; i < 2 ; i++) { a[i].input (); a[i].check (); a[i].deposit (); a[i].check (); a[i].withdraw (); a[i].check (); } return 0 ; }
C.点和圆 (类与对象)
题目描述
设计一个点类Point,包含属性:x坐标和y坐标,方法:设定坐标(setPoint),获取x坐标(getX),获取y坐标(getY)
设计一个圆类Circle,包含属性:圆心坐标x和y、半径r;方法包括:
设定圆心(setCenter),设置圆心x坐标和y坐标
设定半径(setRadius),设置半径长度
计算面积(getArea),计算公式:面积=3.14*r*r
计算周长(getLength),计算公式:周长=2*3.14*r
包含(contain),判断一个圆是否包含一个点,计算圆心到这个点的距离,然后和半径做比较,大于则不包含,小于等于则包含
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 #include <iostream> #include <iomanip> #include <cmath> using namespace std;class Point { public : double x, y; void SetPoint (double x1, double y1) ; double getX () ; double getY () ; }; void Point::SetPoint (double x1, double y1) { x = x1; y = y1; } double Point::getX () { return x; } double Point::getY () { return y; } class Circle { Point p; double r; public : void SetCenter (double x1, double y1) ; void SetRadius (double n) ; double getArea () ; double getLength () ; double Contain (Point q) ; }; void Circle::SetCenter (double x1, double y1) { p.SetPoint (x1, y1); } void Circle::SetRadius (double n) { r = n; } double Circle::getArea () { return 3.14 * r * r; } double Circle::getLength () { return 3.14 * 2 * r; } double Circle::Contain (Point q) { double D_length; D_length = sqrt ((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y)); return D_length; } int main () { double x, y, r, x1, y1; double area, length; cin >> x >> y >> r >> x1 >> y1; Circle c; Point p; c.SetCenter (x, y); c.SetRadius (r); p.SetPoint (x1, y1); area = c.getArea (); length = c.getLength (); if (c.Contain (p) <= r) { cout << fixed << setprecision (2 ) << area << " " << fixed << setprecision (2 ) << length << endl; cout << "yes" << endl; } else { cout << fixed << setprecision (2 ) << area << " " << fixed << setprecision (2 ) << length << endl; cout << "no" << endl; } return 0 ; }
D.最胖的加菲(类与对象+数组)
题目描述
有一群猫猫,每只猫都有自己的名称和体重。
用类来描述猫,名称和体重都是私有属性,要求加入属性的get方法。其他函数根据需要自己定义
创建一个动态的猫对象数组,存储各只猫的名称和体重
根据猫的体重对数组做升序排序,并输出排序后每只猫的名称
题目涉及的数值均用整数处理
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 #include <iostream> #include <iomanip> #include <string> using namespace std;class Cat { public : void set () { cin >> name >> weight; } int get_wei () { return weight; } void get_nam () { cout << name; } private : string name; int weight; }; void Sort (Cat& a, Cat& b) { Cat tmp; if (a.get_wei () > b.get_wei ()) { tmp = a; a = b; b = tmp; } } int main () { int n; cin >> n; Cat* p = new Cat[n]; for (int i = 0 ; i < n; i++) { p[i].set (); } for (int i = 1 ; i < n; i++) for (int j = 0 ; j < n - i; j++) { Sort (p[j], p[j + 1 ]); } for (int i = 0 ; i < n; i++) { p[i].get_nam (); if (i < n - 1 ) cout << " " ; } delete []p; return 0 ; }
E.身体评估(类与对象)
题目描述
评估成年人身体健康有多个指标,包括BMI、体脂率等
设计一个身体健康类,包含私有成员:姓名、身高(米)、体重(公斤),腰围(厘米),实现两个公有方法如下:
BMI方法,返回BMI数值(整数,四舍五入到个位),计算公式BMI= 体重 / 身高的平方
体脂率方法,返回体脂率数值(浮点数),计算过程如下:
1)参数a=腰围(cm)×0.74
2)参数b=体重(kg)×0.082+34.89
3)体脂肪重量(kg)=a-b
4)体脂率 = 体脂肪重量÷体重
其它方法根据需要自行定义
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 #include <iostream> #include <iomanip> #include <string> #include <math.h> using namespace std;class Health { public : void input () ; void display () ; private : string name; double height; double weight; double w_circumference; }; void Health::input () { cin >> name >> height >> weight >> w_circumference; } void Health::display () { double bmi, a, b, c, d; int BMI; bmi = weight / (height * height); BMI = round (bmi); a = w_circumference * 0.74 ; b = weight * 0.082 + 34.89 ; c = a - b; d = c / weight; cout << name << "的BMI指数为" << BMI << "--体脂率为" << fixed << setprecision (2 ) << d << endl; } int main () { int t, i; cin >> t; Health* a = new Health[t]; for (i = 0 ; i < t; i++) { a[i].input (); } for (i = 0 ; i < t; i++) { a[i].display (); } delete []a; return 0 ; }