How this program executed? Pls Explain..

Sep 3, 2014 at 3:00am
#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;
}
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();
}
Sep 3, 2014 at 3:06am
This is not a valid C++ program, it does not compile.
Sep 3, 2014 at 3:31am
this is language C.. as far as i understand it's creating 3 add constructor in add function which.. that's a valid function.. which means every time you call add class it will automatically run..
Last edited on Sep 3, 2014 at 3:31am
Sep 3, 2014 at 3:39am
This is not a valid C program either. Neither C nor C++ has the headers <conio.h> or <iostream.h>. C++ does, however, have <iostream>.
Last edited on Sep 3, 2014 at 3:39am
Sep 3, 2014 at 4:25am
I guess you could say it's valid circa 1995 C++. It just needs some NEAR pointers and for loops without proper scoping to fully recreate the experience.
Last edited on Sep 3, 2014 at 4:27am
Sep 3, 2014 at 12:48pm
Hi All...

When The above program is executed.

The following output is displayed.

Constructor without parameters
Parmeterized constructor
Copy Constructor
Enter data
2
3

Copy Constructor
Paramerized Constructor
Parameterized Constructor
Object a:
The numbers are 2 3
The sum of the numbers are 35
Object b
The numbers are 4253 12290
The sum of the numbers are 0
Object c
The numbers are 10 20
The sum of the numbers are 41


Kindly explain the above output only..... (Bold type)


Sep 3, 2014 at 1:22pm
It should compile enough, if one discards/fixes non-standard and deprecated bits.

If you do go through the code step by step, then you can track each value. Simple mental exercise. Compiler should warn about the step that adds undefined behaviour to some results.
Sep 3, 2014 at 1:27pm
Hi Keskiverto

Why second object b values are assign 4253 12290

pls explain........
Sep 3, 2014 at 2:59pm
What does b=c.addition(); do?
Topic archived. No new replies allowed.