Need Help for Retrieving Value of an Private Member Variable of a Class.

This is a discussion on Need Help for Retrieving Value of an Private Member Variable of a Class. within the C++ Programming forums, part of the General Programming Boards category; This code doesn't work as I expect. The return value of privte member variables are always garbage . Can every ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    25

    Need Help for Retrieving Value of an Private Member Variable of a Class.

    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;
    }
    Last edited by trongsi; 04-02-2006 at 08:57 PM.

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    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.


    Look here:
    Code:
    ...
    
    record student;
    mediate();
    
    n1=student.geta();
    n2=student.getb();
    
    ...
    See the problem?
    Last edited by BMJ; 04-02-2006 at 04:42 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    25
    Quote 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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Can't Access the private member from base class
    By planet_abhi in forum C# Programming
    Replies: 3
    Last Post: 01-09-2006, 03:30 AM
  4. Need help with a class variable.
    By RealityFusion in forum C++ Programming
    Replies: 11
    Last Post: 10-20-2005, 10:26 AM
  5. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21