Back to CEO's Home Page

Classes and Data Abstraction

Charles E. Oyibo

Classes

In object oriented design (OOD), the first step is to identify the components called objects. An object combines data and the operations on that data in a single unit. In C++, the mechanism that allows us to combine data and the operations on that data in a single unit is called a class.

A class is a collection of a fixed number of components. The components of a class are called the members of the class.

The general syntax for defining a class is:

class classIdentifier
{
    classMemberList
};

where classMemberList consists of variable declations and/or functions. That is, a member of a class can be either a variable (to store data) or a function.

In C++, class is a reserved word, and it defines only the data type; no memory is allocated. It announces the declaration of a class. Note too, that the semicolon at the end of the closing brace is a part of the syntax.

The members of a class are classified into three categories: private, public, and protected. We focus mainly on the first two in this chapter.

In C++, private, public, and protected are reserved words, and are called member access specifiers.

The following are some useful facts about private, and public members of a class:

...Discussion about class clockType...

Deciding which member to make public and which to make private depends on the nature of the member. The general rule is that any member that needs to be access directly by the user should be declared public; any member that should not be accessed directly by the user should be declared private. The following statement defines the class clockType:

class clockType
{
public:
    void setTime(int, int, int);
    void getTime(int&, int&, int&) const;
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType& otherClock) const;

private:
    int hr;
    int min;
    int sec;
};

In this definition:

In the definition of class clockType, all data members are private and all function members are public. However, a function member can also be private, and a data member can also be public.

Unified Modeling Language Diagrams

A class and its members can be described graphically using a notation known as the Unified Modeling Language (UML) nototion.

clockType
-hr: int
-min: int
-sec: int
+setTime(int, int, int): void
+getTime(int&, int&, int&) const: void
+printTime() const: void
+incrementSeconds(): void
+incrementMinutes(): void
+incrementHours(): void
+equalTime(const clockType& otherClock) const: bool

 

Variable (Object) Declaration

Once a class is defined, we can declare variables of that type. In C++ terminology, a class variable is called a class object or class instance. The following declares two objects of the type clockType.

clockType myclock;
clockType yourClock;

Each object has 10 members, and each object has a separate memory allocated for hr, min, and sec.

In actuality, memory is allocated only for data members of each class object (object = variable). The C++ compiler generates only one physical copy of a function member of a class, and each class instance (instance = variable) executes the same copy of the member function.

Accessing Class Members

Once an object of a class is defined, it can access the members of the class. The general syntax for an object to access a member of a class is:

classObjectName.memberName

The class members that a class object can access depends on where the object is declared.

Recall that in C++, the dot (.) is called the member access operator.

Examples:

myClock.setTime(5, 2, 30);
yourClock.printTime();

Built-In Operations on Classes

Most of C++'s built-in operations do not apply to classes. We cannot use arithmetic operators to perform arithmetic operations on class objects (unless they are overleaded). Similarly, we cannot use relational operators to compare two class objects for equality (unless they are overloaded).

The two built-in operations that are valid for class objects are member access (.) and assignment (=). We've already seen how to access individual members of a class by using the name of the class object, then a dot, then the member name. We will now discuss how an assignment statement works.

Assignment Operator and Classes

Suppose that myClock and yourClock are variables of the type clockType as defined previously. The statement:

yourClock = myClock;

copies the values of myClock into yourClock. That is,

  1. The value of myClock.hr is copied into yourClock.hr,
  2. The value of myClock.min is copied into yourClock.min,
  3. The value of myClock.sec is copied into yourClock.sec.

We see that an assignment statement performs a member-wise copy.

Class Scope

A class member can be either automatic (that is, created each time the control reaches its declaration, and destroyed when the control exits the surrounding block) or static (that is, created once, when the control reaches its declaration, and destroyed when the program terminates).

Also, we can declare an array of class objects. A class object has the same scope as other variables. A member of a class has the same scope as a member of a struct. That is, a member of a class is local to the class. You access a class member outside the class by using the class object name and the member access operator (.).

Functions and Classes

The following rules describes the relationship between functions and classes:

... continued discussion about Functions and Classes

...

Constructors

Because C++ does not automatically initialize its member variable, and because the private members of a class cannot be accessed outside the class, if the user (i.e. of the class, the programmer) forgets to initialize the class variables, the program will produce erroneous results (recall that uninitialized variables hold garbage).

To guarantee that the data members of a class are initialized, we use constructors. There are two types of constructors: with or without parameters. The constructor without paramters is called the default constructor.

Constructors have the following properties:

Let's extend the definition of class clockType by including two constructors:

class clockType
{
public:
    void setTime(int, int, int);
    void getTime(int&, int&, int&) const;
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType& otherClock) const;
    clockType(int, int, int); //constructor with parameters
    clockType(); // default constructor

private:
    int hr;
    int min;
    int sec;
};

We can now write the definition of these constructors:

clockType::clockType(int hours, int minutes, int seconds)
{
    hr = hours;
    min = minutes;
    sec = seconds;
}

clockType::clockType()
{
    hr = 0;
    min = 0;
    sec = 0;
}

Top of page

Contact Information

Page Last Updated: Saturday February 12, 2005 10:21 AM