Write a class called “arithmetic” having two integer and one character data members. It performs the operation on its integer members indicated by character member (+, -, *, /). For example * indicates multiplication on data members as d1 * d2. Write a class with all necessary constructors and methods to perform the operation and print the operation performed in format Ans = d1 op d2. Test your class using main().
Code :
Output :
Code :
#include<iostream.h>
#include<conio.h>
class arithmetic
{
int a,b;
char op;
public:
arithmetic()
{
cout<<"Give the value of A and B : ";
cin>>a>>b;
cout<<"\nSelect Operation (+,-,*,/) : ";
cin>>op;
}
void show()
{
cout<<"\n A "<<op<<" B :== "<<a<<" "<<op<<" "<<b<<" :== ";
if(op == '+')
{
cout<<a+b;
}
else if(op == '-')
{
cout<<a-b;
}
else if(op == '*')
{
cout<<a*b;
}
else if(op == '/')
{
cout<<a/b;
}
else
{
cout<<"\nERROR : INVALID CHARACTER";
}
}
};
int main()
{
clrscr();
arithmetic obj;
obj.show();
getch();
return 0;
}
Output :
No comments:
Post a Comment