Thread: Array of classes with classes inside

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Question Array of classes with classes inside

    I have an array of (Student)classes created in Manager.h, which contains a new instance of class Name (name),(in Student.h)How would I go about accessing the SetFirstName method in Name.cpp if I was in a class Manager.cpp? I have tried using
    Code:
    Students[i].name.SetFirstName("name");
    but since name is private it is not possible... is there any way to do this??
    Thanks,
    Code:
    // In Manager.h
    #include"Student.h"
    class Manager
    {
    	static const unsigned int max = 10000;
    	Student Students[max];
    public:
    	bool b_running;
    	Manager();
    	bool AddNewStudent();
    	
    
    };
    Code:
    // In Student.h
    
    #include "Name.h"
    
    class Student
    {
    	Name			name;
    	
    public:
    	
    	Student();
    	
    	
    };
    Code:
    //in Name.cpp
    #include "Name.h"
    
    Name::Name(){
    
    }
    Name::~Name(){
     
    }
    
    void Name::SetFirstName(char* fName){
    	firstName = fName;
    }
    Code:
    // In Name.h
    class Name
    {
    	char* firstName;
    	
    public:
    	Name();
    	~Name();
    	void SetFirstName(char*);
    		
    };

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First why are you using a C-string instead of a std::string for the firstName?

    Have you thought about using inheritance?

    Code:
    class Student : public Name
    {
    
       public:
          Student();
    };

    Jim

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by jimblumberg View Post
    Have you thought about using inheritance?

    Jim
    What a strange idea... "Each student HAS a name", not "each student IS a name"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    True but I would have named "Name" "Person".

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  3. structs inside classes
    By *DEAD* in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2007, 11:54 PM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  5. Classes inside Classes
    By LostGeneration in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2003, 12:02 PM