OOPortal OOPortal

C++ Class Construct  «Prev 

Objects and member functions

The declaration
ch_stack s, t, u;

creates three separate ch_stack objects of sizeof(ch_stack) bytes. Each of these objects has its own data members:

char  s[max_len];
int   top;

A member function is conceptually part of the type.
There is no distinct member function for any of these three ch_stack objects.
NOTE: To follow the const-correctness principle, it is always a good idea to declare member functions that do not change any data member of the object as being const. These member functions are also called inspectors, compared to mutators for non-const member functions.


This section discusses the implementations for the Employee member functions. The Employee constructor sets the initial values for the Employee's data members. By default, new employees have no name, an employee number of -1, the default starting salary, and a status of not hired. This constructor implementation shows a second mechanism to initialize class member variables. You can either put the initialization between the curly braces in the body of the constructor, or you can use a constructor initializer, which follows a colon after the constructor name.

#include <iostream>
#include "Employee.h"
using namespace std;
namespace Records {
: mFirstName("")
, mLastName("")
, mEmployeeNumber(-1)
, mSalary(kDefaultStartingSalary)
, mHired(false)
The promote() and demote() methods simply call the setSalary() method with a new value. Note that the default values for the integer parameters do not appear in the source file; they are only allowed in a function declaration, not in a definition.
void Employee::promote(int raiseAmount){
 setSalary(getSalary() + raiseAmount);
void Employee::demote(int demeritAmount){
 setSalary(getSalary() - demeritAmount);
The hire() and fire() methods just set the mHired data member appropriately.
void Employee::hire(){
mHired = true;
void Employee::fire(){
mHired = false;

The display() method uses the console output stream to display information about the current employee. Because this code is part of the Employee class, it could access data members, such as mSalary, directly instead of using getSalary(). However, it is considered good style to make use of getters and setters when they exist, even within the class.
void Employee::display() const
cout << "Employee: " <<  getLastName() <<  ", " <<  getFirstName() <<  endl;
cout <<  "-------------------------" <<  endl;
cout <<  (mHired ? "Current Employee" : "Former Employee") <<  endl;
cout <<  "Employee Number: " <<  getEmployeeNumber() <<  endl;
cout <<  "Salary: $" <<  getSalary() <<  endl;

cout <<  endl;