How this program executed? Pls Explain..

#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();
}
This is not a valid C++ program, it does not compile.
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
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
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
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)


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.
Hi Keskiverto

Why second object b values are assign 4253 12290

pls explain........
What does b=c.addition(); do?
Topic archived. No new replies allowed.