用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();
}
}