//C++允許功能相近的函數在相同的範圍內以相同函數名聲明,從而形成重載。方便使用,便於記憶。//int add(int x, int y);//float add(float x, float y);形參類型不同//int add(int x, int y);//int add(int x, int y, int z);形參個數不同//重載函數的形參必須不同: 個數不同或類型不同。//編譯器將根據實參和形參的類型及個數的首選來選擇調用哪一個函數。//1. 編譯器不以形參名來區分;//2. 編譯器不以傳回值來區分;//3. 不要將不同功能的函式宣告為重載函數,以免出現調用結果的誤解、混淆。#include<iostream.h>struct complex{double real;double imaginary;};int add(int m, int n){ return m+n; }double add(double x, double y){ return x+y; }complex add(complex c1, complex c2){ complex c;c.real=c1.real+c2.real;c.imaginary=c1.imaginary+c2.imaginary;return c;}int main(void){int m, n;double x, y;complex c1, c2, c3;int add(int m, int n);double add(double x, double y);complex add(complex c1, complex c2);cout<<"Enter two integer: ";cin>>m>>n;cout<<"integer "<<m<<'+'<<n<<"="<<add(m,n)<<endl;cout<<"Enter two real number: ";cin>>x>>y;cout<<"real number "<<x<<'+'<<y<<"= "<<add(x,y) <<endl;cout<<"Enter the first complex number: ";cin>>c1.real>>c1.imaginary;cout<<"Enter the second complex number: ";cin>>c2.real>>c2.imaginary;c3=add(c1,c2);cout<<"complex number (" <<c1.real<< ',' << c1.imaginary <<")+("<<c2.real<<',' <<c2.imaginary<<")=("<<c3.real<<',' <<c3.imaginary<<")\n"; return 0;}