Operator Overloading with Pointers

Ram Pothuraju
This program is for Operator Overloading in C++ . It performs the overloading of  '+' and '*' operators. 


C++ or CPP provides us to over load the different operators but the basic working of operators remains the same. Here we have overloaded the operators in a different way by using the local variables and used pointers as reference.



#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

class OverLoad
{
 char *s1,*s2;
 public:
 void get(char &a,char &b);
 void put();
 char* operator +();
 char* operator *();
};
 void OverLoad :: get(char &a,char &b)
 {
  s1=&a;
  s2=&b;

 }

 void OverLoad :: put()
 {
  cout<<"String 1 : "<<s1;
  cout<<"\nString 2 : "<<s2;

 }

 char* OverLoad :: operator +()
 {
  strcat(s1,s2);
  return s1;
 }

 char* OverLoad :: operator *()
 {
  strrev(s1);
  return s1;
 }

void main()
{
 char *a,*b,*c,*d;
 OverLoad obj;
 clrscr();
 cout<<"Enter First String :";
 gets(a);
 cout<<"\nEnter Second String : ";
 gets(b);
 obj.get(*a,*b);
 obj.put();
 c=+obj;
 cout<<"\n\nConcatinated String : "<<c;
 d=*obj;
 cout<<"\n\nReversed String : "<<d;
 getch();
}

Tags

Post a Comment

0Comments

Post a Comment (0)