Q:

Write a C++ program for various Mathematical Operations using Switch case

0

Write a C++ program for various Mathematical Operations using Switch case

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream.h>
#include<conio.h>
#include<process.h>

class demo
{
                float a,b;

                public:
                void getdata();
                void display();
                demo operator +(demo);
                demo operator -(demo);
                demo operator *(demo);
                demo operator /(demo);
                int operator ==(demo);
};

void demo::getdata()
{
                cout<<“Enter values of a and b:”;
                cin>>a>>b;
}

void demo::display()
{
                cout<<“a=”<<a<<“tb=”<<b;
}

demo demo::operator +(demo d1)
{
                demo d2;
                d2.a=a+d1.a;
                d2.b=b+d1.b;
                return d2;
}

demo demo::operator -(demo d1)
{
                demo d2;
                d2.a=a-d1.a;
                d2.b=b-d1.b;
                return d2;
}

demo demo::operator *(demo d1)
{
                demo d2;
                d2.a=a*d1.a;
                d2.b=b*d1.b;
                return d2;
}

demo demo::operator /(demo d1)
{
                demo d2;
                d2.a=a/d1.a;
                d2.b=b/d1.b;
                return d2;
}

int demo::operator ==(demo d1)
{
                if((a==d1.a)&&(b==d1.b))
                                return 1;
                else
                                return 0;
}

int main()
{
                clrscr();
                int ch;
                demo d1,d2,d3;

                cout<<“First Object:n”;
                d1.getdata();
                cout<<“nSecond Object:n”;
                d2.getdata();

                cout<<“nnOperator Overloadig Menu”;
                cout<<“nn1.Additionn2.Subtractionn3.Multiplicationn4.Divisionn5.Comparisonn6.Exit”;
                cout<<“nnEnter your choice(1-6):”;
                cin>>ch;

                switch(ch)
                {
                                case 1:  d3=d1+d2;
                                                cout<<“nThird Object:n”;
                                                d3.display();
                                                break;

                                case 2:  d3=d1-d2;
                                                cout<<“nThird Object:n”;
                                                d3.display();
                                                break;

                                case 3: d3=d1*d2;
                                                cout<<“nThird Object:n”;
                                                d3.display();
                                                break;

                                case 4: d3=d1/d2;
                                                cout<<“nThird Object:n”;
                                                d3.display();
                                                break;

                                case 5: if(d1==d2)
                                                                cout<<“nObjects are Equal”;
                                                else
                                                                cout<<“nObjects are Not Equal”;
                                                break;

                                case 6: exit(0);
                                                break;

                                default:
cout<<“Wrong Choice!!!Press any key to exit”;
                                                 getch();
                }
                getch();
                return 0;


}
 
 

OUTPUT ::

addition, subtraction, multiplication, division and comparison

addition, subtraction, multiplication, division and comparison

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Classes and Objects Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to Compare Two Strings using Overloadi... >>
<< Write a C++ program to Swap two numbers using clas...