Pages

Wednesday, January 28, 2015

C++ class program to perform rational number arithmetic Operations using Operator Overloading

#include<stdio.h>          
 #include<iostream.h>
 #include<conio.h>
 class rational
 {
                 int numer;
                 int denom;
                 public:
                 void getdata()
                 {
                                 cout<<"\n enter the numerator part of the rational no.";
                                 cin>>numer;
                                 cout<<"\n enter the denominator part of the rational no.";
                                 cin>>denom;
                 }
                 void operator+(rational);
                 void operator-(rational);
                 void operator *(rational);
                 void operator /(rational);
 };
 void rational ::operator+(rational c1)
 {
                 rational temp;
                 temp.numer=(numer*c1.denom)+(c1.numer*denom);
                 temp.denom=denom*c1.denom;
                 cout<<"\nrational no. after addition";
                 cout<<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom;
 }
 void raional ::operator -(rational c1)
 {
                 rational temp;
                 temp.numer=(numer*c1.denom)-(c1.numer*denom);
                 temp.denom=denom*c1.denom;
                 cout<<"\n rational no. after subtraction";
                 cout<<"\n numerator="<<temp.numer<,"\n denominator ="<<temp.denom;
 }
 void rational ::operator (rational c1)
 {
                 rational temp;
                 temp.numer=numer*c1.numer;
                 temp.denom=denom*c1.denom;
                 cout<<"\n rational no. after multiplication";
                 cout <<"\n numerator="<temp.numer<<"\n denominator ="<< temp.denom;
 }
 void rational :: operator /(rational c1)
 {
                 rational temp;
                 temp.numer= numer*c1.denom;
                 temp.denom=c1.numer*denom;
                 cout<<"\n rational no. after division";
                 cout <<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom;
 }
 void main()
 {
                 clrscr();
                 rational c1, c2;
                 int n;
                 do
                 {
                                 cout<<"\n 1.Input data for rational no. ";
                                 cout<<"\n 2. Addition of rational no. ";
                                 cout<<"\n 3. Subtraction of rational no. ";
                                 cout<<"\n 4. Multiplication of rational no.";
                                 cout<<\n  5. Division of rational no. ";
                                 cout<<"\n 6. Quit";
                                 cout<<"\n Enter your choice";
                                 cin>>n;
                                 switch(n)
                                 {
                                                 case 1:
                                                 cout<<endl<<"\n enter the data for first rational no.";
                                                 c1.getdata();
                                                 cout<<endl<<"\n enter the data for second rational no. ";
                                                 c2.getdata ();
                                                 clrscr();
                                                 break;
                                                 case 2;
                                                 c1+c2;
                                                 getch();
                                                 clrscr();
                                                 break;
                                                 case 3;
                                                 c1-c2;
                                                 getch();
                                                 clrscr();
                                                 case 4:
                                                 c1*c2;
                                                 getch();
                                                 clrscr();
                                                 break;
                                                 case 5:
                                                 c1/c2;
                                                 getch();
                                                 clrscr();
                                                 break;
                                                 case 6:
                                                 exit(1);
                                                 break;
                                 }
                 } while (n!=6);
                 getch();
 }


Thursday, January 1, 2015

Operator Overloading in C++ to add,substract,multiply and divide two Complex Numbers

Operator Overloading is a technique of polymorphism by which an operator(+,- etc) can be used to do different types of operations. eg:+ can be used to add two integers say a and b using sum=a+b similarly two floating point numbers say fa,fb by fs=fa+fb. In this example +,-,*,- operators are overloaded to add,subtract, multiply and divide two complex numbers. 

#include<iostream.h> 
 #include<conio.h> 
 class complex 
 { 
 int a,b; 
 public: 
 void read() 
 { 
 cout<<"\n\nEnter the REAL PART : "; 
 cin>>a; 
 cout<<"\n\nEnter the IMAGINARY PART : "; 
 cin>>b; 
 } 
 complex operator +(complex c2) 
 { 
 complex c3; 
 c3.a=a+c2.a; 
 c3.b=b+c2.b; 
 return c3; 
 } 
 complex operator -(complex c2) 
 { 
 complex c3; 
 c3.a=a-c2.a; 
 c3.b=b-c2.b; 
 return c3; 
 } 
 complex operator *(complex c2) 
 { 
 complex c3; 
 c3.a=(a*c2.a)-(b*c2.b); 
 c3.b=(b*c2.a)+(a*c2.b); 
 return c3; 
 } 
 complex operator /(complex c2) 
 { 
 complex c3; 
 c3.a=((a*c2.a)+(b*c2.b))/((c2.a*c2.a)+(c2.b*c2.b)); 
 c3.b=((b*c2.a)-(a*c2.b))/((c2.a*c2.a)+(c2.b*c2.b)); 
 return c3; 
 } 
 void display() 
 { 
 cout<<a<<"+"<<b<<"i"; 
 } 
 }; 
 void main() 
 { 
 complex c1,c2,c3; 
 int choice,cont; 
 do 
 { 
 clrscr(); 
 cout<<"\t\tCOMPLEX NUMBERS\n\n1.ADDITION\n\n2.SUBTRACTION\n\n3.MULTIPLICATION\n\n4.DIVISION"; 
 cout<<"\n\nEnter your choice : "; 
 cin>>choice; 
 if(choice==1||choice==2||choice==3||choice==4) 
 { 
 cout<<"\n\nEnter the First Complex Number"; 
 c1.read(); 
 cout<<"\n\nEnter the Second Complex Number"; 
 c2.read(); 
 } 
 switch(choice) 
 { 
 case 1     : c3=c1+c2; 
            cout<<"\n\nSUM = "; 
            c3.display(); 
            break; 
 case 2     : c3=c1-c2; 
            cout<<"\n\nResult = "; 
            c3.display(); 
            break; 
 case 3 : c3=c1*c2; 
            cout<<"\n\nPRODUCT = "; 
            c3.display(); 
            break; 
 case 4     : c3=c1/c2; 
            cout<<"\n\nQOUTIENT = "; 
            c3.display(); 
            break; 
 default     : cout<<"\n\nUndefined Choice"; 
 } 
 cout<<"\n\nDo You Want to Continue?(1-Y,0-N)"; 
 cin>>cont; 
 }while(cont==1); 
 getch(); 
 }