previous | start | next

## Constructors with Parameters

- It's possible to create a class with more than one constructor.

  >     Employee Fred; /* default construction */
  >     Employee Harry("Harry Hacker", 40000); /* alternate construction */

- All constructors have the same name as the class, but have different parameter lists.

  >     class Employee
  >     {
  >     public:
  >        Employee()
  >        Employee(string employee_name, double initial_salary);

  >        void set_salary(double new_salary);

  >        string get_name() const;
  >        double get_salary() const;
  >     private:
  >        string name;
  >        double salary;
  >     };

- Whenever two functions have the same name but are distinguished by their parameter types, the function name is overloaded.
- The second constructor sets the data fields of the new object based on the parameters.

  >     Employee::Employee(string employee_name, double initial_salary)
  >     {
  >        name = employee_name;
  >        salary = initial_salary;
  >     }

previous | start | next