实验1: C++类与对象——任务二+三

用C++语言编写函数,使用函数重载,能求两个整数的最大数、三个整数的最大数、四个整数的最大数。 

实验要求:

1.注意编程规范:程序开头部分的目的,作者以及日期;必要的空格与缩进等,最好跟上面的代码书写一样;

2.不少于3个的测试样例。

#include <iostream>
using namespace std;
int max(int a, int b)
{
	if (a > b)
		cout << "最大值为 : " << a << endl;
	else 
		cout << "最大值为 : " << b << endl;
	return 0;
}
int max(int a, int b, int c)
{
	if (a > b)
	{
		if (a > c) 
			cout << "最大值为 : " << a << endl;
		else 
			cout << "最大值为 : " << c << endl;
	}
	else
	{
		if (b > c) 
			cout << "最大值为 : " << b << endl;
		else 
			cout << "最大值为 : " << c << endl;
	}
	return 0;
}
int max(int a[4])
{
	int i, max = a[0];
	for (i = 0; i < 3; i++)
	{
		if (max < a[i]) max = a[i];
	}
	cout << "最大值为 : " << max << endl;
	return 0;
}
void main()
{
	int i, x, a[4];
	int n;
	cout << "请输入想输入数字的个数: " << endl;
	cin >> n;
	cout << "请输入数据: " << endl;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	if (n == 2)
		x = max(a[0], a[1]);
	else if (n==3)
		x = max(a[0], a[1], a[2]);
	else if (n == 4)
		x = max(a);
}

[实验任务三]学生类

定义一个学生类,设计私有数据成员:

年龄 int age;

姓名 string name;

公有成员函数:

带参数的初始化函数  Input(int a, string str);

获取数据成员函数    Output();

在主函数中定义一个有3个元素的对象数组并分别输入,然后输出对象数组的信息。

实验要求:

1.注意加上必要的输入输出提示;

2.注意开头的标注部分,加上自己的姓名以及修改日期;

3.附上不少于3个的测试结果。

#include <iostream>
using namespace std;

class Student
{
public:
	int age;
	string name;
	int Input(int a, string str);
	int Output();
};
int Student::Input(int a, string str)
{
	a = age;
	str = name;
	return 0;
}

int Student::Output()
{
	cout << age << " " << name << endl;
	return 0;
}
void main()
{
	int i;
	Student a[3];
	for (i = 0; i < 3; i++)
	{
		cout << "请输入学生年龄,姓名";
		cin >> a[i].age >> a[i].name;
		a[i].Input(a[i].age, a[i].name);
	}
	for (i = 0; i < 3; i++)
	{
		a[i].Output();
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值