+ 4
I'm having a compilation error in this code, please can you solve this..
#include <iostream> using namespace std; class item { int sum,a,b; public: void getdata(); void sum() { sum=a+b; } }; void item :: getdata() { cout<<"enter value"; cin<<a<<b; } int main() { item x; cout<<"\nobject x "<<"\n"; x.getdata(); void sum(); cout<<sum; return 0; }
4 Answers
+ 5
I finally got it.......
#include <iostream>
using namespace std;
class xyz
{
int a,b,sum;
public:
xyz()
{
cin>>a>>b;
sum=a+b;
}
void disp();
};
void xyz::disp()
{
cout<<"sum="<<sum;
}
int main()
{
xyz i;
i.disp();
return 0;
}
+ 4
Thanks....
+ 2
Can you post the error message?
You cannot acces the member function sum by "void sum()"
The member function sum should have a return value:
int sum(){
return a+b;
}
int sum = x.sum();
cout<<sum;
0
you variable sum and method sum bot have same identifier.
change method sum to getsum() and it will work fine