函数重载是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++,程序写的不是很好,欢迎大家指出可以改进的地方⊙▽⊙

posted on 2018-03-23 17:13  程序员大彬  阅读(112)  评论(0编辑  收藏  举报