CS Electrical And Electronics
@cselectricalandelectronics

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?
CS Electrical And Electronics Staff asked 4 years ago

I need short information.

1 Answers
CS Electrical And Electronics Staff answered 4 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