This code doesn't work as I expect. The return value of privte member variables are always garbage .![]()
![]()
![]()
Can every body help me out !
Thanks in advandce.
Code:/* elusive problem*/ #include<iostream> using namespace std; class record { private: int a,b; public: void set(); int geta(); int getb(); }; //------------------------------------------------------ void record::set() { cout<<"Please Enter the Numbers.\n"; cout<<"\nFirst Number: "; cin>>a; cout<<"\nSecond Number: "; cin>>b; } //______________________________ int record::geta() { return a; } //______________________________ int record::getb() { return b; } //______________________________ void mediate() // used to set values to a & b and retrieve them at frist time. { int num1,num2; record student; student.set(); //set values to private a & b num1=student.geta(); num2=student.getb(); cout<<"Retrieve private variables a & b through mutators geta() & getb() :\n"; cout<<num1<<" "<<num2<<endl; //no problem when retrieving values of a & b here... } int main() { int n1,n2; record student; mediate(); //values of a & b can not be retrieved here. Why do they return the meaningless number ? n1=student.geta(); n2=student.getb(); cout<<"Retrieve private variables second time :\n"; cout<<n1<<" "<<n2<<endl; return 0; }



LinkBack URL
About LinkBacks




