8. Write a C++ program using class to overload following binary operators (+,-,*,/).


#include<iostream.h>
#include<conio.h>
class arithmatic
{
float n;
public:
void get()
{
cout<<“\nEnter number: \n”;
cin>>n;
}
arithmatic operator+(arithmatic &a)
{
arithmatic t;
t.n=n+a.n;
return t;
}
arithmatic operator-(arithmatic &a)
{
arithmatic t;
t.n=n-a.n;
return t;
}
arithmatic operator*(arithmatic &a)
{
arithmatic t;
t.n=n*a.n;
return t;
}
arithmatic operator/(arithmatic &a)
{
arithmatic t;
t.n=n/a.n;
return t;
}
void display()
{
cout<<n;
}
};
void main()
{
arithmatic a1,a2,a3;
clrscr();
a1.get();
a2.get();
a3=a1+a2;
cout<<“\n\nAddition of two number: “;
a3.display();
a3=a1-a2;
cout<<“\n\nSubtraction of Two number: “;
a3.display();
a3=a1*a2;
cout<<“\n\nMultiplication of two number: “;
a3.display();
a3=a1/a2;
cout<<“\n\nDivision of two number: “;
a3.display();
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s