INTRODUCTION
  1. Overview of the Object-Oriented Design Methodology.
  2. Objects and Classes.
  3. Classes Membership
  4. Predefined ("Built-in") Objects and Classes.
  5. Programmer defined objects and classes.
CONTENTS



THE OBJECT-ORIENTED DESIGN METHODOLOGY

EXAMPLE OBJECT ORIENTED LANGUAGES

SIMULA. The first language to introduce the concept of objects. Essentially ALGOL'60 with objects.

SMALLTALK'80. The archetypal object-oriented language/environment. Everything in Smalltalk is an object. Originally developed by Xerox in early 1970s. Not easy to use and inefficient.

C++. Essentially C with object-oriented features. Best be described as a hybrid between a conventional imperative language and an object-oriented programming language.

Eiffel. Designed in 1980s as a simpler and more efficient implementation of the object-oriented paradigm than that espoused by Smalltalk.

Java. Java was developed in 1993 under the name of Oak, and achieved popularity in 1996 when sun Microsystems launched their Java compiler free on the web. The language is similar to C++ in appearance with the additional (unique) feature that it can easilly be used within the context of the WWW.




OBJECTS AND CLASSES

The key feature of Object Oriented Programming (OOP) is, as the name suggests, the object. An object can be thought of as some real world entity which a programmer wishes to "model".

Classes describe categories of object. They define the data (attributes or features) that can be associated with an object, and the functions (operations or methods) that can be applied to them. Consequently we say that a particular object is an instance of a class.

In C++ a class is considered to be a compound data type (this is not the case in all OOP languages, e.g. Smalltalk'80). Thus, in C++, a class definition can be viewed as a template which enables the programmer to declare instances of a particular class (type).




CLASS MEMBERSHIP

Components of a class can be either data members or function members (member functions) Further, in C++, three categories of membership are identified:

  1. PUBLIC. Members which can be accessed from any where in a program.
  2. PRIVATE. Members for internal use (i.e. within the object) which can only be accessed from outside the object through the use of public member functions.
  3. PROTECTED. Private members which are inherited by classes derived from the current class.

Thus a C++ class can comprise, at most, three sections corresponding to the above three access categories. Note that, since one of the principals of OOP is data hiding, data is normally declared in the private section of a class.




PREPROGRAMMED BUILT-IN OBJECTS & CLASSES

Most OOP languages provide a set of preprogrammed ("built-in") classes and objects to support common operations such as Input and Output (I/O). For this purpose C++ provides the built in classes ostream and istream, and a number of objects which are instances of the classes ostream and istream. The most commonly used ostream and istream objects are:

  1. cout, an output object, which is an instance of the class ostream, and is designed to output data to the screen.
  2. cin, an input object, which is an instance of the class istream, and is designed to input data to a program.

BUILT IN CLASSES FUNCTION (MEMBERS)

Naturally, built in classes also have built in member functions associated with them which may be used by the programmer directly. To associate a member function with an object we use a member operator (`.') in a similar manner to that used to access "members" of C structures. For example the istream class has a built in function getline. This can be be associated with the cin input object as follows:

cin.getline(name,N);

The getline function is used to read a specified number of characters (specified by N) into an array specified by the argument name.

C++ example program using cout and cin

#include < iostream.h >

int main(void)
{
const int Size = 20;
char name[Size];

cin.getline(name,Size);

cout << name << "\n";
}

Input name string (max length 20) and .

With respect to the above code the following points should be noted:

  1. The << operator is referred to as the input operator and is used to pass messages to cout objects.
  2. The >> operator is referred to as the output operator and is used to return responses from cin objects.
  3. Both cout and cin are smart objects capable of automatically converting information from one form to another according to program context (i.e. we do not have to define the input or output type as we do in C).
  4. Not only is cout an ostream object but so is (say) cout << "Yum!"; this feature allows us to write sequences of << operators.



PROGRAMMER DEFINED OBJECTS AND CLASSES

Only a limited amount of programming can be achieved using the built in classes and their members. For most real applications programmers must define their own classes and members.

A C++ example of a user defined class definition

// circle.h

class Circle {
        public:
                float Radius;
                float Area;
                float Circumference;
        };

Here we have created a class Circle which has three data members: Radius, Area and Circumference. Further points to note concerning the above code:


CREATING INSTANCES OF PROGRAMMER DEFINED CLASSES

In the following example programme an instance, x, of the above C++ class is created. Once created the data members of this instance are then assigned values (through user input or by calculation).

#include "circle.h"
#include <math.h>
#include <iostream.h>

main()
{
Circle disc;
const float pi = 22.0/7.0;

cin >> disc.Radius = 7.0;
disc.Area = pi*disc.Radius*disc.Radius;
disc.Circumference = pi*disc.Radius*2;

cout << "Radius:   "  << disc.Radius << "\n" ;
cout << "Area:     "  << disc.Area << "\n" ;
cout << "Circumference: " << disc.Circumference << "\n" ;
}

Input radius as a float and .

Points to note:

In line with the principal of data hiding, in the above example, we would prefer the data to be hidden (i.e. private):

// circle.h

class Circle {
        private:
                float Radius;
                float Area;
                float Circumference;
        };

However, to support this, we now require a public function to create instances of the class. C++ provides a special class member function, called a constructor, to do this. A constructor function has the same name as its class name. For each programmer defined class C++ automatically creates a default constructor. We can thus write a short program to create an instance of the above class, using a constructor function, as follows:

#include "circle.h"

void main(void)
{
Circle disc = Circle();
}



Return to oop home page or continue.




Created and maintained by Frans Coenen. Last updated 03 July 2001