Write a C++ program using class to overload following binary operators (+,-,*,/).
#include<iostream.h>
#include<conio.h>
class number
{ int a;
public:void get();
void show();
number operator +(number);
number operator -(number);
number operator *(number);
number operator /(number);
};
void number:: get()
{
cout<<"\nenter value";
cin>>a;
}
void number:: show()
{
cout<<a;
}
number number::operator+(number n2)
{
number t;
t.a=a+n2.a;
return t;
}
number number::operator-(number n2)
{
number t;
t.a=a-n2.a;
return t;
}
number number::operator*(number n2)
{
number t;
t.a=a*n2.a;
return t;
}
number number::operator/(number n2)
{
number t;
t.a=a/n2.a;
return t;
}
int main()
{
number n1,n2,n3;
clrscr();
n1.get();
n2.get();
cout<<"\n First num ";
n1.show();
cout<<"\n Second num ";
n2.show();
n3=n1+n2;
cout<<"\n Addition is ";
n3.show();
n3=n1-n2;
cout<<"\n Subtraction is ";
n3.show();
n3=n1*n2;
cout<<"\n Multiplication is ";
n3.show();
n3=n1/n2;
cout<<"\n Division is ";
n3.show();
getch();
return 0;
}
===================================================
OUTPUT
Enter value4
Enter value2
First num4
Second num 2
Addition is 6
Subtraction is 2
Multiplication is 8
Division is 2
#include<iostream.h>
#include<conio.h>
class number
{ int a;
public:void get();
void show();
number operator +(number);
number operator -(number);
number operator *(number);
number operator /(number);
};
void number:: get()
{
cout<<"\nenter value";
cin>>a;
}
void number:: show()
{
cout<<a;
}
number number::operator+(number n2)
{
number t;
t.a=a+n2.a;
return t;
}
number number::operator-(number n2)
{
number t;
t.a=a-n2.a;
return t;
}
number number::operator*(number n2)
{
number t;
t.a=a*n2.a;
return t;
}
number number::operator/(number n2)
{
number t;
t.a=a/n2.a;
return t;
}
int main()
{
number n1,n2,n3;
clrscr();
n1.get();
n2.get();
cout<<"\n First num ";
n1.show();
cout<<"\n Second num ";
n2.show();
n3=n1+n2;
cout<<"\n Addition is ";
n3.show();
n3=n1-n2;
cout<<"\n Subtraction is ";
n3.show();
n3=n1*n2;
cout<<"\n Multiplication is ";
n3.show();
n3=n1/n2;
cout<<"\n Division is ";
n3.show();
getch();
return 0;
}
===================================================
OUTPUT
Enter value4
Enter value2
First num4
Second num 2
Addition is 6
Subtraction is 2
Multiplication is 8
Division is 2