0% found this document useful (0 votes)
47 views3 pages

16A Operator Overloading-All Programs

The document discusses operator overloading in C++ through examples of overloading operators like +, -, * for a complex number class and ++, -- for an index class. It also shows an example of overloading the + operator to concatenate two string objects by overloading the + operator for a string class. The examples demonstrate how operator overloading allows defining behavior of operators while working with user-defined classes like complex, index and string.

Uploaded by

darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

16A Operator Overloading-All Programs

The document discusses operator overloading in C++ through examples of overloading operators like +, -, * for a complex number class and ++, -- for an index class. It also shows an example of overloading the + operator to concatenate two string objects by overloading the + operator for a string class. The examples demonstrate how operator overloading allows defining behavior of operators while working with user-defined classes like complex, index and string.

Uploaded by

darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PDPU, Gandhinagar 16 Operator Overloading-All Programs’ Listing

Opovrld1.cpp
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class complex
{
private:
float real,imag;
public:
complex(){};
complex(float r,float i) { real = r; imag = i;};
void getdata()
{
float r,i;
cout << "Enter Real & Imaginary Part\n";
cin >> r >> i;
real = r; imag = i;
}
void setdata(float r,float i) { real = r; imag = i;};
void displaydata(char * nm)
{
cout << "Complex " << nm << endl;
cout << "Real : " << real << endl;
cout << "Imaginary : " << imag << endl;
}
complex operator + ( complex c)
{
complex t;
t.real = real + c.real;
t.imag = imag + c.imag;
return t;
}
complex operator * ( complex c)
{
complex t;
t.real = real * c.real - imag * c.imag;
t.imag = real * c.imag + imag * c.real;
return t;
}
void operator - () // use of unary minus operator overloading.
{
real = -real;
imag = -imag;
}
};
void main()
{
clrscr();
complex c1,c2(1.5,-2.5),c3,c4;
c1.setdata(2.0,2.0);
c3 = c1 + c2;
c3.displaydata("c3");
c4.getdata();
complex c5(2.5,3.0),c6;
c6 = c4 * c5;
c6.displaydata("c4");
complex c7;
c7 = c1 + c2 * c3;
c7.displaydata("c7");
-c7;
c7.displaydata("c7");
getch();
}

By Darshit Shah 1
PDPU, Gandhinagar 16 Operator Overloading-All Programs’ Listing

Opovrld2.cpp
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class index
{
private:
int count;
public:
index()
{
count = 0;
}
index ( int i )
{
count = i;
}
// PREFIX NOTATION.
index operator ++ ( )
{
return index ( ++count) ;
}
// POSTFIX NOTATION.
index operator ++ (int)
{
return index (count ++) ;
}
void showdata()
{
cout << count;
}
};

void main()
{
clrscr();
index c,d,e,f;

e = ++c;
cout << "After execution of e = ++c, we have c = " ;
c.showdata();
cout << " and e = ";
e.showdata();
cout << endl;

f = d++;
cout << "After execution of f = d++, we have d = " ;
d.showdata();
cout << " and f = ";
f.showdata();
cout << endl;
f++;
cout << "After execution of f++, we have f = " ;
f.showdata();
cout << endl;
++f;
cout << "After execution of ++f, we have f = " ;
f.showdata();

getch();
}

By Darshit Shah 2
PDPU, Gandhinagar 16 Operator Overloading-All Programs’ Listing

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

class string
{
private:
char * p;
int len;
public:
string(){ len = 0; p = NULL;};
string (const char * s);
string (const string & s); // copy constructor
~string() { delete p;};

// + operator
friend string operator+(const string &s, const string &t);
friend void show(const string s);
};
string::string(const char * s)
{
len = strlen(s);
p = new char[len+1];
strcpy(p,s);
};
string::string(const string & s)
{
len = s.len;
p = new char[len+1];
strcpy(p,s.p);
}

// overloading + operator
string operator+(const string & s, const string & t)
{
string temp;
temp.len = s.len + t.len;
temp.p = new char [temp.len+1];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
return temp;
};
void show(const string s)
{
cout << s.p << endl;
};
void main()
{
clrscr();
string s1 = "Institute of Petroleum Technologies -";
string s2 = "Gandhinagar.";
string s3 = s1 + s2;
// cout << s1 << endl;
// cout << s2 << endl;
// cout << s3 << endl;
show(s1);
show(s2);
show(s3);
getch();
};

By Darshit Shah 3

You might also like