运算符重载编程基础

例如:

//全局函数 完成 +操作符 重载  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. Mac版本的 Axure rp8 不显示菜单栏

    我之前也是一直在找这个问题,可能mac用的不熟练吧,其实他的菜单栏就近在眼前 你看不见只是因为你的关注点在axure上 往大了看,他的菜单栏显示在你的电脑的菜单栏上,mac的菜单栏基本都是这么显示的, ...

  2. css3 鼠标悬停图片动画

    <div class="grid"> <figure class="effect-milo"> <img src="im ...

  3. 微信小程序中实现左右滑动图片翻页

    页面代码 <swiper class="container" indicator-dots="{{indicatordots}}" autoplay=&q ...

  4. 结对编程-Core 第12组 [pb15061359+pb15061351]

    一.项目要求 1.输入题目数量,生成操作数为3~5个的四则运算题目 2.输入上限值控制生成的操作数的最大值以及结果的最大值 3.输入支持的操作符类型:加.减.乘.除.乘方.括号 4.输入支持的操作数类 ...

  5. 深度探索C++对象模型之第四章:函数语义学

    C++有三种类型的成员函数:1.static/nonstatic/virtual 一.成员的各种调用方式 C with Classes 只支持非静态成员函数(Nonstatic Member Func ...

  6. Echart中X轴数据过多时横向拉动展示

    chart.setOption( { tooltip: { trigger: 'axis' }, toolbox: { feature: { saveAsImage: {} } }, grid: { ...

  7. Python 封装一个函数,查找文字字符串数字英文下标

    def abc(str,data): count = [] numMax = 0 for a in range(len(str)): if a == 0: temp = str.find(data, ...

  8. 使用jQuery对象

    1基本行为 * size()/length: 包含的DOM元素个数 * [index]/get(index): 得到对应位置的DOM元素 * each(): 遍历包含的所有DOM元素 * index( ...

  9. JavaWeb学习篇之----Servlet过滤器Filter和监听器

    首先来看一下Servlet的过滤器内容: 一.Servlet过滤器的概念: ************************************************************** ...

  10. Python 爬取12306火车票

    获取火车站 stations.py #import certifi #import urllib3 import re import requests from pprint import pprin ...