c++函数重载

掌握函数重载的几种方式,并掌握带缺省值参数的函数重载机制。
要求1:利用重载求两个整数、三个整数和四个整数的最小值。
要求2:利用重载实现对10个整数和10个实数的排序。
要求3:定义类Rectangle为一般矩形和正方形,利用内联和重载方式实现其构造函数,并定义成员函数计算输出其面积

#include<iostream>
#include<string>
using namespace std;
int add(int a, int b);
int add(int a, int b, int c);
int add(int a, int b, int c, int d);
int sort(int b[9]);
void sort(double c[9]);

class Rectangle
{
public:
	Rectangle(double m, double n);
	Rectangle(double m);
private:
	double m, n;
};
inline Rectangle::Rectangle(double m, double n) 
{
	cout << "矩形面积为:" << m * n << endl;
}
inline Rectangle::Rectangle(double m)
{
	cout << "正方形面积为:" << m * m << endl;
}
int main()
{
	int n,i;
	cout << "输入你的选择:1--求最小值  2--排序  3--求面积  4--退出" << endl;
	cout << "你的选择是--";
	cin >> n;
	while (n != 5)
	{
		if (n == 1)
		{
			int a[4];
			cout << "请输入整数的个数:";
			cin >> i;
			switch (i)
			{
			case 2: {cin >> a[1] >> a[2]; cout << "最小值为:" << add(a[1], a[2]) << endl; break; }
			case 3: {cin >> a[1] >> a[2] >> a[3]; cout << "最小值为:" << add(a[1], a[2], a[3]) << endl; break; }
			case 4: {cin >> a[1] >> a[2] >> a[3] >> a[4]; cout << "最小值为:" << add(a[1], a[2], a[3], a[4]) << endl; break; }
			default:cout << "输入错误!" << endl;
			}
		}
		else if (n == 2)
		{
			cout << "请选择排序的是1--整数 2--实数:";
			cin >> i;
			if (i == 1)
			{
				int b[9];
				for (i = 0; i < 10; i++)
					cin >> b[i];
				sort(b);
			}
			else if (i == 2)
			{
				double c[9];
				for (i = 0; i < 10; i++)
					cin >> c[i];
				sort(c);
			}
			else
				cout << "输入错误!" << endl;
		}
		else if (n == 3)
		{
			double m, n;
			cout << "请输入要计算1--正方形  2--矩形的面积:";
			cin >> i;
			if (i == 1)
			{
				cout << "请输入正方形的边长:";
				cin >> m;
				Rectangle::Rectangle(m);
			}
			else if (i == 2)
			{
				cout << "请输入矩形的边长:";
				cin >> m >> n;
				Rectangle::Rectangle(m, n);
			}
			else
				cout << "输入错误!" << endl;
		}
		else
			break;
       cout << "输入你的选择:1--求最小值  2--排序  3--求面积  4--退出" << endl;
		cout << "你的选择是--";
		cin >> n;
	}
}
int add(int a, int b)
{
	return a < b ? a : b;
}
int add(int a, int b, int c)
{
	int d = a < b ? a : b;
	return c < d ? c : d;
}
int add(int a, int b, int c, int d)
{
	int e = a < b ? a : b;
	int f = c < d ? c : d;
	return e < f ? e : f;
}
int sort(int b[9])
{
	int t,i,j;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (b[i] > b[j])
			{
				t = b[i];
				b[i] = b[j];
				b[j] = t;
			}
		}
	}
	for (i = 0; i < 10; i++)
		cout << b[i] << endl;
	return 1;
}
void sort(double c[9])
{
	double t;
	int i, j;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (c[i] > c[j])
			{
				t = c[i];
				c[i] = c[j];
				c[j] = t;
			}
		}
	}
	for (i = 0; i < 10; i++)
		cout << c[i] << endl;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值