Thread: Private Data Access

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Private Data Access

    Say I have a class like this:

    Code:
    class Space{
    
    public:
    	Constructor();   // constructor
    	
    	~Constructor();
    	
    
    private:
    	
    	Kids*P[3];
    	
    
    }; // end class Space
    and another class like this
    Code:
    class Kids{
    
    public:
    	Constructor2();   // constructor
    	
    	~Constructor2();
    	
    
    private:
    	
    	      int friends;
                int familyMember;
    
    }
    if i had a member function of class Space how could i print what the array of pointers to class Kids points to from that member function in class Space?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Have a method in Kids that either returns the data, or prints it:
    (the point of private encapsulation is that you cannot (shall not) directly access the data)
    Code:
    class Space
    {
       public:
          Space();	
          ~Space();	
    
       private:	
          Kids* P[3];
    };
    
    class Kids
    {
       public:
          Kids();
          ~Kids();
    
          int getFriends();
          int getFamilyMember();
          void printData();
    
       private:	
          int friends;
          int familyMember;
    }
    Btw, the constructor and the destructor have the same name as the class, not "Constructor".
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    2
    I did what your saying and i sent as one of the parameters to printdata something like this printData(Space A)
    and inside print i did this:

    A.P[index]->friends but it gives me some type of error like segmentation or something

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    PrintData should print its own values, like this:
    Code:
    void Kids::PrintData()
    {
       cout << friends << endl;
       cout << familyMember << endl;
    }
    Then, in the Space class you have a loop tha runs this method for all your kids objects.
    Code:
    void Space::PrintAllKids()
    {
       for(int i=0; i<NrOfKids; i++)
       {
          P[i].PrintData();
       }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 03:57 PM
  2. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  3. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Stack or Heap Private Data...?
    By GrNxxDaY in forum C++ Programming
    Replies: 4
    Last Post: 08-09-2002, 05:13 AM