C++ 实验二 : 类与对象定义初始化

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

在这里插入图片描述

  1. 有三个学生组队参加某比赛,每个学生信息包含准考证号,姓名,个人成绩,另团队有一总成绩。
  1. 请写一学生类完成其定义实现。注意其中准考证号的不可变性,团队成绩的共享性;
  2. 编写主程序模拟生成三个学生给其赋值、完成相关信息的输出。
    基本要求
    能定义适当的类并定义对象完成设计并提交程序清单。

直接给出代码

1. 求造价

#include<iostream>
#include<math.h>

using namespace std;

double pi = acos(-1);

void test() {
	int r;
	cout << "输入泳池半径: ";
	cin >> r;
	
	//大圆半径
	int r2 = r + 3;

	//环形面积
	double s = (pi * r2 * r2) - (pi * r * r);
	//大圆周长
	double l = 2 * pi * r2;


	cout << "过道造价: " << s * 20 << endl;
	cout << "栅栏造价: " << l * 35 << endl;
}


int main() {
	test();

	return 0;
}

2. 成绩类

#include<iostream>
#include<string>

using namespace std;


class Student {
public:
	Student(const string num = "123", string name = "张三", double pGrade = 100)
		:_num(num)
		, _name(name)
		, _pGrade(pGrade)
	{
	}


	Student& operator=(const Student& tmp) {
		_num = tmp._num;
		_name = tmp._name;
		_pGrade = tmp._pGrade;
		return *this;
	}

	void teamGradeAdd(const Student& tmp) {
		_teamGrade += tmp._pGrade;
	}

	void printInfo() {
		cout << _num << "\t" << _name << "\t" << _pGrade << "\t" << endl;
	}


private:
	string _num;
	string _name;
	double _pGrade;
public:
	static double _teamGrade;
};


double Student::_teamGrade = 0;



int main() {
	string num, name;
	double grade;

	Student* s = new Student[3];
	for (int i = 0; i < 3; i++) {
		cin >> num >> name;
		cin >> grade;
		Student tmp(num, name, grade);

		s[i] = (tmp);
		s[i].teamGradeAdd(tmp);

	}

	cout << endl << "学号" << "\t" << "姓名" << "\t" << "个人成绩" << endl;
	for (int i = 0; i < 3; i++) {
		s[i].printInfo();
	}
	cout << "团队成绩:" << Student::_teamGrade << endl;

	return 0;
}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殇&璃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值