C++ Program To Make Calulator For Arithmetic Operator(+,_,*,/) OOP | Globaleducore.com

Write a C++ program create a calculator for an arithmetic operator (+, -, *, /). The program should take two operands from user and performs the operation on those two operands depending upon the operator entered by user. Use a switch statement to select the operation. Finally, display the result. Some sample interaction with the program might look like this: Enter first number, operator, second number: 10 / 3 Answer = 3.333333 Do another (y/n)? y Enter first number, operator, second number: 12 + 100 Answer = 112 Do another (y/n)? 










C++ Programme To Make Calculator For Arithmetic Operators 

SOL:

#include <iostream>
using namespace std;
class number
{
public : float a,b;
         char ch;                         
         char get();
 void display();
 float add();
 float sub();
 float mul();
 float div();
 number();
 number(int m,int n );
 number(number&n1);
 ~number();
};
number::~number()
{
cout<<"\n Constructor Is Destroyed";
}
number::number(int m,int n )
{
a=m;
b=n;
cout<<"\nparameterized const is called";
}
number::number(number &n1)
{
cout<<"\ncopy is called:";
a=n1.a;
b=n1.b;
}
char number:: get()
{
cout<<"\nEnter Any Two Nos.";
cin>>a>>ch>>b;
        return ch;
}
void number:: display()
{
cout<<"\nTwo Nos. are"<<"\n"<<a<<"\n"<<b;
}
float number:: add()
{
return a+b;
}
float number:: sub()
{
return a-b;
}
float number:: mul()
{
return a*b;
}
float number:: div()
{
return a/b;
}
number::number(){a=10 ;b=20;cout<<"\ndefault const is called";}
int main()
{
number n;
n.display();
char ch,op;
number n1(20,30);
n1.display();
number n2(n1);
n2.display();
do
{
ch=n.get();
switch(ch)
{
  case '+':
cout<<"\nAddition Is:"<<n.add();
break;
case '-':
cout<<"\nSubstraction Is:"<<n.sub();
break;
case '*':
cout<<"\nMultiplication Is:"<<n.mul();
break;
case '/':
cout<<"\nDivision Is:"<<n.div();
break;
}
cout<<"\ndo you want to continue (Y)";
cin>>op;
}
while(op=='Y'||op=='y');
}
Expected Output: default const is called
Two Nos. are
10
20
parameterized const is called
Two Nos. are
20
30
copy is called:
Two Nos. are
20
30
Enter Any Two Nos.10
50
do you want to continue (Y)

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget