The Interface to a Class

Interfaces (class declarations) are placed in header files. We have used several header files from the standard C++ library so far. Our Employee interface would be placed in a file called Employee.h (.hpp):
// Employee.h is the interface to Employee class

class Employee           // Employee is a new type
{
  public:                // these functions are visible
    void setName(char first[], char last[]);
    void setSalary(float newSalary);
    void setYears(int numYears);
    void display(void) const;

  private:               // members are not visible
    char firstName[10];  // first name is a char array
    char lastName[12];   // last name is char array
    float salary;        // yearly pay is float
    int years;           // years of service
};
To use the new type, we would include the header file in our C++ file:
#include "Employee.h"   // Employee class

void main(void)
{
  Employee clerk;       // clerk is an Employee object

  clerk.setName("Jane", "Doe");   // set names
  clerk.setSalary(50000.0f);      // set salary
  clerk.setYears(10);             // set years
  clerk.display();                // display data
}
Previous page
Next page

Back to Index
Back to Outline