操作符重载
来源:互联网 发布:dota2画质优化补丁 编辑:程序博客网 时间:2024/04/30 03:39
类中使用操作符,希望明了易懂,可以通过重载操作符,使自定义类使用操作符像一般类使用一样简洁。如何自定义操作符?其实操作符的重载相当于函数的重载(代码中标注)。
下面以“+”的重载为例子。
#ifndef STUDENT_H#define STUDENT_H#include <iostream>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<<"number:" << number<<endl; cout<<"name:" << name<<endl; cout<<endl;}student student::operator+(const student& st){ student st1; st1.name = name + st.name; st1.number = number + st.number; return st1;}student::~student(){ //dtor}
#include <iostream>#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;}
使用返回引用的方法:
其他操作符重载
.h文件
#pragma once#include <iostream>using namespace std;class Book{friend ostream& operator<<(ostream &ost,const Book &bk); //声明友元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 <string>ostream& operator<<(ostream &ost,const Book &bk);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 <<endl; //<<重载//cout << book3 <<endl;//cout << book4 <<endl;/*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号码:568cout << book1 << book1++ << ++book1; system("pause");return 0;}/* * 返回类型不是类的类型,无法将重载操作符函数作为类的成员函数,因此使用友元方式。 * 对于<<,IO标准库中原先具有两个操作数,所以形参也要有两个操作数保持数量一直。 * 建议只有=,+=定义为类成员,其他操作符用友元实现 */ostream& operator<<(ostream &ost, const Book &bk){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;}
- c++重载操作符
- C++ 操作符重载
- 操作符重载浅析
- 操作符重载
- 10.8 操作符重载
- 操作符重载
- 操作符重载手册
- C#操作符重载
- 操作符重载手册
- 操作符重载手册
- 六操作符重载
- 操作符的重载
- C#操作符重载
- 多态,操作符重载
- 操作符重载集锦
- 重载操作符
- 操作符重载
- 操作符重载手册
- zz 保存matlab数据
- VS2008 VC++.net 打包发布静态dll设置
- POJ-1338(ugly number,STL-pair的应用)
- oracle 常用函数介绍
- shell基础整理
- 操作符重载
- CInternetSession 类
- LINUX下各个目录详细介绍
- 整理shell基础
- SwfObjects在页面中嵌入flash(SWF)
- asp.Net下图片防止盗链的原理
- linux: shell基础
- HDU4149 Magic Potion 异或运算(菜鸟杯)
- C# FileStream&StreamWriter