-1

the header:

#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include "Date.h"
#include "String.h"

class Employee {
private:
    String firstname;
    String lastName;
    const Date birth;
    const int id;
    const Date start;
    double salary;
    int status;
public:
    Employee();
Employee(char* firstname,char* lastname,Date birth,int id,Date start,double salary,int status);
virtual ~Employee();
};

the cpp:

#include "Employee.h"
#include "Date.h"
#include "String.h"


Employee::Employee() :id( 0 ) {
    salary=0;
    status=0;
}
Employee::Employee(char* firstname,char* lastname,Date birth,int Id,Date start,double   salary,int status){
}

Employee::~Employee() {

}
#endif /* EMPLOYEE_H_ */

how to initialize all the const variables in the constructor??

General Grievance
  • 4,555
  • 31
  • 31
  • 45
user3258125
  • 65
  • 1
  • 1

1 Answers1

5

The method of initialization you used for id is part of the constructor's initialization list. It's a list, because it can be used to initialize multiple values, comma seperated. You can, and should, even use it to initialize non-const members, if possible.

Employee::Employee()
    :birth(args),
     id( 0 ),
     start(args),
     salary(0.0),
     status(0)
{}

Notice that I've ordered the members in the same order they appear in the class body. This is not a requirement, but is good practice, because that's the order they will be initialized in anyway, regardless of what order you list them in. This becomes especially important if the initialization of one member relies on the value of another.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274