Originally Posted by
BMJ
They're returning garbage because they're uninitialized. You didn't set them. You have two instances of the "record" object, one in main() and one in mediate(). They are not the same object, even though they have the same name.
So again, you have
2 "record" objects in your program. You called them both "student" but they're in different scopes.
Either make "student" global, or initialize your second "student" object.
Yes, Yes, Yes, I got it. Thank you so much.
So, if I want to do so, I must place the argument for mediate(record& student) function, is it correct.
I'm appriciated.
Code:
/* Solved problem
BIG THANKS TO BMJ */
#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(record& student)
// used to set values to a & b and retrieve them at frist time.
{
int num1,num2;
student.set(); //set values to private a & b
num1=student.geta();
num2=student.getb();
cout<<"Retrieve private variables a & b throung mutators geta() & getb() :\n";
}
int main()
{
int n1,n2;
record student;
mediate(student);
//Now, It works... :)
n1=student.geta();
n2=student.getb();
cout<<"Retrieve private variables second time :\n";
cout<<n1<<" "<<n2<<endl;
return 0;
}