Write a program that consists of a main function and a class called Number. The
ID: 3548930 • Letter: W
Question
Write a program that consists of a main function and a class called Number.
The Number class should allow a class instance to add to itself the member-variable values of another class instance.
For example, let x and y both be instances of the Number class.
Then x.Add(y) will have the affect such that whatever value y holds, that value will then be added to x.
Implement Add, Subtract, and Multiply. The member-variables x and y shall hold integer values. In the main function, you should design and implement the necessary code to exercise the Number class member function thoroughly.
Explanation / Answer
#include<iostream>
using namespace std;
class Number
{
public:
int a;
Number(int f)
{
a=f;
}
void Add(Number I);
void Sub(Number I);
void Mul(Number I);
void Div(Number I);
};
void Number::Add(Number I)
{
int sum;
sum=a+I.a;
cout<<sum<<" ";
}
void Number::Sub(Number I)
{
int sub;
sub=a-I.a;
cout<<sub<<" ";
}
void Number::Mul(Number I)
{
int mul;
mul=a*I.a;
cout<<mul<<" ";
}
void Number::Div(Number I)
{
float div;
div=(float)a/I.a;
cout<<div;
}
int main()
{
Number x(5);
Number y(10);
x.Add(y);
x.Sub(y);
x.Mul(y);
x.Div(y);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.