面向对象程序设计

编程入门 行业动态 更新时间:2024-10-26 14:30:38

面向对象<a href=https://www.elefans.com/category/jswz/34/1771020.html style=程序设计"/>

面向对象程序设计

通过几道OJ题目,掌握一些类与对象的基础知识……

问题一:学生类定义

题目描述:

面向对象程序设计的中心就是把客观事物抽象为程序世界里一段段代码,校园里的主体是学生,泛泛的学生包含很多属性,比如姓名、学号、所在学院、专业、性别、住址、联系电话。。。。。。等等,有这些属性,需要操纵它们的动作,比如读取姓名、设置姓名、读取学号、设置学号。。。。。。等等,这就是我们课堂说的属性和方法,对于属性和方法,我们又有访问控制方式限制,标示为public、private、protected等,根据以上的信息,请给出一个完整的学生类定义:Student,并测试输出n个该类对象的各项属性值。

输入要求:

第一行表示要输入n个对象

后续各行输入不同对象的各属性值,每个对象一行。

输出要求:

输出不同对象的各自属性

每个对象占一行

输入样例:

2
WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777

输出样例:

WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string> 
#include<cmath>
#include<algorithm>
using namespace std;class Student {
public:void setName(string name0){name = name0;}void setId(string id0){id = id0;}void setDepartment(string department0){department = department0;}void setMajor(string major0){major = major0;}void setSex(string sex0){sex = sex0;}void setAddress(string address0){address = address0;}void setPhone(string phone0){phone = phone0;}string getName(){return name;}string getId(){return id;}string getDepartment(){return department;}string getMajor(){return major;}string getSex(){return sex;}string getAddress(){return address;}string getPhone(){return phone;}private://由于private,在类外部无法访问,便设定setXXXX函数对其赋值string name, id, department, major, sex, address, phone;
};int main() {int t;cin >> t;while (t--){Student s;string name, id, department, major, sex, address, phone;cin >> name >> id >> department >> major >> sex >> address >> phone;s.setName(name);//对类内部private属性进行赋值s.setId(id);s.setDepartment(department);s.setMajor(major);s.setSex(sex);s.setAddress(address);s.setPhone(phone);cout << s.getName() << " " << s.getId() << " " << s.getDepartment() << " " << s.getMajor() << " " << s.getSex() << " " << s.getAddress() << " " << s.getPhone() << endl;//通过设定get函数对private属性进行调用}
}

一道十分简单基础的题目,有利于传参好习惯的养成。

问题二:音像制品

题目描述:

某商店出租音像制品,制品信息包括:类型、名称、租金单价、状态。

其中类型用单个数字表示,对应关系为:1-黑胶片(BF),2-CD,3-VCD,4-DVD

名称是字符串,存储制品的名称信息

租金单价表示每天租金价格

状态用单个数字表示,0是未出租(not rented),1是已出租(rented)

商店提供业务操作包括

1. 初始化(可使用构造函数或set方法),从键盘输入音像制品的信息,并设置到对象中

2. 查询Print,输出音像制品的信息

3. 计算租金Fee,参数是租借的天数,输出租金总价,如果未出租则提示,具体输出信息看示范

请定义音像制品类,并创建相应的对象来完成操作

题目涉及的数值均用整数处理

输入要求:

第一行输入n表示有n个音像制品

每个音像制品对应两行输入

一行输入一个音像制品的多个参数,具体为:类型、名称、租金单价、状态

一行输入操作命令,如果输入为0表示查询操作,非0则表示查询并且计算租金费用,租用天数就是这个非0值

依次输入2n行

输出要求:

根据每个音像制品的操作命令,调用相应的操作,然后输出相关结果

输出样式看示范。

输入样例:

4
1 AAA 43 1
0
2 BBB 19 0
3
3 CCC 27 1
5
4 DDD 32 1
7

输出样例:

BF[AAA]rented
CD[BBB]not rented
No rental
VCD[CCC]rented
Rental: 135
DVD[DDD]rented
Rental: 224
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string> 
#include<cmath>
#include<algorithm>
using namespace std;class Video {
public:void setType(int type0)//根据输入的数字找到对应的类型{if (type0 == 1){type = "黑胶片";}else if (type0 == 2){type = "CD";}else if (type0 == 3){type = "VCD";}else if (type0 == 4){type = "DVD";}}void setName(string name0){name = name0;}void setCent(int cent0){cent = cent0;}void setCondition(int con0)//con与condition分开,利于对是否出租进行判断{con = con0;if (con0 == 0){condition = "未出租";}else if (con == 1){condition = "已出租";}}string getType()//设置get函数,调用类里的private属性{return type;}string getName(){return name;}string getCondition(){return condition;}int getCon(){return con;}int getCent(){return cent;}
private:string type, name, condition;int cent, con;
};int main() {int t;cin >> t;while (t--){Video v;string name0;int cent0, con0, type0;cin >> type0 >> name0 >> cent0 >> con0;v.setType(type0);v.setName(name0);v.setCent(cent0);v.setCondition(con0);int operate;cin >> operate;if (operate == 0)//直接查询{cout << v.getType() << "[" << v.getName() << "]" << v.getCondition() << endl;}else if (operate > 0){cout << v.getType() << "[" << v.getName() << "]" << v.getCondition() << endl;if (v.getCon() == 0)//若出租则计算租金,不出租则未产生租金{cout << "未产生租金" << endl;}else{cout << "当前租金为" << operate * v.getCent() << endl;}}}return 0;
}

根据指定要求传参数,调用函数即可。

问题三:身体评估

题目描述:

评估成年人身体健康有多个指标,包括BMI、体脂率BFR等

设计一个身体健康类,包含私有成员:姓名、身高(米)、体重(公斤),腰围(厘米),实现两个公有方法如下:

BMI方法,返回BMI数值(整数),计算公式BMI= 体重 / 身高的平方

体脂率方法,返回体脂率数值(浮点数),计算过程如下:

1)参数a=腰围(cm)×0.74

2)参数b=体重(kg)×0.082+34.89

3)体脂肪重量(kg)=a-b

4)体脂率 = 体脂肪重量÷体重

其它方法根据需要自行定义

输入要求:

第一行输入t表示有t个测试实例

第二行起,每行输入四个参数:姓名、身高、体重,腰围

依次输入t行

输出要求:

输出t行,每行输入一个实例的BMI和体脂率,小数数值精确到小数点后2位,用空格隔开

输入样例:

2
David 1.85 78.5 85.2
Sara 1.55 67.6 77.3

输出样例:

David's BMI: 23--BFR: 0.28
Sara's BMI: 28--BFR: 0.25
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string> 
#include<cmath>
#include<algorithm>
using namespace std;class Health {
public:void setName(string name0){name = name0;}void setBMI(float h0, float w0)//通过函数计算BMI指数{BMI = w0 / (h0 * h0);}void setBFR(float w0, float wl0)//通过函数计算BFR指数{BFR = (wl0 * 0.74 - w0 * 0.082 - 34.89) / w0;}string getName() {return name;}float getBMI() {return BMI;}float getBFR() {return BFR;}private:string name;float height, weight, wline, BMI, BFR;
};int main() {int t;cin >> t;while (t--){Health s;string name;float height, weight, wline;cin >> name >> height >> weight >> wline;s.setName(name);s.setBMI(height, weight);s.setBFR(weight, wline);cout << s.getName() << "'s BMI: ";cout << fixed << setprecision(0) << s.getBMI() << "--BFR: ";//fixed<<setpercision(x)为精确到x位小数cout << fixed << setprecision(2) << s.getBFR() << endl;}
}

问题四:点和圆

题目描述:

设计一个点类Point,包含属性:x坐标和y坐标,方法:设定坐标(SetPoint),获取x坐标(GetX),获取y坐标(GetY)

设计一个圆类Circle,包含属性:圆心坐标x和y、半径r;方法包括:

1. 设定圆心(SetCenter),设置圆心x坐标和y坐标

2. 设定半径(SetRadius),设置半径长度

3. 计算面积(getArea),计算公式:面积=3.14*r*r

4. 计算周长(getLength),计算公式:周长=2*3.14*r

5. 包含(Contain),判断一个圆是否包含一个点,计算圆心到这个点的距离,然后和半径做比较,大于则不包含,小于等于则包含

注意:提交代码时必须用注释划分出三个区域:类定义、类实现、主函数,如下

//-----类定义------

class XXX

{ // 写类定义代码

};

//----类实现------

void XXX::Process()

{ // 写类定义代码

};

//-----主函数-----

int main()

{

//自定义一些变量

//创建一个圆对象和一个点对象

//输入圆对象和点对象的属性数值,并做初始化

//输出圆的面积和圆的周长

//输出圆是否包含点,包含则输出yes,否则输出no

return 0;

}

输入要求:

第一行输入圆的三个参数:圆心的x和y坐标,半径

第二行输入点的两个参数:x和y坐标

输出要求:

第一行输出圆的面积和周长,结果之间用空格隔开,输出精度到小数点后2位

第二行输出圆是否包含点,包含则输出yes,否则输出no

输入样例:

1 1 1
2 2

输出样例:

3.14 6.28
no
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string> 
#include<cmath>
#include<algorithm>
using namespace std;class Point {
public:void setPoint(float ix, float iy){x = ix;y = iy;}float getX(){return x;}float getY(){return y;}private:float x, y;
};class Circle {
public:void setCenter(float jx, float jy){x = jx;y = jy;}void setRadius(float jr){r = jr;}float getX(){return x;}float getY(){return y;}float getRadius(){return r;}float getArea(){return 3.14 * r * r;}float getLength(){return 2 * 3.14 * r;}
private:float x, y, r;
};int main() {Point p;Circle c;float x0, y0, r0, x1, y1;cin >> x0 >> y0 >> r0 >> x1 >> y1;c.setCenter(x0, y0);c.setRadius(r0);p.setPoint(x1, y1);cout << fixed << setprecision(2) << c.getArea() << " " << c.getLength() << endl;//固定精度输出面积与周长,面积与周长都是定义在类中的属性if ((c.getX() - p.getX()) * (c.getX() - p.getX()) + (c.getY() - p.getY()) * (c.getY() - p.getY()) > c.getRadius())//点与圆位置关系的判定{cout << "no" << endl;}else{cout << "yes" << endl;}
}

问题五:存折类定义

题目描述:

定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。

编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。

输入要求:

第一个存折的账号、姓名、余额

存款金额

取款金额

第二个存折的账号、姓名、余额

存款金额

取款金额

输出要求:

第一个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

第二个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

输入样例:

9111 Tom 1000
500
1000
92220 John 500
500
1500

输出样例:

Tom's balance is 1000
saving ok!
Tom's balance is 1500
withdraw ok!
Tom's balance is 500
John's balance is 500
saving ok!
John's balance is 1000
sorry! over limit!
John's balance is 1000
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string> 
#include<cmath>
#include<algorithm>
using namespace std;class CAccount {
public:void Deposit(float d0)//存款操作对应的函数{balance += d0;cout << "saving ok!" << endl;}void Withdraw(float w0)//取款操作对对应的函数{if (w0 <= balance){balance -= w0;cout << "withdraw ok!" << endl;}else{cout << "sorry! over limit!" << endl;}}void Check(){cout << name << "'s balance is " << balance << endl;}void setAccount(long acnt){account = acnt;}void setName(string name0){name = name0;}void setBalance(float b0){balance = b0;}private:long account;string name;float balance;
};int main() {for (int i = 0; i < 2; i++){CAccount s;long ac0;string n0;float b0, d0, w0;cin >> ac0 >> n0 >> b0;s.setAccount(ac0);//一个不断设置指令,调用指令的过程s.setName(n0);s.setBalance(b0);s.Check();cin >> d0;s.Deposit(d0);s.Check();cin >> w0;s.Withdraw(w0);s.Check();}
}

根据要求,完成类与对象的编写,通过指令,运行代码。

更多推荐

面向对象程序设计

本文发布于:2024-03-07 17:15:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序设计   面向对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!