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.
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).
Components of a class can be either data members or function members (member functions) Further, in C++, three categories of membership are identified:
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.
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:
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.
#include < iostream.h > int main(void) { const int Size = 20; char name[Size]; cin.getline(name,Size); cout << name << "\n"; }
With respect to the above code the following points should be noted:
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.
// 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:
#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" ; }
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