【C++初学】课后作业汇总复习(二)函数重载与类的成员函数重载,默认参数值函数——认识多态 - Toggle
1、 重载求最大值函数(zlh)
定义重载函数maxinum,分别求三个整数、三个浮点数和三个字符中最大的一个并输出。
输入样例1:45 67 100 34 2.3 78.2 a A B
输出样例1:100 78.2 a
输入样例2:0 1 2 2.3 3.4 8.0 d r z
输出样例2:2 8.0 z
//重载求最大值函数(zlh)
//定义重载函数maxinum,分别求三个整数、三个浮点数和三个字符中最大的一个并输出。
//
//输入样例1:45 67 100 34 2.3 78.2 a A B
//输出样例1:100 78.2 a
//
//输入样例2:0 1 2 2.3 3.4 8.0 d r z
//输出样例2:2 8.0 z
#include <iostream>
using namespace std;
int maxinum(int x, int y ,int z)
{
return (x > y?(x > z? x : z):(y>z?y:z));
}
float maxinum(float x, float y ,float z)
{
return (x > y?(x > z? x : z):(y>z?y:z));
}
char maxinum(char x, char y ,char z)
{
return (x > y?(x > z? x : z):(y>z?y:z));
}
//StudybarCommentBegin
int main()
{
int b1, b2, b3;
float c1, c2, c3;
char a1, a2, a3;
cin >> b1 >> b2 >> b3 >> c1 >> c2 >> c3>> a1 >> a2 >> a3;
cout << maxinum(b1, b2, b3) << ' ' << maxinum(c1, c2, c3) << ' ' << maxinum(a1, a2, a3);
return 0;
}
//StudybarCommentEnd
2.重载middle函数,读后缀代码,设计不同类型参数的重载函数
Description:
编写一个C++风格的程序,建立一个名为middle的函数,其功能是返回三个数当中中间那个数的值。重载middle函数3次,分别完成返回3个整数、3个双精度数和3个无符号整数的中间那个数的值。要求这9个数分别存放在3个相应类型的数组中。
注意:程序前缀代码及后缀代码均已给出。
Sample Input:
无
Sample Output:
The numbers are 45 3 89
the middle of them is 45
The numbers are 12.4 6 55.8
the middle of them is 12.4
The numbers are 345 776 565
the middle of them is 565
//Description:
// 编写一个C++风格的程序,建立一个名为middle的函数,其功能是返回三个数当中中间那个数的值。重载middle函数3次,分别完成返回3个整数、3个双精度数和3个无符号整数的中间那个数的值。
//要求这9个数分别存放在3个相应类型的数组中。
//
//注意:程序前缀代码及后缀代码均已给出。
//
//Sample Input:
//无
//
//Sample Output:
//The numbers are 45 3 89
//the middle of them is 45
//The numbers are 12.4 6 55.8
//the middle of them is 12.4
//The numbers are 345 776 565
//the middle of them is 565
#include <iostream>
using namespace std;
#include <algorithm>
int middle(int arr[3])
{
sort(arr, arr+3);
return arr[1];
}
double middle(double arr[3])
{
sort(arr, arr+3);
return arr[1];
}
unsigned int middle(unsigned int arr[3])
{
sort(arr, arr+3);
return arr[1];
}
//StudybarCommentBegin
int main()
{
int iNumber[3]={45,3,89};
double dNumber[3]={12.4,6,55.8};
unsigned uNumber[3]={345,776,565};
cout<<"\nThe numbers are ";
for( int i=0;i<3;i++)
cout<<iNumber[i]<<"\t";
cout<<endl<<"the middle of them is "<<middle(iNumber)<<endl;
cout<<"\nThe numbers are ";
for( int i=0;i<3;i++)
cout<<dNumber[i]<<"\t";
cout<<endl<<"the middle of them is "<<middle(dNumber)<<endl;
cout<<"\nThe numbers are ";
for( int i=0;i<3;i++)
cout<<uNumber[i]<<"\t";
cout<<endl<<"the middle of them is "<<middle(uNumber)<<endl;
return 0;
}
//StudybarCommentEnd
3.函数重载参数个数不同的模仿
设计一个做多个数相加的函数,并完成函数重载
举例
int myadd(int a,int b)
{
return a+b;
}
int myadd(int a,int b,int c)
{
return a+b+c;
}
//函数重载参数个数不同的模仿
//设计一个做多个数相加的函数,并完成函数重载
//
// 举例
//
//int myadd(int a,int b)
//{
// return a+b;
//}
//
//int myadd(int a,int b,int c)
//{
// return a+b+c;
//}
#include <iostream>
using namespace std;
int myadd(int a,int b)
{
return a+b;
}
int myadd(int a,int b,int c)
{
return a+b+c;
}
//StudybarCommentBegin
main()
{
cout<<myadd(2,3)<<"\n";
cout<<myadd(2,3,4);
}
//StudybarCommentEnd
4、函数重载,读后缀代码,这里有数组做参数的重载
设计一个做多个数相加的函数,并完成函数重载
举例
int myadd(int a,int b)
{
return a+b;
}
int myadd(int a,int b,int c)
{
return a+b+c;
}
测试输入
2
输出
8
测试输入
3
输出
17
//设计一个做多个数相加的函数,并完成函数重载
//
//
//
//举例
//
//
//
//int myadd(int a,int b)
//{
// return a+b;
//}
//
//int myadd(int a,int b,int c)
//{
// return a+b+c;
//}
//
//
//
//测试输入
//
//2
//
//输出
//
//8
//
//测试输入
//
//3
//
//输出
//
//17
#include <iostream>
using namespace std;
int myadd(int a, int b)
{
return a + b;
}
int myadd(int a, int b,int c)
{
return a + b + c;
}
int myadd(int a[],int n)
{
int sum = 0;
int i = 0;
for(i = 0 ; i < n ;i++)
{
sum += a[i];
}
return sum;
}
//StudybarCommentBegin
main()
{
int a[20],n,i;
cin>>n;
if(n==2)
cout<<myadd(5,3);
else if (n==3)
cout<<myadd(5,3,9);
else if(n>3)
{
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<myadd(a,n);
}
}
//StudybarCommentEnd
5、时间类-构造函数重载
设置Cmytime类。
具有三个成员函数
构造函数
Set(int h,int m,int s)
Show()
输入 23 25 38
输出:
3:2:1
23:25:38
0:0:0
5:0:0
//设置Cmytime类。
//
//具有三个成员函数
//
//构造函数
//
//Set(int h,int m,int s)
//
//Show()
//
//输入 23 25 38
//
//输出:
//
//3:2:1
//
//23:25:38
//
//0:0:0
//
//5:0:0
#include <iostream>
using namespace std;
class Cmytime
{
private:
int hour;
int minute;
int second;
public:
Cmytime():hour(0),minute(0),second(0){};
Cmytime(int h,int m, int s):hour(h),minute(m),second(s){};
Cmytime(int h):hour(h),minute(0),second(0){};
void Set(int h ,int m ,int s)
{
hour = h;
minute = m;
second = s;
}
void Show()
{
cout <<hour <<":" <<minute <<":"<<second;
}
};
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1(3,2,1),t2,t3(5);
t1.Show();
cout<<"\n";
t1.Set(h,m,s);
t1.Show();
cout<<"\n";
t2.Show();
cout<<"\n";
t3.Show();
return 0;
}
//StudybarCommentEnd
6、时间类-构造函数-默认参数值实现
设置Cmytime类。
具有三个成员函数
构造函数
Set(int h,int m,int s)
Show()
输入 23 25 38
输出:
3:2:1
23:25:38
0:0:0
5:0:0
//设置Cmytime类。
//
//具有三个成员函数
//
//构造函数
//
//Set(int h,int m,int s)
//
//Show()
//
//输入 23 25 38
//
//输出:
//
//3:2:1
//
//23:25:38
//
//0:0:0
//
//5:0:0
#include <iostream>
using namespace std;
class Cmytime
{
private:
int hour;
int minute;
int second;
public:
Cmytime(int h,int m,int s):hour(h),minute(m),second(s){}
Cmytime():hour(0),minute(0),second(0){}
Cmytime(int h):hour(h),minute(0), second(0){}
void Show()
{
cout <<hour <<":" <<minute <<":"<<second;
}
void Set(int h,int m,int s)
{
hour = h;
minute = m;
second = s;
}
};
//StudybarCommentBegin
int main(void) {
int h,m,s;
cin>>h>>m>>s;
Cmytime t1(3,2,1),t2,t3(5);
t1.Show();
cout<<"\n";
t1.Set(h,m,s);
t1.Show();
cout<<"\n";
t2.Show();
cout<<"\n";
t3.Show();
return 0;
}
//StudybarCommentEnd
7、引用作为函数参数
利用“引用形参”实现两个变量的值互换
例如输入 3 5
输出 5 3
//利用“引用形参”实现两个变量的值互换
//
//例如输入 3 5
//
//输出 5 3
#include <iostream>
using namespace std;
void swap(int &i,int &j)
{
int temp = i;
i = j;
j = temp;
}
//StudybarCommentBegin
#include <iostream>
using namespace std;
void swap(int &,int &);
int main( )
{
int i,j;
cin >> i >>j ;
swap(i,j);
cout<<i<<" "<<j<<endl;
return 0;
}
//StudybarCommentEnd
8、 复习——指针作为函数参数
利用“引用形参”实现两个变量的值互换
例如输入 3 5
输出 5 3
//利用“引用形参”实现两个变量的值互换
//
//例如输入 3 5
//
//输出 5 3
#include <iostream>
using namespace std;
void swap(int *i, int *j)
{
int temp = *i;
*i = *j;
*j = temp;
}
//StudybarCommentBegin
#include <iostream>
using namespace std;
void swap(int *,int *);
int main( )
{
int i,j;
cin >> i >>j ;
swap(&i,&j);
cout<<i<<" "<<j<<endl;
return 0;
}
//StudybarCommentEnd