Providing Access to Class Members

Since data members in a class are usually private, and therefore, inaccessible, we need a way for clients to access them. We do so by providing public member functions.
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
};
Here is an example of how you could manipulate the class members:
Employee clerk;       // clerk is an Employee object

clerk.setName("Jane", "Doe");  // firstName is "Jane"
                               // lastName is "Doe"
clerk.setSalary(50699.0f);     // salary is 50,699.00
clerk.setYears(0);             // years is 0
clerk.display();      // outputs data members to screen
At this point, we haven't seen the details of how this is accomplished. We have only seen the interface to the class. The implementation provides the details.

Previous page
Next page

Back to Index
Back to Outline