Addition of two numbers

Hi,

Addition of two numbers

Code is below

#include<iostream.h>
#include<conio.h>
class addition
{
private:
int a,b,c;
public:
int getdata(int a,int b)
{
int c;
c=a+b;
return c;
}
int dispdata()
{
cout<<"The result of addition is "<<endl<<c;
return 0;
}
};
int main()
{
addition a1;
a1.getdata(2,3);
a1.dispdata();
getch();
return 0;
}

Output result is displayed as 24.

In Above program,iam geeting data(2,3) and displays the result but result is displayed as 24 instead of 5?confused where did i get wrong




Last edited on
You didn't assign the variable
c
of your class with the result in your getdata function, instead you retruned a local variable c so the real c is left uninitialized with random value.

when you call dispdata method undefined result is dispaled.

for example to reimplmemnt your getdata method, this is one way:
1
2
3
4
void getdata(int a,int b)
{
	c = a + b; // assign the real c of real object
}


Also use the code tags next time to format your code :)
Thanks,iam so silly : ) can't think of it....

Will use code tags next time while posting
Topic archived. No new replies allowed.