Find the errors in this programming...

#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.
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
I see that you have posted a new thread rather than continued the previous with this same program: http://www.cplusplus.com/forum/general/141703/

I also noticed that nobody has told you to use code tags. See http://www.cplusplus.com/articles/jEywvCM9/
Tags and (sane) indentation make a code post much more readable.


1
2
3
4
5
6
add add::addition()
{
  add a(5,6);
  sum = num1 + num2 + a.num1 + a.num2;
  return a;
}

Questions for you:
1. What variables are modified by this function?
2. What does the returned value contain?
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....
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.
Dear Keski

First of all thanks for your response...

Can you explain line by line. If you free.....
Your code:
1
2
3
4
5
6
add add::addition()
{
  add a(5,6);
  sum = num1 + num2 + a.num1 + a.num2; // This line does not change a
  return a;
}

The return value is thus same as add(5,6).
Topic archived. No new replies allowed.