array of classes in C++

This is a discussion on array of classes in C++ within the C++ Programming forums, part of the General Programming Boards category; After i create an array of a class object how do i access a private member of an element of ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    7

    Exclamation array of classes in C++

    After i create an array of a class object how do i access a private member of an element of the array ??

    example:

    class student
    {private:
    int ID;
    char name;

    public:
    get_data();
    print();
    };

    student stan[10];

    how do i access stan[2].id if i want to change that id # alone ?

    I would appreciate the help.

    thanks

    Stan

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Create another function in your public section of your class that changes the id # passed to it, or which gets it from the user(whatever you want). Then use it just like you said before:
    Code:
    stan[2].changeid;
    [edit]
    Code:
    class student 
    {private: 
               int  ID; 
               char name; 
    
    public: 
               get_data(); 
               print(); 
               void changeid(int &idnum);
    };
    
    void Student::changeid(int &idnum){
             ID = idnum;
    }
    
    int main(){
             Student stan[10];
             stan[2].changeid(546);
             return(0);
    }
    [/edit]
    Last edited by Traveller; 05-29-2002 at 08:22 PM.

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    You need a set/get member function pair. Something like this:

    Code:
    //in your student.h
    class student
    {
    private:
      int ID;
      char name;
    public:
      student();
      ~student();
      void SetID(int NewID);
      int GetID();  
    };
    
    //in your student.cpp
    void student::SetID(int NewID)
    {
       if(NewID>0) //some form of error checking (probably should check against duplicates, too
       {
          ID=NewID;
       }   
    }
    
    int student::GetID()
    {
       return(ID);
    }
    
    //in you main *.cpp file
    student stan[10];
    
    int WhoIsIt=stan[0].GetID();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Char array and classes
    By Heineken in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2005, 12:32 PM
  4. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

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