C++之类与对象(练手题)

这两个题难度都不高,主要考察类与对象的初始化

1.一圆型游泳池如图1所示,现在需在其周围建一圆型过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米。过道宽度为3米,游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。                         

 

2. 有三个学生组队参加某比赛,每个学生信息包含准考证号,姓名,个人成绩,另团队有一总成绩。

①请写一学生类完成其定义实现。注意其中准考证号的不可变性,团队成绩的共享性;

②编写主程序模拟生成三个学生给其赋值、完成相关信息的输出。

基本要求:

能定义适当的类并定义对象完成设计并提交程序清单。

  • 过道算造价

#include<iostream>
#include<string>
using namespace std;

class Price							//造价类
{
public:
	void Bar_Way_Price(float r);     //计算游泳池边过道及栅栏造价
public:
	float _radial;		//半径
private:
	float s;        //面积
	float c;		//周长
};					
void Price::Bar_Way_Price(float r)				//计算游泳池边过道及栅栏造价
{
	s = 3.14*(r + 3)*(r + 3) - 3.14*r*r;    //大圆面积减去小圆面积得到圆形过道面积
	c = 2 * 3.14*(r + 3) + 2 * 3.14*r;    //大圆周长加小圆周长得到所需栅栏的长度
	auto way_price = s * 20;
	auto bar_price = c * 35;                    //与单价相乘得到造价
	cout << "过道的造价为:" << way_price << "元\t" << "栅栏的造价为:" << bar_price << "元\n" << endl;
}

int main()
{
	
	Price swim_pool;
	cout << "请输入游泳池的半径(米) "<< endl;
	cin >> swim_pool._radial;
	swim_pool.Bar_Way_Price(swim_pool._radial);

	system("pause");
	return 0;
}
  • 比赛信息

#include<iostream>
#include<string>
using namespace std;

class Student							//学生类
{
private:
	string exam_number[20];
	string stu_name[20];
public:
	float team_grade;
public:
	void PrintStuInfo();		
	void ScanStuInfo();
};
void Student::ScanStuInfo()					//输入学生信息
{
	cout << "请输入姓名及准考证号" << endl;
	for (int i = 0; i < 3 ; i++)
	{
		cout << "第" << i + 1 << "个学生姓名:" << endl;
		getline(cin, stu_name[i]);                  //获取学生姓名
		cout << "第" << i + 1 << "个学生准考证号:" << endl;
		getline(cin, exam_number[i]);               //获取学生准考证号     
	}
	cout << "请输入团队成绩:" << endl;
	cin >> team_grade;
	cout << "\n" << endl;
}
void Student::PrintStuInfo()				//输出学生信息
{
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stu_name[i] << "\t" << "准考证号:" << exam_number[i] << endl;
	}
	cout << "团队成绩:" << team_grade << "\n" << endl;
}

int main()
{
	Student stu;
	stu.ScanStuInfo();
	stu.PrintStuInfo();

	system("pause");
	return 0;
}

 

>>>如有问题,敬请指点,学艺不精,十分希望从各位大佬那里汲取经验。<<<

猜你喜欢

转载自blog.csdn.net/qq_40846862/article/details/89053599