北大 C++ 4.1 运算符重载
2017-02-28 20:30
162 查看
运算符重载
C++预定义了表示对数据的运算
+,-,*,/,^,&,~,!,|,=,<<,>>,!=,==
只能用于基本的数据类型
整型,实型,字符型,逻辑型……
C++提供了数据抽象的手段:
用户自己定义数据类型 –类
·调用累的成员函数à操作它的对象
类的成员函数à操作对象时,很不方便
·在数学上两个复数可以直接进行+/-等运算
Vs. 在C++,直接将+,-用于复数是不行的。
于是我们需要使用运算符重载
目的:
对抽象数据类型也能够直接使用C++提供的运算符
程序更简洁
代码更容易理解
例1:
complex_a和complex_b是两个复数对象,我们希望直接complex_a + complex_b.
运算符重载
·对已有的运算符赋予多重的含义
使同一运算符作用于不同类型的数据时à不同类型的行为
目的:
扩展C++中提供的运算符的适用范围,以用于类所表示的抽象数据类型
运算符重载的实质是函数重载
返回值类型 operator 运算符(形参表)
{
……
}
在程序编译的时候:
把含 运算符的表达式à对运算符函数的调用
把运算符的操作数à运算符函数的参数
运算符被多次重载是,根据实参的类型决定调用哪个运算符函数,个数呢? 个数不用考虑,因为个数都一样。
运算符可以被重载成普通函数
也可以被重载成类的成员函数
1)运算符可以被重载成普通函数
class Complex {
public:
Complex(double r= 0.0, double i = 0.0){
real = r;
imaginary = i;
}
double real;
double imaginary;
};
Complex operator+(const Complex &a,const Complex &b){
return Complex(a.real+b.real,a.imaginary+b.imaginary);
}
Complex a(1,2),b(2,3),c;
c = a + b; //也可以写成 c = operator+(a,b);
重载为普通函数时,参数个数为运算符目数。
2)运算符重载为成员函数
此时a+b其实是a.operator+(b);所以重载为成员函数时,参数个数为运算符目数减一
#include
<iostream>
using
namespace std;
class
Complex {
public:
Complex(double
r = 0.0,
double
m = 0.0) :real(r),
imaginary(m) { }//constructor
Complex
operator+(const
Complex &);
Complex
operator-(const
Complex &);
public:
double real;
double imaginary;
};
Complex
Complex::operator+(const
Complex &
operand2) {
return
Complex(real +
operand2.real, imaginary +
operand2.imaginary);
}
Complex
Complex::operator-(const
Complex &
operand2) {
return
Complex(real -
operand2.real, imaginary -
operand2.imaginary);
}
int main() {
Complex x, y(4.3, 8.2), z(3.3, 1.1);
x
= y
+ z; //x = y.operator+(z);
cout
<< x.real
<< endl;
x
= y
- z; //x= y.operator-(z);
cout
<< x.real
<< endl;
system("pause");
return 0;
}
C++预定义了表示对数据的运算
+,-,*,/,^,&,~,!,|,=,<<,>>,!=,==
只能用于基本的数据类型
整型,实型,字符型,逻辑型……
C++提供了数据抽象的手段:
用户自己定义数据类型 –类
·调用累的成员函数à操作它的对象
类的成员函数à操作对象时,很不方便
·在数学上两个复数可以直接进行+/-等运算
Vs. 在C++,直接将+,-用于复数是不行的。
于是我们需要使用运算符重载
目的:
对抽象数据类型也能够直接使用C++提供的运算符
程序更简洁
代码更容易理解
例1:
complex_a和complex_b是两个复数对象,我们希望直接complex_a + complex_b.
运算符重载
·对已有的运算符赋予多重的含义
使同一运算符作用于不同类型的数据时à不同类型的行为
目的:
扩展C++中提供的运算符的适用范围,以用于类所表示的抽象数据类型
运算符重载的实质是函数重载
返回值类型 operator 运算符(形参表)
{
……
}
在程序编译的时候:
把含 运算符的表达式à对运算符函数的调用
把运算符的操作数à运算符函数的参数
运算符被多次重载是,根据实参的类型决定调用哪个运算符函数,个数呢? 个数不用考虑,因为个数都一样。
运算符可以被重载成普通函数
也可以被重载成类的成员函数
1)运算符可以被重载成普通函数
class Complex {
public:
Complex(double r= 0.0, double i = 0.0){
real = r;
imaginary = i;
}
double real;
double imaginary;
};
Complex operator+(const Complex &a,const Complex &b){
return Complex(a.real+b.real,a.imaginary+b.imaginary);
}
Complex a(1,2),b(2,3),c;
c = a + b; //也可以写成 c = operator+(a,b);
重载为普通函数时,参数个数为运算符目数。
2)运算符重载为成员函数
此时a+b其实是a.operator+(b);所以重载为成员函数时,参数个数为运算符目数减一
#include
<iostream>
using
namespace std;
class
Complex {
public:
Complex(double
r = 0.0,
double
m = 0.0) :real(r),
imaginary(m) { }//constructor
Complex
operator+(const
Complex &);
Complex
operator-(const
Complex &);
public:
double real;
double imaginary;
};
Complex
Complex::operator+(const
Complex &
operand2) {
return
Complex(real +
operand2.real, imaginary +
operand2.imaginary);
}
Complex
Complex::operator-(const
Complex &
operand2) {
return
Complex(real -
operand2.real, imaginary -
operand2.imaginary);
}
int main() {
Complex x, y(4.3, 8.2), z(3.3, 1.1);
x
= y
+ z; //x = y.operator+(z);
cout
<< x.real
<< endl;
x
= y
- z; //x= y.operator-(z);
cout
<< x.real
<< endl;
system("pause");
return 0;
}
相关文章推荐
- 北大 C++ 4.4 运算符重载实例
- 北大 C++ 4.5 流插入和流输出运算符重载
- 王老师 C++ 运算符重载 转换函数 第一讲
- [C++学习]矩阵运算符重载
- C++中运算符重载实例
- C++ 运算符重载
- C++ 运算符重载示例
- C++学习摘要之七:运算符重载
- [转] C++ 运算符重载 转换函数
- C++中的运算符重载
- C++编程实例-运算符重载
- c++中的运算符重载
- C++-运算符重载
- C++ Template在g++ 4.1和vc8/9之间的一些区别----强悍的VC
- C++类型转换运算符重载
- 老陈学 C++ 序列之一: 运算符重载
- 浅谈C++中运算符重载
- 王老师 C++ 运算符重载 转换函数 第三讲
- 阿牛哥C++笔记---4.1
- c++ 运算符重载