码迷,mamicode.com
首页 > 其他好文 > 详细

运算符重载

时间:2019-11-30 17:45:32      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:改变   str   方式   否则   return   point   private   字符数组   real   

1.  双目运算符类成员函数重载

观察下列程序,掌握双目运算符的类成员函数重载方式。

#include<iostream>
using namespace std;
class complex {
    double real, imag;
public:
    complex(double r = 0, double i = 0) { real = r; imag = i; }
    complex operator + (complex&);  //运算符重载的成员函数原型
    //运算符结果类型 operator @(参数表列);  其中,@表示运算符
    void print();
};
/*运算符重载实现:
函数返回类型 类名::operator @(参数表列)
{
  //代码
}*/
complex  complex::operator+(complex& c) {  //operator+ 是类complex的成员函数
    complex sum;
    sum.real = real + c.real;
    sum.imag = imag + c.imag;
    return sum;
}
void complex::print() {
    cout << "real=" << real << endl;
    cout << "imag=" << imag << endl;
}
void main() {
    complex  a(5.0, 6.0), b(4.0, 5.0);
    complex  c;
    c = a + b;
    //c=5+a;
    c.print();
}

说明:+是双目运算符,成员函数operator+(complex &c)的参数c是+的右操作数。在main函数中,c=a+b等价c=a.operator+(b);

编写程序:(运算符重载的成员函数)

定义people类,具有数据成员name(姓名)、id(身份证号),均为字符数组或string类型。定义成员函数:

(1)带参构造函数,实现给数据成员赋初值;

(2)重载运算符==,实现对象比较(如果两个people类对象的身份证id相等,则结果为1,否则结果为0)。

在main函数中,通过if语句测试程序正确性。

#include <iostream>
#include <cstring>
using namespace std;
class people
{
private:
    string name;
    string id;
public:
    people(string name_1, string id_1)
    {
        name == name_1;
        id = id_1;
    }
    bool operator==(people& p);//重载的成员函数声明
};
bool people::operator==(people& p)
{
    if (id == p.id)
        return 1;
    return 0;
}
int main()
{
    people p1("HUAHUA", "123432"), p2("HUGFFY", "234143");
    cout << "nanaa:" << "123432" << endl;
    cout << "lili:" << "234143" << endl;
    if (p1 == p2)
    {
        cout << "ID相等." << endl;
    }
    cout << "ID不相等!" << endl;

    return 0;
}

 

2、双目运算符的非成员函数的重载

 

class  complex;//需要类的原型声明 VC++编辑器需要
complex  operator+(int, complex);//需要友元普通函数声明VC++编辑器需要
class complex {
private:
    double real, imag;
public:
    complex(double r = 0, double i = 0) { real = r; imag = i; }
    friend  complex operator + (int, complex);  //运算符重载普通友元函数原型
    //函数返回值类型 friend operator @(参数表列); 其中@表示运算符
    void print();
};
/*运算符重载普通友元函数实现部分格式:
函数返回类型 operator @(参数表列)  {   //代码  } */
complex operator +(int c, complex d) {
    complex sum;
    sum.real = c + d.real;
    sum.imag = d.imag;
    return sum;
}

 

class people
{
private:
    string name;
    string id;
public:
    people(string name_1, string id_1)
    {
        name == name_1;
        id = id_1;
    }
    friend bool operator==(people& p3, people& p);
};
bool operator==(people& p3, people& p)
{
    if (p3.id == p.id)
        return 1;
    return 0;
}

3、单目运算符的重载

#include<iostream>
using namespace std;
class complex {
private:
    double real, imag;
public:
    complex(double r = 0, double i = 0) { real = r; imag = i; }
    complex& operator++();  //前缀形式原型
    complex  operator++(int);  //后缀形式原型
    void print();
};
complex& complex::operator++() {//前缀形式定义
    ++real;
    ++imag;
    return (*this);
}
complex complex::operator++(int c) {//后缀形式定义
    complex temp = *this;
    ++real;
    ++imag;
    return temp;
}
void complex::print() {
    cout << "real=" << real << endl;
    cout << "imag=" << imag << endl;
}
void main() {
    complex a(5.0, 6.0), b(4.0, 5.0);
    complex c, d, f, g;
    f = ++c; //等价形式c.operator++();
    g = d++; //等价形式d.operator++(0);
    f.print();
    g.print();
}

注意:前缀 ++ 函数返回的是*this,后缀++返回的是一个局部对象。

自增++运算符的重载

++前缀运算符的重载原型(++a,a为对象)

complex& operator++(); //前缀形式原型

++后缀运算符重载原型(a++,a为一个对象)

complex operator++(int); //后缀形式  原型

 

 例:编程:

定义Point类,有坐标_x,_y两个私有数据成员。

对Point类重载++(自增)、--(自减)运算符,实现对坐标值的改变;

定义成员函数print,输出成员_x,_y的值。用main函数验证结果。

#include <iostream>
using namespace std;

class Point {
public:
    Point() { }
    Point(int _x, int _y);
    ~Point() { }
    Point& operator++();//对应于a++ 
    Point& operator--();//对应于a-- 
    Point operator++(int);//对应于a++ 
    Point operator--(int);//对应于a-- 
    void print();
private:
    int x;
    int y;
};

Point::Point(int _x, int _y) {
    x = _x;
    y = _y;
}

Point& Point::operator++() {//++a
    x++;
    y++;
    return *this;
}

Point Point::operator++(int) {//a++
    Point p=* this;
    ++(*this);
    return p;
}

Point& Point::operator--() {//--a
    x--;
    y--;
    return *this;
}

Point Point::operator--(int) {//a--
    Point p=*this;
    --(*this);
    return p;
}
void Point::print()
{
    cout << "(" << x << "," << y << ")" << endl;
}
int main()
{
    Point a(3,5);
    a.print();
    cout << endl;
    (a++).print();//测试后置++
    a.print();
    cout << endl;
    (++a).print();//测试前置++
    a.print();
    cout << endl;
    (a--).print();//测试后置--
    a.print();
    cout << endl;
    (--a).print();//测试前置--
    a.print();
    return 0;
}

4编程:定义一个矩阵类Matrix

class Matrix  {  //定义Matrix类

public:

   int &operator [] (  int i  );   //重载下标运算符[]

   void input();               //输入数据函数

   void display();             //输出数据函数

private:

   int mat[5];

};

input函数实现对数据成员mat数组的输入,display函数实现对数据成员mat的输出。重载运算符[ ],取得数据成员mat指定下标的元素值,比如:

Matrix m;

   cout<<m[2];  //取得数据成员mat中,下标为2的值

#include<iostream>
#include<iostream>
using namespace std;

class Matrix {  //定义Matrix类
public:
    int& operator [] (int i);   //重载下标运算符[]
    void input();               //输入数据函数
    void display();             //输出数据函数
private:
    int mat[5];
};
void Matrix::input() {
    for (int i = 0; i < 5; i++)
    {
        cout << "输入第" << i << "个元素:";
        cin >> mat[i];
    }
}
void Matrix::display() {
    for (int i = 0; i < 5; i++)
    {
        cout << mat[i] << " ";
    }
}
int& Matrix::operator[](int i) {
    return  this->mat[i];
}
void main() {
    Matrix m;
    m.input();
    m.display();
    cout << "Mat[2]=";
    cout << m[2];
}

总结:

1. 将复数的加减法这样的运算重载为类的成员函数,除了使用了关键字operator外,运算符重载成员函数与类的普通成员函数没有什么区别。

2. 使用友元函数。即运算符重载为非成员函数

3. 一般在C++中只要定义非成员函数,就使用友元函数。(可以访问私有成员)

4. 单目运算符分为前置++和后置++。所以规定:对于前置单目运算符,重载函数没有形参;对于后置单目运算符,重载函数有一个int型形参。

运算符重载

标签:改变   str   方式   否则   return   point   private   字符数组   real   

原文地址:https://www.cnblogs.com/nanaa/p/11963298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!