java 操作符 重载_操作符重载

类中使用操作符,希望明了易懂,可以通过重载操作符,使自定义类使用操作符像一般类使用一样简洁。如何自定义操作符?其实操作符的重载相当于函数的重载(代码中标注)。

下面以“+”的重载为例子。

#ifndef STUDENT_H

#define STUDENT_H

#include

using namespace std;

class student

{

public:

student();

student(int num, string name);

void print();

student operator+(const student& st);//重载+操作符,形参为const引用,系统不再调用复制构造函数,直接使用传入源参,提高效率。

virtual ~student();

protected:

private:

int number;

string name;

};

#endif // STUDENT_H#include "../include/student.h"

student::student()

{

number = 0;

name = "";

}

student::student(int num, string name)

{

//ctor

number = num;

this->name = name;

}

void student::print()

{

cout<

cout<

cout<

}

student student::operator+(const student& st)

{

student st1;

st1.name = name + st.name;

st1.number = number + st.number;

return st1;

}

student::~student()

{

//dtor

}

#include

#include "include/student.h"

using namespace std;

int main()

{

student st1(1,"lee");

student st2(2,"boy");

student st3;

st3 = st1 + st2;/*操作符+的使用就像一般调用函数一样。此处还可以写成st3 = st1.operator+(st2); st1.operator+(st2)的返回值为student的一个副本,也就是st1 + st2为返回的副本。多数时操作符重载会使用返回引用,也就是无需复制构造的过程(但被引用的对象会被修改,如果这里使用返回引用,则只能使用修改当前对象st1的方法:student& student::operator+(const student& st)

{

name = name + st.name;

number = number + st.number;

return *this;

})。*/

st2.print();

st3.print();

return 0;

}

0_1324558321c3De.gif

使用返回引用的方法:

0_1324559779oo5d.gif

其他操作符重载

.h文件

#pragma once

#include

using namespace std;

class Book

{

friend ostream& operator<

friend istream& operator>>(istream &ist, Book &bk);

public:

Book(void);

Book(string str, int id);

~Book(void);

public:

//重载声明

Book& operator=(const Book &bk);

Book& operator+=(const Book &bk);

Book operator+(const Book &bk);

Book& operator[](int num);

//int只是用来区别前加还是后加,放在哪个里边都可以

Book operator++(); //后++

Book& operator++(int); //前++

private:

string m_sName;

int m_iId;

};

.cpp文件 部分代码省略

Book::Book(string str, int id):m_sName(str), m_iId(id)

{

}

Book& Book::operator=(const Book &bk)

{

this->m_sName = bk.m_sName;

this->m_iId = bk.m_iId;

return *this;

}

Book& Book::operator+=(const Book &bk)

{

this->m_sName += bk.m_sName;

this->m_iId += bk.m_iId;

return *this;

}

Book Book::operator+(const Book &bk)

{

Book t_Bk = *this;

t_Bk += bk;//调动+=,修改类时,只需修改+

return t_Bk;

}

Book& Book::operator[](int num)

{

return *(this+num); //下标操作就是一定量的偏移

}

//后++

Book Book::operator++()

{

Book temp = *this;

this->m_iId++;

return temp;

}

//前++

Book& Book::operator++(int)

{

this->m_iId++;

return *this;

}

主程序:

// OverOperator.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "Book.h"

#include

ostream& operator<

istream& operator>>(istream &ist, Book &bk);

int _tmain(int argc, _TCHAR* argv[])

{

//Book book1("lee",566);

//Book book2 = book1;//=重载

//Book book3 = book2;

//book3 += book1;//+=重载

//Book book4 = book1 + book2; //+重载

//流重载测试

//cout << book2 <

//cout << book3 <

//cout << book4 <

/*Book bk1,bk2;

cin >> bk1 >> bk2;

cout << bk1 << bk2;*/

//[]重载测试

/*Book *bk = new Book[5];

cin >> bk[1];

cin >> bk[2];

cout << bk[2] << bk[1];*/

//++运算测试

Book book1("lee",566);

//运算是从右往左运行的:名字:leeID号码:568名字:leeID号码:567名字:leeID号码:568

cout << book1 << book1++ << ++book1;

system("pause");

return 0;

}

/*

* 返回类型不是类的类型,无法将重载操作符函数作为类的成员函数,因此使用友元方式。

* 对于<

* 建议只有=,+=定义为类成员,其他操作符用友元实现

*/

ostream& operator<

{

ost << "名字:" << bk.m_sName << "ID号码:" << bk.m_iId ;

return ost;

}

istream& operator>>(istream &ist, Book &bk)

{

ist >> bk.m_sName >> bk.m_iId;

return ist;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值