运算符重载编程基础
例如:
//全局函数 完成 +操作符 重载 Complex operator+(Complex &c1, Complex &c2)
//类成员函数 完成 -操作符 重载 Complex operator-(Complex &c2)
#include <iostream> using namespace std; class Complex { public: int a; int b; public: Complex(int a=0, int b=0) { this->a = a; this->b = b; } void printCom() { cout<<a<<" + " << b << "i" <<endl; } }; //2 函数名 升级 Complex operator+(Complex &c1, Complex &c2) { cout<<"12345上山 打老虎"<<endl; Complex tmp(c1.a + c2.a, c1.b+ c2.b); return tmp; // } void main() { int a = 0, b = 0; int c; c = a + b; //1 基础数据类型 编译器已经知道了. 如何运算 // a + bi 复数运算规则 Complex c1(1, 2), c2(3, 4); Complex c3; //2 类 也是一种数据类型 用户自定义数据类型 C++编译器 是不知道如何进行运算 //步骤3 Complex c4 = c1 + c2; c4.printCom(); //总结: 1 运算符重载的本质 是 函数调用 cout<<"hello..."<<endl; system("pause"); return ; }
二、实列
#include <iostream> using namespace std; /* class ostream { }; */ class Complex { private: int a; int b; //friend void operator<<(ostream &out, Complex &c1); friend ostream& operator<<(ostream &out, Complex &c1); public: Complex(int a=0, int b=0) { this->a = a; this->b = b; } void printCom() { cout<<a<<" + " << b << "i" <<endl; } public: //实现 + 运算符重载 Complex operator+(Complex &c2) { Complex tmp(a + c2.a, b + c2.b); return tmp; } //前置++ Complex& operator++() { a++; b++; return *this; } //后置++ Complex operator++(int) { //先使用 在让c1加加 Complex tmp = *this; //return c1; this->a ++; this->b ++; return tmp; } //成员函数法 实现 -运算符重载 Complex operator-(Complex &c2) { Complex tmp(this->a - c2.a, this->b - c2.b); return tmp; } //前置-- Complex& operator--() { this->a --; this->b --; return *this; } //后置-- Complex operator--(int) { Complex tmp = *this; this->a--; this->b--; return tmp; } }; void main31() { Complex c1(1, 2), c2(3, 4); //1 全局函数法 实现 + 运算符重载 // Complex operator+(Complex &c1, Complex &c2); Complex c3 = c1 + c2; c3.printCom(); //2 成员函数 法 实现 -运算符重载 //c1.operator-(c2); //Complex operator-(Complex &c2) Complex c4 = c1 - c2; c4.printCom(); //前置++操作符 用全局函数实现 ++c1; c1.printCom(); //前置--操作符 成员函数方法 --c1; c1.printCom(); //Complex& operator++(Complex &c1) //c1.operator--(); //后置++操作符 用全局函数实现 c1++; c1.printCom(); //后置--操作符 用成员函数实现 c1--; c1.printCom(); //c1.operator--() cout<<"hello..."<<endl; system("pause"); return ; } /* void operator<<(ostream &out, Complex &c1) { out<<"12345 生活真是苦"<<endl; out<<c1.a << " + " << c1.b << "i" << endl; } */ ostream& operator<<(ostream &out, Complex &c1) { out<<"12345 生活真是苦"<<endl; out<<c1.a << " + " << c1.b << "i" << endl; return out; } void main() { int a = 10; Complex c1(1, 2), c2(3, 4); cout<<a<<endl; //按照数据类型 //1 cout << c1 ; //2 ostream 类中 添加 成员函数 .operator<< //ostream //cout.operator<<(c1); //2 函数返回值当左值 需要返回一个引用 cout << c1 << "aaddddd"; // //cout.operator<<(c1) .operator<<("aaddddd"); //void.operator<<("aaddddd"); system("pause"); }