using c++ Write a Class Calculator which has following members: Private variable
ID: 3914096 • Letter: U
Question
using c++
Write a Class Calculator which has following members:
Private variable – int operand1, int operand2
Define and declare all accessor and mutator functions.
Write 2 constructors for your class Calculator – default constructor and another constructor with 2 parametrs.
Public functions –
Addition(int, int) of return type void
Subtraction(int, int) of return type int
Multiplication(int, int) of return type int
Division(int, int) of return type double
Write a test program with user given inputs for operand1 = 11 and operand2 = 2. Call all the functions you declared and defined from your main test program.
Explanation / Answer
#include <iostream>
using namespace std;
class Calculator
{
private:
int operand1;
int operand2;
public:
//set and get methods
void setOperand1(int operand1)
{
this->operand1 = operand1;
}
int getOperand1()
{
return operand1;
}
void setOperand2(int operand2)
{
this->operand2 = operand2;
}
int getOperand2()
{
return operand2;
}
Calculator()// default constructor
{
operand1 = 0;
operand2 = 0;
}
Calculator(int operand1,int operand2)// argument constructor
{
this->operand1 = operand1;
this->operand2 = operand2;
}
// arithmetic functions
void Addition(int x,int y)
{
operand1 = x;
operand2 = y;
cout<<(operand1 + operand2);
}
int Subtraction(int x,int y)
{
operand1 = x;
operand2 = y;
return (operand1 - operand2);
}
int Multiplication(int x,int y)
{
operand1 = x;
operand2 = y;
return (operand1 * operand2);
}
double Division(int x,int y)
{
operand1 = x;
operand2 = y;
return ((double)operand1 / operand2);
}
};
int main() {
Calculator c;
cout<<"Addition of 11 and 2 : ";
c.Addition(11,2);
cout<<" Subtraction of 11 and 2 : "<<c.Subtraction(11,2);
cout<<" Multiplication of 11 and 2 : "<<c.Multiplication(11,2);
cout<<" Division of 11 and 2 : "<<c.Division(11,2);
return 0;
}
Output:
Addition of 11 and 2 : 13
Subtraction of 11 and 2 : 9
Multiplication of 11 and 2 : 22
Division of 11 and 2 : 5.5
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.