OO Encapsulation  «Prev  Next»
Lesson 11 Class or struct?
ObjectiveDesign a Person Class

C++ - Class versus struct

The Person class contains members to store data and a member function to print out the data members. Classes in C++ are introduced by the keyword class. They are a form of struct whose default privacy specification is private.
Thus struct and class can be used interchangeably with the appropriate access specifications.
More the on the C++ Class can be found at the following page Class versus Struct.
Let us look at how we would turn our struct ch_stack into a class. First, here is the struct:

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.
Figure 2-11: Inheritance examples
Base class Derived classes
Student GraduateStudent, UndergraduateStudent
Shape Circle, Triangle, Rectangle, Sphere, Cube
Loan CarLoan, HomeImprovementLoan, MortgageLoan
Employee Faculty, Staff
Account CheckingAccount, SavingsAccount

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

 
const int max_len = 40;

struct ch_stack {
  //data representation
  char s[max_len]; 
  int top;
  enum{ EMPTY = -1, FULL = max_len - 1 };

  //operations represented as member functions
  void reset() { top = EMPTY; }
  void push(char c) {
    assert(top != FULL);
    top++;
    s[top] = c;
  }
  char pop() {
    assert(top!= EMPTY);
    return s[top--];
  }
  char top_of() {
    assert(top!= EMPTY);
    return s[top];
  }
  bool empty()
  { return (top == EMPTY); }
  bool full()
   { return (top == FULL); }

const int max_len = 40;
 class ch_stack {
 //data representation
 char s[max_len]; 
 int top;
 enum{ EMPTY = -1, FULL = max_len - 1};

 //operations represented as member functions
 void reset() { top = EMPTY; }
 void push(char c) {
  assert(top != FULL);
  top++;
  s[top] = c;
 }
 char pop() {
  assert(top!= EMPTY);
  return s[top--];
 }
 char top_of() {
   assert(top!= EMPTY);
   return s[top];
 }
 bool empty(){
  return (top == EMPTY); 
  }
 bool full() {
  return (top == FULL); 
 }

The only difference is the keyword class, which is used in place of struct.

Person Class Exercise

Click the Exercise link below to design a class called Person that contains members to store data and a member function to print out the data members.
Person Class - Exercise