Sep 7, 2014 at 6:41am Sep 7, 2014 at 6:41am UTC
#include<iostream.h>
#include<conio.h>
class add
{
int num1,num2,sum;
public:
add()
{
cout<<"\n Constructor without parameters";
num1='\0';
num2='\0';
sum='\0';
}
add (int s1,int s2)
{
cout<<"\n Parameterized constructor";
num1=s1;
num2=s2;
sum=NULL;
}
add (add &a)
{
cout<<"\n Copy constructor";
num1=a.num1;
num2=a.num2;
sum=NULL;
}
void getdata()
{
cout<<"Enter data";
cin>>num1>>num2;
}
void addition(add b)
{
sum=num1+num2+b.num1+b.num2;
}
add addition()
{
add a(5,6);
sum=num1+num2+a.num1+a.num2;
return a;
}
void putdata()
{
cout<<"\n The numbers are";
cout<<num1<<'\t'<<num2;
cout<<"\n The sum of the numbers are"<<sum;
}
};
void main()
{
clrscr();
add a,b(10,20),c(b);
a.getdata();
a.addition(b);
b=c.addition();
c.addition();
cout<<"\n Object a:";
a.putdata();
cout<<"\n Object b";
b.putdata();
cout<<"\n Object c";
c.putdata();
getch();
}
Hi All
when the program executed the Object b only return 0. Can any one tell me why the the Object b is returned 0. I want this program for constructor overloading.
Sep 7, 2014 at 9:12am Sep 7, 2014 at 9:12am UTC
A large part of programming is spent debugging and it's also the best way of learning. This question is too broad, test it every few lines and ensure the values are as expected.
I just looked through it and your compiler should be telling you what's wrong. Functions with no return value must use void. There's a lot of things wrong/bad, do one thing at a time and test it.
Last edited on Sep 7, 2014 at 9:15am Sep 7, 2014 at 9:15am UTC
Sep 7, 2014 at 12:41pm Sep 7, 2014 at 12:41pm UTC
Dear Keskiverto
I have modified your code. But compilers display the following output....
Object b:
The numbers are 5 6
The sum of the numbers are 0
Why compiler display zero (0) for object b:
Kindly explain this....
Sep 7, 2014 at 12:53pm Sep 7, 2014 at 12:53pm UTC
Not "for object b". It is "why the value of b.sum is 0?"
b=c.addition();
In other words:
add x(5, 6);
b =x;
Why is b.sum 0?
Because x.sum is 0.
Sep 7, 2014 at 1:50pm Sep 7, 2014 at 1:50pm UTC
Dear Keski
First of all thanks for your response...
Can you explain line by line. If you free.....