Write a Cpp program to read three number and add two numbers among three, display them. Use multiple constructor and allocate memory dynamically?

All QuestionsCategory: Cpp LanguageWrite a Cpp program to read three number and add two numbers among three, display them. Use multiple constructor and allocate memory dynamically?
Chetan Shidling Staff asked 5 years ago

I need short information.

1 Answers
Chetan Shidling Staff answered 5 years ago

Code:

#include <iostream>
using namespace std;
class integer
{
int *x;
int *y;
int *z;
public:
integer(int z1)
{
z = new int;
*z = z1;
}
integer(int x1, int y1)
{
x = new int;
*x = x1;
y = new int;
*y = y1;
}
void add()
{
int sum;
sum = *x + *y;
cout<<“Sum of 2 nos”<<“\t”<<sum;
}
void sum()
{
cout<<“Z is”<<“\t”<<*z;
}
};
int main()
{
integer j(10, 20);
integer k(40);
k.sum();
j.add();
return 0;
}

Output:

Z is : 40
Sum of 2 nos : 30