Category Archives: Operator Overloading

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;
}

1 Comment

Filed under C++, Operator Overloading

C++ : String Operator Overloading

Problem:
==============================================
String Operator Overloading:
+ Operator.
== Operator.
!= Operator.
< Operator.
Operator.
>= Operator.
[] Operator.
cout Operator.
cin Operator.
==============================================
Solution:

#include
#include

using namespace std;

class MyString
{
char *value;
int len;

public:

MyString()
{
len=0;
value=0;
}
~MyString()
{
}

//String From Array
MyString(char *s)
{
len=strlen(s);
value=new char[len+1];
strcpy(value,s);
}

//String using Copy Constructor
MyString(MyString & s)
{
len=s.len;
value=new char[len+1];
strcpy(value,s.value);
}

//str3=str1+str2
friend MyString operator+(MyString obj1,MyString obj2)
{
MyString obj3;
obj3.len=obj1.len+obj2.len;
obj3.value=new char[obj3.len+1];

strcpy(obj3.value,obj1.value);
strcat(obj3.value,obj2.value);

return obj3;
}

//if(str1==str2)
friend int operator==(MyString obj1,MyString obj2)
{
int rel=0;

if(strcmp(obj1.value,obj2.value)==0)
{
rel=1;
}
return rel;
}

//if(str1!=str2)
friend int operator!=(MyString obj1,MyString obj2)
{
int rel=0;

if(strcmp(obj1.value,obj2.value)!=0)
{
rel=1;
}
return rel;
}

//if(str1<str2)
friend int operator<(MyString obj1,MyString obj2)
{
int rel=0;
int result=0;

rel=strcmp(obj1.value,obj2.value);

if(rel<0)
{
result=1;
}
return result;
}

//if(str1<=str2)
friend int operator<=(MyString obj1,MyString obj2)
{
int rel=0;
int result=0;

rel=strcmp(obj1.value,obj2.value);

if(relstr2)
friend int operator>(MyString obj1,MyString obj2)
{
int rel=0;
int result=0;

rel=strcmp(obj1.value,obj2.value);

if(rel>0)
{
result=1;
}
return result;
}

//if(str1>=str2)
friend int operator>=(MyString obj1,MyString obj2)
{
int rel=0;
int result=0;

rel=strcmp(obj1.value,obj2.value);

if(rel>0 || rel==0)
{
result=1;
}
return result;
}

//char rel=str1[2]
char operator [](int val)
{
char rel;

if(val>len)
{
cout<<"\n Invalid Index position\n";
}
else
{
rel=value[val];
cout<<"\nThe result is:"<<rel<>str1
friend istream & operator >>(istream & ccin,MyString & obj)
{
char val[20];

cout<>val;

obj.len=strlen(val);
obj.value=new char[obj.len+1];

strcpy(obj.value,val);

return ccin;
}

//cout<<str1
friend ostream & operator <<(ostream & ccout,MyString & obj)
{
ccout<<"\n Entered String is:"<<obj.value<<"\n";
return ccout;
}

void display()
{
if(len==0)
{
cout<<"\n String is Empty \n";
}
else
{
cout<<"\nThe result is:"<<value<<"\n";
}
}
};

int main()
{
MyString str1("xxxyyyzzz");

MyString str2("abc");

str1.display();

str2.display();

MyString str3;

str3=str1+str2;

str3.display();

if(str1<str2)
{
cout<<"\n Condition is True\n";
}
else
{
cout<<"\n Condition is False \n";
}

return 0;
}

1 Comment

Filed under C++, Operator Overloading

Operator Overloading in C++ (Part – 3)

Leave a comment

Filed under C++, Operator Overloading

Operator Overloading in C++ (Part – 2)

Leave a comment

Filed under C++, Operator Overloading

Operator Overloading in C++ (Part – 1)

Leave a comment

Filed under C++, Operator Overloading