prowessapps.in Operator Overloading Programs in C++

#include< iostream>
using namespace std;
class Distance {
 int m, cm;
 public:
 Distance(int a, int b) {
  m = a;
  cm = b;
 }
 Distance operator++(int) { 
  m = m+1;
  cm = cm+5;
  return Distance(m,cm);
 }
 Distance operator++() { 
  m = m+3;
  cm = cm+10;
  return Distance(m,cm);
 }
 Distance operator-() {
 m = (-1)*m;
 cm = (-1)*cm;
 return Distance(m,cm);
 }
 void display() {
  cout<< m<<" meter "<< cm<<" cm\n";
 }
};
int main() {
 Distance d(10,20);
 d.display();
 d++;
 d.display();
 ++d;
 d.display();
 d = -d;
 d.display();
 return 0;
}
OUTPUT
10 meter 20 cm
11 meter 25 cm
14 meter 35 cm
-14 meter -35 cm

#include< iostream>
using namespace std;
class Time {
 int h, min,sec;
 public :
 Time(int a, int b,int c) {
  h = a;
  min = b;
  sec = c;
 }
 Time() {
  h = min = 0;
 }
 void show() {
  cout<< h<<" Hours "<< min;
  cout<<" minutes "<< sec<<" sec.\n";
 }
 Time operator=(Time t){
  h = t.h;
  min = t.min;
  sec = t.sec;
  return Time(h,min,sec);
 }
friend Time operator>>(istream &ob,Time &t);
friend Time operator<<(ostream &ob,Time &t);
};
Time operator>>(istream &ob,Time &t){
 cout<<"Enter Time ";
 ob>>t.h;
 ob>>t.min;
 ob>>t.sec;
}
Time operator<<(ostream &ob,Time &t){
 ob<< t.h<<"::"<< t.min;
 ob<<"::"<< t.sec<< endl;
}
int main() {
 Time t;
 cin>>t;
 cout<< t;
 t.show();
 Time t2,t3(5,10,30);
 t2 = t3;
 t3.show();
  return 0;
}

OUTPUT
Enter Time 10 20 40
10::20::40
10 Hours 20 minutes 40 sec.
5 Hours  10 minutes 30 sec.

#include< iostream>
#include< string.h>
using namespace std;
class String {
 public:
 char a[20];
 void getString(char x[]) {
  strcpy(a,x);
 }
 void display() {
  cout<< a<< endl;
 }
 String operator+(String x) {
  String obj;
  strcpy(obj.a,a);
  strcat(obj.a,x.a);
  return obj;
 }
 int operator==(String x) {
  int len1 = strlen(a);
  int len2 = strlen(x.a);
  return len1-len2;
 }	
};
int main() {
 String a,b;
 char name[30],sec[30];
 cout<<"Enter first String ";
 cin.getline(name,20);
 a.getString(name);
 cout<<"Enter Second String ";
 cin.getline(sec,30);
 b.getString(sec);
 a.display();
 b.display();
 String c;
 c = a+b;
 c.display();
 if((a==b)==0){
  cout<<"Same length\n";
 }
 else {
  cout<<"Different length\n";
 }
 return 0;
}
OUTPUT
Enter first String Alok
Enter Second String Deepu
Alok
Deepu
AlokDeepu
Different length

#include< iostream>
#include< string.h>
using namespace std;
class Time24 {
 public :
 int h, min;
 void show() {
  cout<< h<<" : "<< min<< endl;
 }
};
class Time12 { 
 public :
 int hr, m;
 string loc;
 Time12(int a, int b, string l) {
  hr = a;
  m = b;
  loc = l;
 }
 void show() {
  cout<< hr<<" :: "<< m<<" :: ";
  cout<< loc<< endl;
 }
 operator Time24() {
  Time24 ob;
  ob.h = 	hr;
  ob.min = m;
  if(loc=="pm") {
   ob.h = ob.h+12;
  }
  return ob;
 }
};
int main() {
 Time12 t(10,15,"pm") ;
 Time24 t2;
 t2 = (Time24)t;
 t.show();
 t2.show();
 return 0;
}
OUTPUT
10 :: 15 :: pm
22 : 15	

#include< iostream>
using namespace std;
class Distance {
 int m, cm;
 public:
 Distance(int a, int b) {
  m = a;
  cm = b;
 }
 void display() {
  cout<< m<<" m "<< cm<<" cm"<< endl;
 }
 Distance operator+(Distance d2) {
  int x, y;
  x = m+d2.m;
  y = cm+d2.cm;
  return Distance(x,y);
 }
 Distance operator+(int a) {
  int x, y;
  x = m;
  y = cm+a;
  return Distance(x,y);
 }
 friend Distance operator+(int a,Distance d);	
};
Distance operator+(int a, Distance d2) {
 int x, y;
 x = a+d2.m;
 y = d2.cm;
 return Distance(x,y);
}
int main() {
 Distance d(10,20);
 Distance d2(50,60);
 Distance d3 = d+d2;
 d3.display();
 Distance d4 = d+2;
 d4.display();
 Distance d5 = 2+d;
 d5.display();
 return 0;	
}
OUTPUT
60 m 80 cm
10 m 22 cm
12 m 20 cm

#include< iostream>
using namespace std;
class Complex1{
 int real, imag;
 public:
 Complex1(int r, int i) {
 real = r;
 imag = i;
 }
 Complex1() { }
 void display() {
  if(imag>= 0)
   cout<< real<<" +i "<< imag<< endl;
  else
   cout<< real<<" -i "<< imag<< endl;
 }
 friend Complex1 operator+(Complex1 ob1, Complex1 ob2) ;
 friend Complex1 operator-(Complex1 ob1, Complex1 ob2 ); 
};
Complex1 operator+(Complex1 ob1, Complex1 ob2) {
 int a = ob1.real+ob2.real;
 int b = ob1.imag+ob2.imag;
 return Complex1(a,b);
}
Complex1 operator-(Complex1 ob1, Complex1 ob2 ) {
 int a = ob1.real-ob2.real;
 int b = ob1.imag-ob2.imag;
 return Complex1(a,b);
}
int main() {
 int a,b,c,d;
 cout<<"Enter real & imag part of no 1: ";
 cin>>a>>b;
 cout<<"Enter real & imag part of no 2: ";
 cin>>c>>d;
 Complex1 c1(a,b),c2(c,d),c3,c4;
 c3 = c1+c2;
 c3.display();
 c4 = c1-c2;
 c4.display();
 return 0;
}
OUTPUT
Enter real and imag part of no 1: 10  10
Enter real and imag part of no 1: 5  15
15 +i 25
5 -i -5


Get it on Google Play


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved