运算符重载编程基础

例如:

//全局函数 完成 +操作符 重载  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=, int b=)
{
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 = , b = ;
int c;
c = a + b; //1 基础数据类型 编译器已经知道了. 如何运算 // a + bi 复数运算规则
Complex c1(, ), c2(, );
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=, int b=)
{
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(, ), c2(, ); //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 = ;
Complex c1(, ), c2(, );
cout<<a<<endl; //按照数据类型 //
cout << c1 ;
//2 ostream 类中 添加 成员函数 .operator<<
//ostream
//cout.operator<<(c1); //2 函数返回值当左值 需要返回一个引用
cout << c1 << "aaddddd";
//
//cout.operator<<(c1) .operator<<("aaddddd");
//void.operator<<("aaddddd"); system("pause");
}

C++——运算符重载的更多相关文章

  1. C++ 运算符重载时,将运算符两边对象交换问题.

    在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...

  2. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  3. C++运算符重载

    C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...

  4. 标准C++之运算符重载和虚表指针

    1 -> *运算符重载 //autoptr.cpp     #include<iostream> #include<string> using namespace std ...

  5. python运算符重载

    python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...

  6. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  7. 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

    [源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...

  8. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  9. c/c++面试题(6)运算符重载详解

    1.操作符函数: 在特定条件下,编译器有能力把一个由操作数和操作符共同组成的表达式,解释为对 一个全局或成员函数的调用,该全局或成员函数被称为操作符函数.该全局或成员函数 被称为操作符函数.通过定义操 ...

  10. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

随机推荐

  1. iOS组件化开发-CocoaPods安装

    首先要检查Mac是否安装了rvm(ruby version manager).打开终端,输入指令$ rvm -v ,若没有安装 curl -L https://get.rvm.io | bash -s ...

  2. vt-is-UTF8 - check whether current VT is in UTF8- or byte-mode. 检查当前VT是否处于VTF8模式或是字节模式.

    总览 vt-is-UTF8 [-h|--help] [-V|--version] [-q|--quiet] 描述 vt-is-UTF8 checks whether the current VT is ...

  3. 批量Ping执行Bash脚本

    #!/bin/bash # Ping Batch Script # 连接超时时间 TMOUT= # 最大线程数 MAX_THREAD= # 保留内存大小 MIN_MEM= # 默认ip配置,可通过 - ...

  4. Sql Server 小知识不断扩充中

    1.  char.varchar.nvarchar 区别 char 定长字符数据长度8000字符,小于8000字符时以空格填充. varchar 变长字符数据最大长度8000,小于8000字符时不会以 ...

  5. leetcode-回溯③

    题77 回溯: class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res = [] def backtr ...

  6. Java ArrayList使用技巧 - 两个ArrayList去除重复的元素

    方法一.ArrayList中提供的removeAll方法(效率最低) List1.removeAll(mSubList); 方法二.双重循环(比方法一效率高) 双重循环分为内外两层循环,经过测试,将元 ...

  7. cmake -help

    { Usage cmake [options] <path-to-source>  cmake [options] <path-to-existing-build> Speci ...

  8. Dart编程数字Number

    Dart数字可以分为: int - 任意大小的整数. int 数据类型用于表示整数. double -64位(双精度)浮点数,由IEEE 754标准规定. 在 double 数据类型用于表示小数 in ...

  9. ETL详解

    ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为企业的决策提供分析依据. ETL是BI项目重要的一个环节. 通常情况下,在 ...

  10. C语言-实例3个数由小到大排序

    VS2012 //C语言实例 3个数由小到大排序 #include <stdio.h> void main() { int a, b, c, t; printf("Please ...