博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++友元实现操作符重载
阅读量:7003 次
发布时间:2019-06-27

本文共 2450 字,大约阅读时间需要 8 分钟。

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

#include 
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<<<" + "<<<"i "<

为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");}

转载地址:http://crevl.baihongyu.com/

你可能感兴趣的文章
Java将每半年发布一个版本
查看>>
多重影分身:一套代码如何生成多个小程序?
查看>>
Alluxio在多级分布式缓存系统中的应用
查看>>
C#将引入可空的引用类型
查看>>
Envoy Proxy的多面性:边缘网关、服务网格和混合网桥
查看>>
JavaScript对象:我们真的需要模拟类吗?
查看>>
js对象监听实现
查看>>
【JavaScript】call与apply兄弟列传
查看>>
分离django中的媒体文件,静态文件和网页
查看>>
算法笔记(JavaScript版)——优先队列
查看>>
js谜之正则表达式
查看>>
开发工具Eclipse篇之深度设置
查看>>
JS构造函数内的方法与构造函数prototype属性上方法的对比
查看>>
Andorid Studio NDK开发-Hello World
查看>>
PHP|PHP实践-生成器
查看>>
Win7远程桌面以及远程关机设置注意事项
查看>>
写给想做前端的你
查看>>
First angular 2 app
查看>>
css边框入门
查看>>
从零开始写个编译器吧 - 分析非终结符
查看>>