google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Write a C++ program using class to overload following binary operators

Monday, February 23, 2015

Write a C++ program using class to overload following binary operators

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

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...