实验四 类与对象

本文介绍了四个关于类和对象的编程题目,包括学生数组初始化、存折类定义、点和圆的运算以及猫的体重排序。每个题目都详细描述了类的属性和方法,并给出了相应的输入输出示例。通过这些例子,读者可以更好地理解如何在C++中使用类和对象进行编程。
摘要由CSDN通过智能技术生成

A. 对象数组(类和对象)

题目描述

        课堂上我们谈到类这个概念,比如第一题我们有学生类这个抽象的概念,成千上万个学生都具有同样的属性,但针对某个具体学生来说,他/她具有自己的鲜明个性,比如计算机专业的王海同学,信息工程学院的李慧同学,那么我们是定义一个该类的变量:Student  student; 假设该类包含姓名、学号、性别、所属学院、联系电话等;在程序运行过程,把变量student赋予不同值就可以让它表示是王海还是李慧,尝试定义一个学生数组(比如四个元素大小,请思考此时四个对象是如何初始化的呢?),然后输入四个不同学生各项属性给数组里学生初始化(最好定义一个输入函数),然后输出该学生对象数组的各对象的姓名。

输入

输入数组元素的大小

依次每行输入每个对象的各项属性值,各属性值最大长度不超过20

输出

每行输出一个学生类对象的姓名

输入样例1

3
tom 2013333333 男 计算机学院 13766666666
Marry 2012222222 女 信息工程学院 13555555555
John  2014444444 男 管理学院 13888888888

输出样例1

tom
Marry
John

#include<stdio.h>
#include<iostream>
#include <iomanip>
#include<cstring>
#include<algorithm>    //sort函数 sort(begin,end,cmp)  
using namespace std;
class student
{   private:
	string name,num,sex,college,phone;
    public:
	void set()
	{
		cin >> name >>num>> sex >> college >> phone;

	}
	void print()
	{
		cout << name<<endl;
	}

};
int main()
{
	int n;
	cin >> n;
	student* stu = new student [n];
	for (int i = 0; i < n; i++)
	{
		stu[i].set();
	}
	for (int i = 0; i < n; i++)
	{
		stu[i].print();
	}
	delete[]stu;
}

B. 存折类定义(类与对象)

题目描述

定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。

输入

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

存款金额

取款金额

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

存款金额

取款金额

输出

第一个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

第二个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

输入样例1

9111 Tom 1000
500
1000
92220 John 500
500
1500

输出样例1

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<stdio.h>
#include<iostream>
#include <iomanip>
#include<cstring>
#include<algorithm>    //sort函数 sort(begin,end,cmp)  
using namespace std;
class CAccount
{
   private:
	   long account;
	   string name;
	   float balance;
   public:
	   void set(long a,string n,float b)
	   {
		   account = a;
		   name = n;
		   balance = b;
	   }
	   void deposit(float plus)
	   {
		   balance += plus;
		   cout << "saving ok!" << endl;
	   }
	   void withdraw(float minus)
	   {
		   if (minus > balance)
		   {
			   cout << "sorry! over limit!" << endl;
		   }
		   else
		   {
			   balance -= minus;
			   cout << "withdraw ok!" << endl;
		   }
	   }
	   void check()
	   {
		   cout << name << "'s banlance is " << balance << endl;
	   }
};
int main()
{
	long account;
	string name;
	float balance,num;
	CAccount c1;
	cin >> account >> name >> balance;
	c1.set(account,name,balance);
	cin >> num;
	c1.check();
	c1.deposit(num);
	c1.check();
	cin >> num;
	c1.withdraw(num);
	c1.check();
	CAccount c2;
	cin >> account >> name >> balance;
	c2.set(account, name, balance);
	cin >> num;
	c2.check();
	c2.deposit(num);
	c2.check();
	cin >> num;
	c2.withdraw(num);
	c2.check();
}

C. 点和圆 (类与对象)

题目描述

设计一个点类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

在C++中,输出指定精度的参考代码如下:

#include <iostream>

#include <iomanip> //必须包含这个头文件

using namespace std;

void main( )

{

        double a =3.14;

        cout<<fixed<<setprecision(3)<<a<<endl;  //输出小数点后3位

}

输入样例1 

1 1 1
2 2
输出样例1

3.14 6.28
no
 

#include<stdio.h>
#include<iostream>
#include <iomanip>
#include<cstring>
#include<algorithm>  
#include<cmath>
using namespace std;
class Point
{
private:
	int x, y;
public:
	void setpoint(int xx, int yy)
	{
		x = xx;
		y = yy;
	}
	int getx()
	{
		return x;
	}
	int gety()
	{
		return y;
	}
};
class Circle
{
private:
	int x, y,r;
public:
	void setcenter(int xx,int yy)
	{
		x = xx;
		y = yy;
	}
	void setr(int rr)
	{
		r = rr;

	}
	float getarea()   //返回浮点数
	{
		return 3.14 * r * r;
	}
	float getlenth()
	{
		return 3.14 * 2 * r;

	}
	void contain(Point& p)
	{
		int dis;
		dis = sqrt((p.getx() - x) * (p.getx() - x) + (p.gety() - y) * (p.gety() - y));
		if (dis < r)
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
	}
};
int main()
{
	int x, y, r;
	int p_x, p_y;
	cin >> x >> y >> r;
	Circle c1;
	c1.setcenter(x, y);
	c1.setr(r);
	Point p1;
	cin >> p_x >> p_y;
	p1.setpoint(p_x, p_y);
	cout << fixed << setprecision(2) << c1.getarea() << ' ';
	cout << fixed << setprecision(2) << c1.getlenth() << endl;
	c1.contain(p1);

}

D. 最胖的加菲(类与对象+数组)

题目描述

有一群猫猫,每只猫都有自己的名称和体重。

用类来描述猫,名称和体重都是私有属性,要求加入属性的get方法。其他函数根据需要自己定义

创建一个动态的猫对象数组,存储各只猫的名称和体重

根据猫的体重对数组做升序排序,并输出排序后每只猫的名称

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

输入

第一行输入n表示有n只猫

第二行输入一只猫的名称和体重,名称的最大长度为30

依次输入n行

输出

输出一行,输出排序后的猫的名称

输入样例1

4
巧克力胖三斤 1500
自来水瘦八两 400
芝士蛋糕肥六斤 3000
蔬菜沙拉轻四两 200
输出样例1

蔬菜沙拉轻四两 自来水瘦八两 巧克力胖三斤 芝士蛋糕肥六斤
 

#include<stdio.h>
#include<iostream>
#include <iomanip>
#include<string>
#include<algorithm>    //sort函数 sort(begin,end,cmp)  
#include<math.h>
using namespace std;
class cat
{
private:
	string name;
	int weight;
public:
	int getweight()
	{
		return weight;
	}
	string getname()
	{
		return name;
	}
	void set(string n, int w)
	{
		name = n;
		weight = w;
	}
	
};
void swap(cat& a, cat& b)
{
	cat t;
	if (a.getweight() > b.getweight()) //看体重 升序排猫
	{
		t = a;
		a = b;
		b = t;
	}
}
int main()
{
	int n;
	cin >> n;
	cat* c = new cat[n];
	for(int i=0;i<n;i++)
	{
		string name;
		int weight;
		cin >> name >> weight;
		c[i].set(name, weight);
	}
	for (int i = 1; i < n; i++)      //n-1次冒泡排序
	{
		for (int j = 0; j < n - i; j++) //一次冒泡排序  n-i次比较
		{
			swap(c[j], c[j + 1]); 
		}
	}
	for (int i = 0; i < n; i++)
	{
		cout<<c[i].getname()<<" ";
	}
	
}

E. 身体评估(类与对象)

题目描述

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

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

BMI方法,返回BMI数值(整数,四舍五入到个位),计算公式BMI= 体重 / 身高的平方

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

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

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

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

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

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

输入

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

第二行起,每行输入四个参数:姓名、身高、体重、腰围,姓名的最大长度不超过20

依次输入t行

输出

输出t行,每行输入一个实例的BMI和体脂率,BMI四舍五入到个位,体脂率小数数值精确到小数点后2位

输入样例1 

2
张高 1.85 78.5 85.2
李圆 1.55 67.6 77.3
输出样例1

张高的BMI指数为23--体脂率为0.28
李圆的BMI指数为28--体脂率为0.25

#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstring>
using namespace std;
class health
{
private:
	string name;
	double h, w, waist;
public:
	double getbmi()
	{
		return w / (h * h);
	}
	double getbodyfat()
	{
		double a = waist * 0.74;
		double b = w * 0.082 + 34.89;
		double c = a - b;
		return c / w;
	}
	void set(string n, double hh, double ww, double wa)
	{
		name = n;
		h = hh;
		w = ww;
		waist = wa;
	}
	string getname()
	{
		return name;
	}

};
int main()
{
	int t;
	cin >> t;
	health *person=new health[t];
	for(int i=0;i<t;i++)
	{
		string name;
		double h, w, waist;
		cin >> name >> h >> w >> waist;
		person[i].set(name, h, w, waist);
		cout << person[i].getname() << "的BMI指数为" << fixed << setprecision(0) << person[i].getbmi();//四舍五入到个位
		cout << "--";
		cout << "体脂率为" << fixed << setprecision(2) <<person[i].getbodyfat() << endl;
	}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值