Explain this program...

Write your question here.


#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();
}


Kindly explain this program ...
Last edited on
Hi chervil

Why object b of sum value is get zero?

Kindly explain...
Why object b of sum value is get zero?

http://www.cplusplus.com/forum/general/141951/#msg749505
Dear Chervil

Thanks for your response

I have one doubt Why c.addition get value 41
But b=c.addition get value zero.

Kindly explain please

Thanks in advance...
Your code has:
b = c.addition();
That assigns something to object b. The b has type class add.

The object c has type class add.
Function add::addition() returns an object of type class add.
Assigning add to add is copy assignment.

Compiler generates copy assignment operator automatically. It will set b.sum equal to member sum of the object returned by add::addition().

How does your add::addition() prepare the returned object?
1
2
3
4
5
6
add add::addition()
{
  add fubar(5,6);
  this->sum = this->num1 + this->num2 + fubar.num1 + fubar.num2;
  // WHAT HAPPENS HERE?
}

We have no idea. Your function should return a value. There is no return statement.
Dear keskiverto

add addition()
{
add a(5,6);
sum=num1+num2+a.num1+a.num2;
return a;
}

Now I have added return statement...

The value of Object b is 5 and 6
But sum value get zero

Please explain
Please, do use the code tags. See http://www.cplusplus.com/articles/jEywvCM9/

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

Your observation is that the member sum of the object returned by function add::addition() has value 0.

The function returns local object a. The a.sum must thus be 0 at line 5.

Where was it set/changed? Does line 4 change a.sum? If not, what does?
Dear Ksekiverto

Thanks a lot

Sorry for frequently disturbed..

Why Object C get the value sum is 41

Please explain...
You do call member function addition() of object c.

The code of that function is on my previous post.
What does happen on line 3?
What does happen on line 4?

What member variables does object of type class add have?
What were the values of the member variables of object c before calling the function?
Topic archived. No new replies allowed.