CSIT Object Oriented Programming with C++(Unary Operator Overloading using Friend function) Part IV

 I am assuming that, You have already studied Part I,II and III of Operator Overloading Series. So in this post, I am going to teach you How to perform Unary operator overloading using friend function.

So, let's get started,

//Overloading pre-increment operator using friend function

#include<iostream>

class A{

int length,breadth;

public:

A(int l, int b)

{

length=l;

breadth=b;

}

friend void operator ++( A&);

void display()

{

cout<<"Length: "<<length<<endl<<"Breadth: "<<breadth<<endl;

}

};

void operator ++( A& b);

{

++b.length;

++b.breadth;

}

int main()

{
A a(7,14);

++a;    //equivalent to operator ++(a);

a.display();

return 0;

}


OUTPUT

Length: 8

Breadth: 15


This is for pre-increment operator.

In next post, I will teach you overloading binary operator using member function. So refer next  post for that topic.


If This post Helped you, Please leave a comment, and share with your Coder Friends.
Thank You.

Keep Learning, Keep Growing

Comments

Popular posts from this blog

CSIT Object Oriented Programming with C++(Insertion and Extraction Operator Overloading ) Part VI

CSIT Object Oriented Programming with C++(Unary Operator Overloading) Part III