Prefix Operator Overloading in C++ using Simple and Friend function

In this video I have explained about Postfix & Prefix Operator Overloading in C++:

1. Explain Operator Overloading.

2. Example showing the use of Postfix & Prefix Operator Overloading using Simple Function.

3. Example showing the use of Postfix & Prefix Operator Overloading using Friend Function.

Secondly I have shown code explaining the use of Postfix & Prefix Operator Overloading using Simple & Friend function in C++ Inheritance.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Postfix & Prefix Operator Overloading using Simple & Friend Function.
*/

#include

using namespace std;

class A
{
int a;

public:

A()
{ }
A(int ta)
{
a=ta;
}
void display()
{
cout<<"A"<<a;
}

//Simple Function Prefix
void operator--()
{
a=a-10;
}

//Simple Function Postfix
void operator--(int dummy)
{
a=a-20;
}

//Friend Function Prefix
friend void operator++(A &obj)
{
obj.a=obj.a+1;
}

//Friend Function Postfix
friend void operator++(A &obj,int dummy)
{
obj.a=obj.a+10;
}

};

int main()
{
A obj(100);

obj.display();

obj++;

obj.display();

return 0;
}

Advertisements

1 Comment

Filed under C++, Operator Overloading

One response to “Prefix Operator Overloading in C++ using Simple and Friend function

  1. hi u have a nice website. I wanna say thanks for posting this interesting articles with us. Keep up this great job.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s