Operator Overloading Friends
Operator Overloading Friends
Today
Overloading Operators
formally they have definitions with respect to one or more classes i.e. they apply to classes
Example scenario
classComplex{ doublereal,imaginary; public: Complex():real(0.0),imaginary(0.0){ } Complex(doubler,doubleim):real(r), imaginary(im){ } ... }; intmain(){ Complexcm1,cm2(10,15); Complexcm3=cm1+cm2; }
But...
Because the operator '+' has no meaning w.r.t. the Complex class! Two forms of the same solution:
Form #1
const Complex operator+( const Complex &c1, const Complex &c2 ); number of operands equals the type of operator
at least one operand must be a class type can be pass-by-value or -reference, with or without const
efficiency concerns
functions that are declared public and provide access to private members
double Complex::GetReal() const{ return real;} double Complex::GetImaginary() const { return imaginary; } const Complex operator+( const Complex &c1, const Complex &c2 ) { double r = c1.GetReal() + c2.GetReal(); double im = c1.GetImaginary() + c2.GetImaginary(); return Complex( r, im ); }
Form #2
const Complex operator+( const Complex &c2 ); number of operands equals one minus the type of operator
none for unary, one for binary and so on hence we omit it as an argument
Form #2 Definition
const Complex operator++(); // prefix form const Complex operator++( int dummy ); // suffix form
The definition?
constComplexComplex::operator++ (intdummy){ ++this>real; ++this>imaginary; return*this; }
as opposed to r-values
this is valid:
Complex c3 = (c1+c2)++; // convince yourself
Different Classes?
Yes it does
More Overloading
remember, overloading as a member: the argument on the left is the calling class
constComplexComplex::operator+(inti);
Friends of a class
Define it
i.e. not directly accessible to non members functions but not so for friends
void Display( const Complex &c ) { cout << c.real << + << c.imaginary << i\n; ... }
Friend Classes
A friend class can access private or protected members of the class it is friends with Similar to friend functions (functionclass), but more elaborate (class-class) Declaration is different
Example
class Node{ int data; int key; friend class BinaryTree; // class BinaryTree can now access data directly }; class BinaryTree{ Node *root; int find(int key){ if(root->key == key){ // no need to go through an accessor function return root->data; } // perform rest of find }
Almost correct
But class Node does not know anything about class BinaryTree at the point where it is declared use forward declaration before declaring class Node
class BinaryTree; class Node{ ... }
Helps implement serialization To be able to write statements like Complex c; cin >> c; cout << c <<endl; Argument on the left is not a user-defined object
Goal?
Problem?
Then, how?
ostream& operator << (ostream& os, const Complex& s); istream& operator >> (istream& is, Complex& s);
When calling the function x.f(), the keyword this in the body of f() stores the address of x
Example
struct X { private: int a; public: void Set_a(int a) { // The 'this' pointer is used to retrieve 'xobj.a' // hidden by the automatic variable 'a' this->a = a; } };