深入理解运算符的重载

*运算符重载:

1.运算符重载就是赋予已有的运算符多重含义,即多种功能。

2.运算符重载的目的:通过运算符的重载即重新定义使得其能够用于特定类的对象执行特定的功能。

3.对于运算符的重载首先要关心的就是那些运算符能够重载,那些不能重载;

能够重载的运算符:

(1).算术运算符:+,-,*,/,%,++,--;

(2)位操作运算符:&,|,^,~,<<,>>;

(3)逻辑运算符:!,&&,||;

(4)比较运算符:>,<,>=,<=,==,!=;

(5)赋值运算符:=,+=,-=,*=,/=,%=,&=,|=,^=,~=,<<=,>>=;

(6)其他运算符:[],(),->,’,new,delete,new[],delete[],->*。

不能重载的运算符:

.,.*,::,?:

4.运算符重载后不影响其结合性和优先级,切不改变其操作数的个数及语法结构。(即运算符重载后单目运算符仍旧为单目运算符,双目运算符仍旧为单目运算符)

5.重载运算符的限制条件:

(1).不能妄造新的运算符,必须把重载运算符限制在C++语言中已有的运算符范围内允许重载的运算符内。

(2).重载运算符的四个“不能改变”:

a.不能改变运算符操作数的个数;

b.不能改变运算符原有的优先级;

c.不能改变运算符原有的结合性;

d.不能改变运算符原有语法结构;

6.运算符重载必须遵循的原则:

运算符重载可以使得程序更加简洁,使表达式更加直观,增强可读性,但是不可过多的使用,否则会适得其反。

(1).重载的运算符含义必须清楚:这个程序中,加法(+)运算用于Time的对象,含义不清,所以给Time类不能重载运算符+。

Eg:class Time
{
public:
 Time()
  { hours = minutes = seconds = 0; }
  Time(int h, int m, int s)
  {
   hours = h; minutes = m; seconds = s;
  }
private:
 int hours, minutes, seconds;
};
Time t1(8, 10, 20), t2(9, 15, 30), t3;
     t3 = t1 + t2;

(2).重载运算符不能有二义性:

(3)在定义运算符时必须含义准确,用法确定,不可含糊不清,在同一个地方一种重载运算符只能拥有一种理解。*运算符重载函数的两种形式:

1.重载为类的成员函数:

Eg:

#include<iostream.h>

class complex
{
 public:
 complex()
 {
       real = image = 0;
 }
  complex(double r, double i)
 {
       real = r; image = i;
 }
 complex operator+(const complex&c);
 complex operator-(const complex&c);
 complex operator*(const complex&c);
 complex operator/(const complex&c);
 friend void printf(const complex&c);
private:
 double real, image;
};
inline complex complex::operator+(const complex& c)
{
 return complex(real + c.real, image + c.image);
}
inline complex complex::operator-(const complex&c)
{
 return complex(real - c.real, image - c.image);
}
inline complex complex::operator*(const complex&c)
{
 return complex(real*c.real - image*c.image, real*c.image + image*c.real);
}
inline complex complex::operator/(const complex&c)
{
 return complex((real*c.real + image*c.image) / (c.real*c.real+c.image*c.image),
  (image*c.real - real*c.image) / (c.real*c.real + c.image*c.image));
}

void print(const complex &c)
{
 if (c.image < 0)
  cout << c.real << c.image << ‘i‘;
 else
  cout << c.real << ‘+‘ << c.image << ‘i‘;
}

. . . . . . . . . . . . . .  (主函数)

2.重载为类的友员函数:

Eg:

#include<iostream.h>

class complex
{
public:
 complex()
 {
  real = image = 0;
 }
 complex(double r, double i)
 {
  real = r; image = i;
 }
 friend complex operator+(const complex&c1,const complex&c2);
 friend complex operator-(const complex&c1, const complex&c2);
 friend complex operator*(const complex&c1,const complex&c2);
 friend complex operator/(const complex&c1,const complex&c2);
 friend void printf(const complex&c);
private:
 double real, image;
};
inline complex complex::operator+(const complex& c1,const complex&c2)
{
 return complex(c1.real + c2.real,c1.image + c2.image);
}
inline complex complex::operator-(const complex&c1,const complex&c2)
{
 return complex(c1.real - c2.real, c1.image - c2.image);
}
inline complex complex::operator*(const complex&c1,const complex&c2)
{
 return complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image +c1.image*c2.real);
}
inline complex complex::operator/(const complex&c1,const complex&c2)
{
 return complex((c1.real*c2.real + c1.image*c2.image) / (c2.real*c2.real+c2.image*c2.image),
  (c1.image*c2.real - c1.real*c2.image) / (c2.real*c2.real + c2.image*c2.image));
}

void print(const complex &c)
{
 if (c.image < 0)
  cout << c.real << c.image << ‘i‘;
 else
  cout << c.real << ‘+‘ << c.image << ‘i‘;
}

. . . . . . .  . . . . . . . . .(主函数)

时间: 2024-08-06 20:08:21

深入理解运算符的重载的相关文章

C++ 运算符的重载

#include <iostream> #include <stdio.h> using namespace std; class Complex //复数类 { public: double real;//实数 double imag;//虚数 Complex(double real=0,double imag=0) { this->real=real; this->imag=imag; } }; Complex operator+(Complex com1,Comp

C++中关于指针运算符-&gt;的重载问题

#include<iostream>using namespace std;struct date{ int year; int month; int day;};struct Person{ string name; int age; bool gender; double salary; date birth; Person() {  cout<<"创建persond对象"<<this<<endl;  age=10; } ~Perso

运算符的重载(复数的相关运算)

运算符的重载实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该重载的运算符时调用此函数.这个函数叫做运算符重载函数,通常为类的成员函数.    定义运算符重载函数的一般格式: 返回值类型 类名::operator重载的运算符(参数表) {--} operator是关键字,它与重载的运算符一起构成函数名.因函数名的特殊性,C++编译器可以将这类函数识别出来. 具体的加减乘除等代码运算如下: #include<iostream> #include<string>

对下标运算符[]和函数调用运算符()的重载

一.对下标运算符[]的重载: 重载的格式:返回类型 类名 operator [](参数): #include<iostream> using namespace std; class A { public: A(int n); ~A(); int & operator [](int n);//返回类型为int & 引用类型 private: int *p; int size; }; int & A::operator[](int n){ return p[n];//返回

自增自减运算符的重载(强制类型转换运算符重载)

前置运算符重载为一元运算符,后置运算符重载为二元运算符. Operator int() { return n; } int作为一个强制类型转换运算符被重载, Demo s; (int)s;       //等效于s.int(): 强制类型转换运算符重载时, 不能写返回值类型 实际上其返回值类型----强制类型转换运算符代表的类型 只能作为成员函数,不能作为友元函数或普通函数 转换构造函数和类型转换运算符有一个共同的功能:当需要的时候,编译系统会自动调用这些函数,建立一个无名的临时对象(或临时变量

【C++】一些基本的运算符的重载

// 实现运算符的重载 #include <iostream> using namespace std; class Int { public: Int(int i = 0) :m(i) { cout << "constructed function" << endl; } ~Int() { cout << "destructor" << endl; } public: Int operator+(cons

结构体类型快速驱魔运算及运算符的重载

下面得到这段代码可以用在很多地方:只需要自己修改下接Ok. 1 struct Matrix 2 { 3 long long mat[N][N]; 4 Matrix operator*(const Matrix m)const//定义矩阵乘法的运算符* 5 { 6 Matrix tmp; 7 for(int i = 0;i < n;i++) 8 { 9 for(int j = 0;j < n;j++) 10 { 11 tmp.mat[i][j] = 0; 12 for(int k = 0;k &

【c++】实现运算符的重载

// 实现运算符的重载 #include <iostream> using namespace std; class Int { public: Int(int i = 0) :m(i) { cout << "constructed function" << endl; } ~Int() { cout << "destructor" << endl; } public: Int operator+(cons

C++ 运算符的重载(转载自http://blog.csdn.net/insistgogo/article/details/6626952)

(转载自http://blog.csdn.net/insistgogo/article/details/6626952) 什么是运算符的重载? 运算符与类结合,产生新的含义. 为什么要引入运算符重载? 作用:为了实现类的多态性(多态是指一个函数名有多种含义) 怎么实现运算符的重载? 方式:类的成员函数 或 友元函数(类外的普通函数) 规则:不能重载的运算符有 .  和 .* 和 ?: 和 ::  和 sizeof 友元函数和成员函数的使用场合:一般情况下,建议一元运算符使用成员函数,二元运算符使