Simple application of c/c + + friend

Keywords: C++

Simple application of friend

1. Object + object, or object + number, can overload + function with class member function. However, number + object cannot overload + function with class member function,

Because the compiler will translate the number + object into the number. operator+(const class & object), because the number is not an object of the class, it cannot be passed to the member function this pointer of the class.

UFIDA de overloading: number + object

2. UFIDA de overloading: > > operator and < < operator.

In fact, the member function of a class can also overload the < operator, but it's weird to use. You can't use cout < object, you can only use object < cout.

#include <iostream>
using namespace std;

class Imaginary{
  friend Imaginary operator+(int i, const Imaginary &m);
  friend ostream& operator<<(ostream &os, const Imaginary &m);
  friend istream& operator>>(istream &is, Imaginary &m);
public:
  Imaginary():real(0), imag(0){
    cout << "c:" << this << endl;
  }
  Imaginary(int real, int imag):real(real), imag(imag){
    cout << "c:" << this << endl;
  }
  Imaginary operator+ (const Imaginary &m){
    return Imaginary (real + m.real, imag + m.imag);
  }
  Imaginary operator+ (int i){
    return Imaginary(real + i, imag);
  }
  Imaginary& operator= (const Imaginary &m){
    cout << "asign" << endl;
    if(this != &m){
      real = m.real;
      imag = m.imag;
    }
    return *this;
  }
 ostream& operator<<(ostream& os){
    os <<  "[" << real << "," << imag << "]";
    return os;
  }
  ~Imaginary(){
    cout << this << endl;
  }
private:
  int real;
  int imag;
};
Imaginary operator+(int i, const Imaginary &m){
  return Imaginary(i + m.real, m.imag);
}

ostream& operator<<(ostream &os, const Imaginary &m){
  os <<  "(" << m.real << "," << m.imag << ")";
  return os;
}
istream& operator>>(istream &is, Imaginary &m){
  is >> m.real >> m.imag;
  return is;
}
int main(){
  Imaginary m1(10, 20);
  Imaginary m2(1, 2);
  Imaginary m3 = m1 + m2;
  Imaginary m4 = m1 + 10;
  Imaginary m5 = 20 + m1;
  cout << "a" << m5 << "aa" << endl;;
  m5 << cout << "bb" << endl;
  Imaginary m6;
  cin >> m6;
  cout << m6 << endl;
  return 0;
}

Posted by ssj4gogita4 on Thu, 02 Jan 2020 10:50:37 -0800