Skip to content

Commit

Permalink
doc:non-Member,Friend Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
muhibarshad committed Oct 18, 2022
1 parent 24af726 commit e4bc655
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 46 deletions.
157 changes: 156 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,7 @@ int main()
<!--Operator overloading-->
# Operator Overloading
**So, the first Question is what are operators?**\
In mathematics and sometimes in computer programming, an operator is a character that represents an action, as for example + is an arithmetic operator that represents addition.
In mathematics and sometimes in computer programming, an operator is a character that represents an action, as for example + is an arithmetic operator that represents addition.\
**Why we need operator overloading concept?**\
The Arithmetic operator (+, -, *, /, %) are already defined for the built in data types like int, float, char etc. But, right now, when we are creating/defining our own data types( class represents a data type), So, we need to write our own operators for this purpose.
### Defination
Expand Down Expand Up @@ -2518,5 +2518,160 @@ int main()
}

```
## Non-Member functions
**Why we need the non-member functions to operator overloading**?\
Because We know that the L.H.S object calls the R.H.S object . If L.H.S operand is not the object its dataType is different from the l.H.S operand than what we have to do.
Like this,
```cpp
2 + p1; // left operand is int
cout << p1; // left operand is ostream class object
cin >> p1; //left operand if istream class object
```
Therefore, Non-member functions can be used for such operations

- Non-member function cannot be defined inside the class
- They cannot access the private data members of a class
- Operators that cannot be overloaded through non-member functions are
`=, [], (), ->, &`(address of operator)
- All other operators can be overloaded through non-member functions
- Unary operators
- Non-member function, needs one argument
- Binary operators
- Non-member function, needs two arguments
- One argument must be class object or reference
• There is no `this` pointer in non-member functions

## Non-Member `friend` functions
`friend` function can access private and protected members of another class.\
`friend` functions are non-member functions of class.\
•They are defined outside of class scope
Can only add prototype inside class definition for granting friendship.
•There is no `this` pointer in non-member friend functions.\
**Properties of friendship**\
• Friendship is granted, not taken\
• Not symmetric (if B a friend of A, A not necessarily a friend of B)\
• Not transitive (if A is friend of B, B is friend of C, A not necessarily a
friend of C).

### Uniary Operator -

Non-member function takes one
argument that must be the class object

Can be called in two ways.
```cpp
Point p1(3, 4);
operator-(p1);
// calls friend function
Or
-p1;
Point p2 = -p1;
```
• Only add one function member or non-
member friend to avoid conflict.
<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/uniaryFriend.png" style="height: 70vh; padding-left: 0vh;">
</p>
### Binary operator Addition +
Can be overloaded as a friend in 3 ways;
- Both operands are class objects.
- One operand left one is class object.
- One operand right one is class object.
Must define non-member function.\
>Non-member function takes two arguments
<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/binaryFriend.png" style="height: 70vh; padding-left: 0vh;">
</p>
Example Code:
```cpp
#include <iostream>
using namespace std;
class Point
{
int x, y;
public:
Point(int x = 0, int y = 0):x(x),y(y){}
//Both are objects
friend Point operator+(const Point &p, const Point &q);
// with int right operand
friend Point operator+(const Point &p, const int &n);
// with int left operand
friend Point operator+(const int &n, const Point &p);
};
Point operator+(const int &n, const Point &p)
{
return p + n;
// Reuse code of right operand function
}
int main()
{
Point p1(3, 4);
operator+(3, p1);
10 + p1;
// both p1 and int 10 are passed argumnets
int a = 10;
Point p3 = a + p1;
// cascaded call
}
```
### Binary Operators Stream insertion (<<) and Stream extraction (>>)

- One operand left one is `stream` object and right one is class object.
- Must define non-member function, which
takes two arguments
- First non constant reference of `ostream` object in case of insertion `<<` operator and `istream` object in case of extraction `>>` opeartor.
- Second const reference of class object
- For cascading return `ostream` object by reference from function.

<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/<<>>.png" style="height: 60vh; padding-left: 0vh;">
</p>

**Code Example here**:
```cpp
#include <iostream>

using namespace std;

class Point
{
int x, y;

public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
friend ostream &operator<<(ostream &, const Point &);
friend istream &operator>>(istream &, Point &);
};

ostream &operator<<(ostream &out, const Point &p)
{
out << "X:" << p.x << endl;
out << "Y:" << p.y << endl;
return out;
}
istream &operator>>(istream &in, Point &p)
{
in >> p.x;
in >> p.y;
return in;
}
int main()
{
Point p1;
cin >> p1; // 1,2
cout << p1; // X:1,Y:2
}
```

# Summary of Operator Overloading
<p align="center">
<img src="/Some%20extra%20concepts/codeSnaps/summary.png" style="height: 100vh; padding-left: 0vh;">
</p>
Binary file added Some extra concepts/codeSnaps/<<>>.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Some extra concepts/codeSnaps/binaryFriend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Some extra concepts/codeSnaps/summary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Some extra concepts/codeSnaps/uniaryFriend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified temp2
Binary file not shown.
59 changes: 14 additions & 45 deletions temp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,20 @@ class myArray
size = 0;
ptr = nullptr;
}
myArray(int size)
{
this->size = size;
if (size > 0)
{
ptr = new int[size];
for (int i = 0; i < size; i++)
ptr[i] = i + 1;
}
else
ptr = nullptr;
}
myArray(int size);
myArray(int *arr, int size);
myArray(const myArray &); // copy constructor
~myArray();
myArray &operator=(const myArray &); // Assignment
int &operator[](const int i);
const int &operator[](const int i) const;
myArray &operator++(); // increment data of all elements
myArray operator++(int); // increment data of all elements
bool operator==(const myArray &); // compare size and data of all elements
bool operator!=(const myArray &);
friend istream &operator>>(istream &, myArray &); // take size and data from console
friend ostream &operator<<(ostream &, const myArray &); // Print data of array on console
myArray operator+(const myArray &); // Return array containing data of both arrays merged
friend myArray operator+(const int, const myArray &); // add int value to all elements of array
};
// implementation for Normal object
int &myArray::operator[](const int i)
{
// check if index i is in range
if (i >= 0 && i < size)
return ptr[i];
// return element by reference as lvalue
}
// implementation accessor for Constant object
const int &myArray::operator[](const int i) const
{
// check if index i is in range
if (i >= 0 && i < size)
return ptr[i];
// return element by reference as constant rvalue
}
int main()
{
myArray a1(5); // creates array inside object
a1[0] = 100; // return reference to int element 1 of array
// store 100 in element 1
a1[1] = a1[0]; // copy element 1 to element 2
cout << a1[1]; // print value of element 2
const myArray a2(3); // creates array of size 3 inside constant object
cout << a2[1];
// return constant reference (read only) to int
// a2[1] = 10; // wrong as constant reference is returned for constant object
// • Not work on pointers to objects directly
myArray *aptr = new myArray(5); // creates array inside object
aptr[3] = 100; // wrong as aptr is pointer
(*aptr)[3] = 100; // first dereference the pointer then access data
aptr[0][3] = 100;
}

0 comments on commit e4bc655

Please sign in to comment.