Array of classes with classes inside

This is a discussion on Array of classes with classes inside within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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
    2,713
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    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, 03: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, 11:02 AM

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