一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

所谓重载,就是重新赋予新的含义,函数重载是对一个已有的函数赋予新的含义。

运算符重载的方法是定义一个重载运算符的函数,在需要执行被重载的运算符时,系统就自动调用该运算符。也就是说,运算符重载是通过定义函数实现的,运算符重载实质是是函数的重载。

实例1:复数加法

 1 #include <iostream>
 2 
 3 using namespace std; 
 4 class Complex//定义基类
 5 {
 6 public:
 7     Complex();//构造器 
 8     Complex(double r,double i);//重载函数,输入参数不同,功能不同 
 9     Complex Complex_add(Complex &d);//加入一个方法,实现复数加法,Complex指其类型 
10     void print();
11     
12 private:
13     double real;
14     double imag;
15 }; 
16 
17 Complex::Complex()//构造函数实现 
18 {
19     real = 0;
20     imag = 0;
21 } 
22 Complex::Complex(double r,double i)//重载函数实现 
23 {
24     real = r;
25     imag = i;
26 } 
27 
28 Complex Complex::Complex_add(Complex &d)//Complex &d为定义基类对象d(被加数) complex_add为基类Complex方法 
29 {                                       //第二个Complex为继承基类的加数对象 
30     Complex c;//定义基类对象c           //第一个Complex为其类型 
31     
32     c.real = real + d.real;
33     c.imag = imag + d.imag;
34     
35     return c;
36 }
37 void Complex::print()
38 {
39     cout << "(" << real << ", " << imag << "i)\n";
40 } 
41 
42 int main()
43 {
44     Complex c1(3,4),c2(5,-10),c3;//定义三个对象c1、c2、c3及其重载函数参数值 
45     c3 = c1.Complex_add(c2);
46     
47     cout << "c1 = ";
48     c1.print();
49     cout << "c2 = ";
50     c2.print();
51     cout << "c1 + c2 = ";
52     c3.print();
53     
54     return 0;
55 }

实例2:复数加法2

 1 #include <iostream>
 2 
 3 using namespace std;
 4 class Complex//定义基类
 5 {
 6 public:
 7     Complex();//构造器 
 8     Complex(double r,double i);//重载函数,输入参数不同,功能不同 
 9     friend Complex operator+(Complex &c,Complex &d);//加入一个方法,实现复数加法,Complex指其类型 
10     void print();
11     
12 private:
13     double real;
14     double imag;
15 }; 
16 
17 Complex::Complex()//构造函数实现 
18 {
19     real = 0;
20     imag = 0;
21 } 
22 Complex::Complex(double r,double i)//重载函数实现 
23 {
24     real = r;
25     imag = i;
26 } 
27 //注意:这里作为友元函数,不属于Complex,记得别写:: 
28 Complex operator+(Complex &c,Complex &d)//Complex &d为定义基类对象d(被加数) complex_add为基类Complex方法 
29 {
30     return Complex(c.real + d.real,c.imag + d.imag);
31 }
32 void Complex::print()
33 {
34     cout << "(" << real << ", " << imag << "i)\n";
35 } 
36 
37 int main()
38 {
39     Complex c1(3,4),c2(5,-10),c3;//定义三个对象c1、c2、c3及其重载函数参数值 
40     c3 = operator+(c1,c2);
41     
42     cout << "c1 = ";
43     c1.print();
44     cout << "c2 = ";
45     c2.print();
46     cout << "c1 + c2 = ";
47     c3.print();
48     
49     return 0;
50 }

作业:分数加减乘除的操作符重载

  1 #include <iostream>
  2 #include <string>
  3 #include <math.h> 
  4 
  5 using namespace std;
  6 class Rational//定义基类
  7 {
  8 public:
  9     Rational(int num,int denom); //构造器 num = 分子,denom = 分母 
 10     
 11     Rational operator+(Rational rhs);// rhs == right hand side(右手边参数) 
 12     Rational operator-(Rational rhs);
 13     Rational operator*(Rational rhs);
 14     Rational operator/(Rational rhs);
 15     
 16     void print();//打印 
 17     
 18 private:
 19     void normalize();//负责对分数的简化处理
 20     
 21     int numerator; //分子
 22     int denominator; //分母 
 23 }; 
 24 
 25 Rational::Rational(int num,int denom)//构造函数实现 
 26 {
 27     numerator = num;
 28     denominator = denom;
 29     
 30     normalize(); 
 31 } 
 32 //normalize()对分数进行简化操作包括:
 33 //1.只允许分子为负数,如果分母为负数则把负数挪到分子部分,如1/-2==-1/2 
 34 //2.利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 => 1/5 
 35 
 36 void Rational::normalize()
 37 {
 38     //确保分母为正
 39     if( denominator < 0) 
 40     {
 41         numerator = -numerator;
 42         denominator = -denominator;
 43     }
 44     //欧几里德算法 
 45     int a = abs(numerator);
 46     int b = abs(denominator);
 47     
 48     //求出最大公约数
 49     while(b>0)
 50     {
 51         int t = a % b;//取余 
 52         a = b;
 53         b = t;
 54     }
 55      
 56      //分子、分母分别除以最大公约数得到最简化分数
 57      numerator /= a;
 58      denominator /= a; 
 59 }
 60 //a   c   a*d   c*b   a*d + c*d
 61 //- + - = --- + --- = ----------
 62 //b   d   b*d   b*d      b*d
 63 Rational Rational::operator+(Rational rhs)//分数的加运算 
 64 {
 65     int a = numerator;
 66     int b = denominator;
 67     int c = rhs.numerator;
 68     int d = rhs.denominator;
 69     
 70     int e = a*b + c*d;
 71     int f = b*d;
 72     
 73     return Rational(e,f);
 74 }
 75 //a   c   a   -c
 76 //- - - = - + -- 
 77 //b   d   b   d 
 78 Rational Rational::operator-(Rational rhs)//分数的减运算 
 79 {
 80     rhs.numerator = -rhs.numerator; //被减数分子取负数 
 81     return operator+(rhs);
 82 }
 83 //a   c   a*c
 84 //- * - = --- 
 85 //b   d   b*d 
 86 Rational Rational::operator*(Rational rhs)//分数的乘运算
 87 {
 88     int a = numerator;
 89     int b = denominator;
 90     int c = rhs.numerator;
 91     int d = rhs.denominator;
 92     
 93     int e = a*c;
 94     int f = b*d;
 95     
 96     return Rational(e,f);
 97 }
 98 //a   c   a   d
 99 //- / - = - * - 
100 //b   d   b   c
101 Rational Rational::operator/(Rational rhs)//分数的除运算
102 {
103     //rhs的分子分母互换 
104     int t = rhs.numerator;
105     rhs.numerator = rhs.denominator;
106     rhs.denominator = t;
107     
108     return operator*(rhs);
109 }
110 void Rational::print() //打印最简分数 
111 {
112     if(numerator % denominator == 0)
113     cout << numerator / denominator;
114     else 
115     cout << numerator << "/" << denominator;
116 }
117 
118 int main()
119 {
120     Rational f1(2,16);//定义f1对象,且传入(2,16)参数 
121     Rational f2(7,8);
122     
123     //测试有理数加法运算 
124     Rational res = f1 + f2;//相当于res=f1.operator+(f2)
125     f1.print();
126     cout << " + ";
127     f2.print();
128     cout << " = ";
129     res.print();
130     cout << "\n"; 
131     
132     //测试有理数减法运算 
133     res = f1 - f2;
134     f1.print();
135     cout << " - ";
136     f2.print();
137     cout << " = ";
138     res.print();
139     cout << "\n"; 
140     
141     //测试有理数乘法运算 
142     res = f1 * f2;
143     f1.print();
144     cout << " * ";
145     f2.print();
146     cout << " = ";
147     res.print();
148     cout << "\n";
149     
150     //测试有理数除法运算 
151     res = f1 / f2;
152     f1.print();
153     cout << " / ";
154     f2.print();
155     cout << " = ";
156     res.print();
157     cout << "\n";
158     
159     return 0;
160 }
posted on 2023-08-18 14:56  一杯清酒邀明月  阅读(35)  评论(0编辑  收藏  举报