运算符重载的本质是一个函数

#include <iostream>
using namespace std; class A {
private:
int m_a;
int m_b; friend A operator+(A &a1, A &a2);//友元函数訪问私有属性。实现二元运算符重载 friend A operator++(A &a); //友元函数訪问私有属性。实现一元运算符重载(前置) /*后置为避免与前置函数定义同样,使用占位符int。告诉编译器是后置运算符重载*/
friend A operator++(A &a, int); //友元函数訪问私有属性。实现一元运算符重载(后置) friend class B; //友元类,訪问私有属性。 若B类是A类的友员类,则B类的全部成员函数都是A类的友员函数 public:
A(int a){
this->m_a = a;
} A(int a, int b) {
m_a = a;
m_b = b;
} void show() {
cout << m_a << endl;
} }; class B {
private:
A *pObjA = new A(21); public:
void showA() {
cout << pObjA->m_a << endl;
} }; A operator+(A &a1, A &a2) {
A tmp(a1.m_a + a2.m_a, a1.m_b + a2.m_b);
return tmp; } A operator++(A &a) { //前置++
a.m_a++;
a.m_b++;
return a; } A operator++(A &a, int) { //后置++
A tmp = a;
a.m_a++;
a.m_b++;
return tmp;
} int main() { A a1(1, 9);
A a2(3, 4);
A a3 = a1 + a2;
a3.show(); ++a3;
a3.show(); a3++;
a3.show(); return 0;
}
class Complex
{
public:
int a;
int b;
friend Complex operator+(Complex &c1, Complex &c2);
public:
Complex(int a=0, int b=0)
{
this->a = a;
this->b = b;
} public:
void printCom()
{
cout<<a<<" + "<<b<<"i "<<endl;
} private:
}; /*
Complex myAdd(Complex &c1, Complex &c2)
{
Complex tmp(c1.a+ c2.a, c1.b + c2.b);
return tmp;
}
*/ Complex operator+(Complex &c1, Complex &c2)
{
Complex tmp(c1.a+ c2.a, c1.b + c2.b);
return tmp;
} void main()
{
Complex c1(1, 2), c2(3, 4); //Complex c3 = c1 + c2; //用户自己定义类型 编译器无法让变量相加
//Complex myAdd(Complex &c1, Complex &c2); //1 普通函数
//Complex c3 = myAdd(c1, c2);
//c3.printCom(); //2 operator+ 函数名称
//Complex c3 = operator+(c1, c2);
//c3.printCom(); //3 +替换 函数名
Complex c3 = c1 + c2; //思考C++编译器怎样支持操作符重载机制的 (依据类型)
c3.printCom();
{
int a =0, b = 0, c; //基础类型C++编译器知道怎样加减
c = a +b;
} //4 把Complex类变成私有属性
//friend Complex operator+(Complex &c1, Complex &c2); cout<<"hello..."<<endl;
system("pause");
return ;
}

为vector类重载流插入运算符和提取运算符

class vector
{
public :
vector( int size =1 ) ;
~vector() ;
int & operator[]( int i ) ;
friend ostream & operator << ( ostream & output , vector & ) ;
friend istream & operator >> ( istream & input, vector & ) ;
private :
int * v ;
int len ;
}; vector::vector( int size )
{
if (size <= 0 || size > 100 )
{
cout << "The size of " << size << " is null !\n" ; abort() ;
}
v = new int[ size ] ; len = size ;
} vector :: ~vector()
{
delete[] v ;
len = 0 ;
} int &vector::operator[]( int i )
{
if( i >=0 && i < len ) return v[ i ] ;
cout << "The subscript " << i << " is outside !\n" ; abort() ;
}
ostream & operator << ( ostream & output, vector & ary )
{
for(int i = 0 ; i < ary.len ; i ++ )
output << ary[ i ] << " " ;
output << endl ;
return output ;
}
istream & operator >> ( istream & input, vector & ary )
{
for( int i = 0 ; i < ary.len ; i ++ )
input >> ary[ i ] ;
return input ;
} void main()
{
int k ;
cout << "Input the length of vector A :\n" ;
cin >> k ;
vector A( k ) ;
cout << "Input the elements of vector A :\n" ;
cin >> A ;
cout << "Output the elements of vector A :\n" ;
cout << A ;
system("pause");
}

c++友元实现操作符重载的更多相关文章

  1. c++ 操作符重载和友元

    操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...

  2. C++基础 (4) 第四天 this指针 全局函数和成员函数 友元 操作符重载

    1static强化练习-仓库进货和出货 #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; c ...

  3. C++ 友元(friend关键字)、类中的重载、操作符重载(operator关键字)

    C++ 中友元的用法: 1.在类中使用friend关键字声明 2.类的友元可以是其它类或者具体函数 3.友元不是类的一部分 4.友元不受类中访问级别的限制 5.友元可以直接访问具体类中的所有成员. 友 ...

  4. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

  5. C++一些注意点之操作符重载

    重载操作符需要注意 (1)重载操作符必须具有一个类类型操作数.不能重载内建类型的操作符. operator +(int,int);//这个是错误的,都为内建类型 operator +(int,clas ...

  6. C++中的操作符重载

    一.什么是操作符重载 操作符重载可以分为两部分:“操作符”和“重载”.说到重载想必都不陌生了吧,这是一种编译时多态,重载实际上可以分为函数重载和操作符重载.运算符重载和函数重载的不同之处在于操作符重载 ...

  7. 15.C++-操作符重载

    首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...

  8. C++基础知识:操作符重载

    1.C++标准库: C++标准库并不是C++语言的一部分C++标准库是由C++语言编写而成的类库和函数的集合C++标准库中定义的类和对象都位于std命名空间中C++标准库的头文件都不带.h后缀C++标 ...

  9. 15.C++-操作符重载、并实现复数类

    首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...

随机推荐

  1. oracle实现查询每个部门的员工工资排在前三的员工的基本信息具体举例

    --先删除原先存在的表: drop table emp; --创建表emp create table emp ( deptno number, ename varchar2(20), sal numb ...

  2. cocos2dx项目创建

    射击类游戏文档 作者:浙江传媒学院  新媒体  张勇 1>编译环境 首先我们先去cocos2dx官网上下载cocos2dx最新版本号 http://www.cocos2d-x.org/ 我下载的 ...

  3. 2015.05.11,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论事情进展” SESSION 44

    1. not the real McCoy simulate(['simjuleit] v. 假装,冒充,模仿,模拟)来自拉丁simulo,copy的意思.simulo本身派生自拉丁形容词simili ...

  4. Ruby学习笔记(二)——从管道读取数据

    在对文件名修改后,今天又给自己出了新的难题,想从实验结果中提取数据,并将其作为文件夹的名称.其中,比赛的主办方提供的评估算法是用perl写的,因此读取实验结果最为简单的想法自然是使用管道命令,即 ./ ...

  5. mysqli数据库操作简单实例

    mysqli数据库操作简单实例 代码 结果

  6. mysqil操作数据库

    mysqil操作数据库 每次用到mysql_connect连接数据库的时候都会提示: 1 Deprecated: mysql_connect(): The mysql extension is dep ...

  7. 11. Container With Most Water[M]盛最多水的容器

    题目 Given \(n\) non-negative integers \(a_1,a_2,\cdots,a_n\), where each represents a point at coordi ...

  8. Android Fragment RecycleListView

    1.新建SuperActivity package com.example.ting.criminalintentpractise; import android.os.Bundle;import a ...

  9. adplus 抓取dump

    工具所在路径   C:\Program Files\Windows Kits\10\Debuggers\x64 cmd窗口切换目录倒adplus所在路径下,输入抓取命令.adplus -hang -p ...

  10. React组件化开发

    环境搭建: 1.安装node.js 2.安装cnpm  # npm install -g cnpm --registry=https://registry.npm.taobao.org 3.全局安装c ...