You are here:

C++/C++ classes


QUESTION: I am trying to do a C++ program with 2 classes Employee and Company.

Employee has a member function called getempID, which reads in the Employee ID in the Employee class.

In Company class, I need to write a member function to compare the EmployeeID with any given ID, so it needs to use the same getempID member function of Employee class. Could you please explain how this could be done ? I have tried using the following

Employee* Company::getEmployee(char* id)
  int x = strcmp(Employee.getempID(),id);
  if (x == 0) return Employee;
In Employee class, the getempID function is as below

char* Employee::getempID()
  return EmployeeID;


ANSWER: > Employee has a member function called getempID ...
> char* Employee::getempID()
> {
>     return EmployeeID;
> }

this is not const-correct. the const-correct version would be:

   const char* Employee::getempID() const
         return EmployeeID;


> In Company class, I need to .....
> ...Could you please explain how this could be done ?

in class Company, have a collection of Employee objects and a member function to find an Employee given an EmployeeId. eg.

class Company
         const Employee* get_employee( const char* id ) const ;
         Employee* employee_collection[100] ;
         int num_employees ;          

implement the function to iterate through the collection and locate the Employee with the given id:

       const Employee* Company::get_employee( const char* id ) const
         for( int i = 0 ; i < num_employees ; ++i )
         const Employee* e = employee_collection[i] ;
         if( std::strcmp( e->getempID(), id ) == 0 )
         return e ;
         return 0 ; // not found

---------- FOLLOW-UP ----------

QUESTION: thank u so much for the answer, however i still have problems trying to create an Employee under Company class. The function is AddEmployee and I am trying to make this code work. The eNew is created correctly, but the array called employee_collection is not getting loaded with the correct EMployee data. please see my code below

void Company::addEmployee(char* fname, char* lname, char* id)
  Employee eNew(fname, lname, id);
     int x = EmployeeNumber;
  const Employee* e = employee_collection[x-1];     
Employee* employee_collection[0](fname, lname, id);
       e = &eNew;
  e -> printEmployee();
thank you

the array employee_collection contains *pointers* Employee objects, not the objects themselves. so you need Employee objects with a dynamic storage class (created using new). and to keep track of the number of Employee objects in the array by incrementing the counter num_employees.

void Company::addEmployee(char* fname, char* lname, char* id)
 if( num_employees < 100 ) // if there is space in the array
     Employee* eNew = new Employee(fname, lname, id);
     // ...
     employee_collection[num_employees] = eNew ;  
     ++num_employees ;
     enew-> printEmployee();
     // ...
   // no space to add more employees


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.