|
Characteristics of an Object-oriented
Language
The important elements of an
object-oriented language are:
Class
An Object in C++ is an instance of a
class. For example, in the following
statement:
int iNum1, iNum2, iNum3;
iNum1, iNum2, iNum3 are three
variables of type int.
Similarly, you can define many objects
of the same class as shown in Figure
10.2
|
 |
| |
|
Fig 10.2 Class
and Objects of the Class |
Inheritance
Inheritance is the process of creating
a new class, called the derived class,
from the existing class, called the
base class.
Figure 10.3 shows the inheritence of a
derived class from the base class.
 |
| |
|
Figure 10.3 Inheritance of a
Derived Class From a Base Class |
For example, motorcycles, cars and
trucks have certain common
properties-- all have wheels, engines
and breaks. Therefore, they can be
grouped under a class called
automobiles. Apart from sharing these
common features, each subclass has its
own particular characteristices--cars
use petrol while trucks use diesel.
The derived class has its own
characteristics and, in addition,
inherits the
properties of the base class.
Reusability
The concept of inheritence provides an
important feature to the
object-oriented lanuage-reusability. A
programmer can take an existing class
and, without modifyinh it, and
additional features and capabilities
to it. This is done by deriving a new
class from an existing class.
Polymorphism
The word polymorphism is derived from
two Latin words poly (many) and
morphos
(forms). The concept of using
operators or functions in different
ways, depending on what they are
operating on, is called polymorphism.
The following program uses an
overloaded function to illustrate
polymorphism:
//This
program uses an overloaded function to
display a character or an integer
#include<iostream.h>
void disp(int);
void disp(char);
void main()
{
disp(10);
disp(‘a’);
}
void disp(int iNum1)
{
cout<<”The number is”<<iNum1<<endl;
}
void disp(char cCh)
{
cout<<”The character is”<<oCh<<endl;
}
The output of Program 10.1 is:
The number is 10
The character is a
In Program 10.1, the function disp()
is overloaded and, based on the
parameter passes, the corresponding
message is displayed, therefore
exhibiting polymorphism.
|