OO Encapsulation  «Prev 

Class or struct

It will be our C++ style in this course to prefer class to struct unless all members are data members with public access.
It will also be our style to use access keywords explicitly and to place public members first and private members last. We call this the "need to know" style, because everyone needs to know the public interface, but only the class provider needs to know the private implementation details.
A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
Classes are generally declared using the keyword class, with the following format:

class class_name {
  access_specifier_1:
    member1;
  access_specifier_2:
    member2;
  ...
  } object_names;

Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations or access specifiers. The difference is we can now include also functions and members, but also this new element called access specifier.
An access specifier is one of the following three keywords:
  1. private,
  2. public,
  3. or protected.

Base Classes and Derived Classes

Often, an object of one class is an object of another class, as well. For example, in geometry, a rectangle is a quadrilateral (as are squares, parallelograms and trapezoids). Thus, in C++, class Rectangle can be said to inherit from class Quadrilateral. In this context, class Quadrilateral is a base class, and class Rectangle is a derived class. A rectangle is a specific type of quadrilateral, but it is incorrect to claim that a quadrilateral is a rectangle, the quadrilateral could be a parallelogram or some other shape. Figure 2-11 lists several simple examples of base classes and derived classes.

Inheritance examples
Figure 2-11: Inheritance examples

Derived-Class object

Because every derived-class object is an object of its base class, and one base class can have many derived classes, the set of objects represented by a base class typically is larger than the set of objects represented by any of its derived classes. For example, the base class Vehicle represents all vehicles, including cars, trucks, boats, airplanes, bicycles and so on. By contrast, derived class Car represents a smaller, more specific subset of all vehicles. Inheritance relationships form treelike hierarchical structures. A base class exists in a hierarchical relationship with its derived classes. Although classes can exist independently, once they are employed in inheritance relationships, they become affiliated with other classes. A class becomes either a base class, supplying members to other classes, a derived class, inheriting its members from other classes, or both. Let us develop a simple inheritance hierarchy with five levels (represented by the UML class diagram in Fig. 2-12). A university community has thousands of members.

Inheritance hierarchy for university CommunityMembers
Figure 2-12: Inheritance hierarchy for university CommunityMembers

These members consist of employees, students and alumni. Employees are either faculty members or staff members. Faculty members are either administrators (such as deans and department chairpersons) or teachers. Some administrators, however, also teach