Implementing Constructors

Constructors are implemented just like other member functions in the implementation file:
Employee::Employee(char first[], char last[])
{
  setName(first, last); // set the names (validate)
  salary = 0.0f;        // set salary to default 0.0f
  years = 0;            // set years to default 0
}
Now, when we instantiate an object, all of its data members are set to our default values:
#include "Employee.h"     // Employee class

void main(void)
{
  Employee clerk1;     // error, can't do this anymore
                       // because we've defined our own
                       // constructor

  Employee clerk2("Jane", "Doe");  // ok

  clerk2.display();
}
Output:

  Name: Doe, Jane 
Salary: $0.00
 Years: 0
Previous page
Next page

Back to Index
Back to Outline