PDPU, Gandhinagar 16 Operator Overloading-All Programs’ Listing
[Link]
#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;
[Link] = real + [Link];
[Link] = imag + [Link];
return t;
}
complex operator * ( complex c)
{
complex t;
[Link] = real * [Link] - imag * [Link];
[Link] = real * [Link] + imag * [Link];
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;
[Link](2.0,2.0);
c3 = c1 + c2;
[Link]("c3");
[Link]();
complex c5(2.5,3.0),c6;
c6 = c4 * c5;
[Link]("c4");
complex c7;
c7 = c1 + c2 * c3;
[Link]("c7");
-c7;
[Link]("c7");
getch();
}
By Darshit Shah 1
PDPU, Gandhinagar 16 Operator Overloading-All Programs’ Listing
[Link]
#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 = " ;
[Link]();
cout << " and e = ";
[Link]();
cout << endl;
f = d++;
cout << "After execution of f = d++, we have d = " ;
[Link]();
cout << " and f = ";
[Link]();
cout << endl;
f++;
cout << "After execution of f++, we have f = " ;
[Link]();
cout << endl;
++f;
cout << "After execution of ++f, we have f = " ;
[Link]();
getch();
}
By Darshit Shah 2
PDPU, Gandhinagar 16 Operator Overloading-All Programs’ Listing
[Link]
#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 = [Link];
p = new char[len+1];
strcpy(p,s.p);
}
// overloading + operator
string operator+(const string & s, const string & t)
{
string temp;
[Link] = [Link] + [Link];
temp.p = new char [[Link]+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