函数重载是C++的重要特性,它允许我们在参数列表不同的前提下定义同名函数。在函数调用时将编译器将根据函数名与调用时的参数类型决定调用的具体函数。
下面通过一个简单的例子了解一下函数重载。
/*************************************************
Author: Tyson
Date:2018-03-22
Description:对乘法的重载 ,开发环境Dev-c++ 5.10
**************************************************/
#include <iostream>
#include<ctime> //调用time()所需头文件
#include<cstdlib> //调用rand(),srand()所需头文件
using namespace std;
const int ROW = 10;//二维数组的行数
const int COLUMN = 10;//二维数组的列
//整型数乘法
int product(int a, int b) {
return a*b;
}
//浮点数乘法
float product(float a, float b) {
return a*b;
}
//复数乘法
int *product(int a, int b, int c, int d) { //a+b*i c+d*i
int *num = new int[2];
num[0]= a*c - b*d;
num[1] = a*d + b*c;
return num;
}
//矩阵乘法
int **product(int str1[][COLUMN], int str2[][COLUMN]) {
int** str = new int*[ROW];//str是指向指针的指针,声明二维数组
for(int i=0;i<ROW;i++) {
str[i] = new int[COLUMN];
}
int matrix_product;
for(int i=0;i<ROW;i++) {
for(int j=0;j<COLUMN;j++) {
matrix_product=0;
for(int k=0;k<COLUMN;k++) {
matrix_product += str1[i][k] * str2[k][j];
}
str[i][j] = matrix_product;
}
}
return str;
}
int main() {
srand(time(NULL)); //随机数发生器的初始化函数
int m, n, productInterger;
cout<<"please input tow integer: ";
cin>>m>>n;
productInterger = product(m, n);
cout<<"the product of two integer: "<<endl;
cout<<productInterger<<endl;
/**********************************************/
float p, q, floatProduct;
cout<<"please input tow float number: ";
cin>>p>>q;
floatProduct = product(p,q);
cout<<"the product of two float number:"<<endl;
cout<<floatProduct<<endl;
/**********************************************/
int *num = new int[2];//接收实部和虚部两部分
int a, b, c, d;
cout<<"please input tow complex number: ";//输入格式 a+b*i c+d*i
cin>>a>>b>>c>>d;
num = product(a, b, c, d);
cout<<"the product of two complex number:"<<endl;
cout<<num[0]<<"+"<<num[1]<<"i"<<endl<<endl;
/**********************************************/
cout<<"press any key to create two matrices."<<endl;
system("PAUSE"); //按下任意键继续
cout<<"Matrix1:"<<endl;
int str1[ROW][COLUMN];
int str2[ROW][COLUMN];
for(int i=0;i<ROW;i++) {
for(int j=0;j<COLUMN;j++) {
str1[i][j] = rand()%100;//随机数1-100,给数组赋值
cout<<str1[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
cout<<"Matrix2:"<<endl;
for(int i=0;i<ROW;i++) {
for(int j=0;j<COLUMN;j++) {
str2[i][j] = rand()%100;
cout<<str2[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
int** matrixProduct = new int*[ROW];//二维数组声明
for(int i=0;i<ROW;i++) {
matrixProduct[i] = new int[COLUMN];
}
matrixProduct = product(str1, str2);
cout<<"press any key to create the product of two matrices."<<endl;
system("PAUSE");
cout<<"the product of two matrices is:"<<endl;
for(int i=0;i<ROW;i++) {
for(int j=0;j<COLUMN;j++) {
cout<<matrixProduct[i][j]<<" ";//输出矩阵乘积的结果
}
cout<<endl;
}
system("PAUSE");
return 0;
}
初学C++,程序写的不是很好,欢迎大家指出可以改进的地方⊙▽⊙
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)