In object oriented programming we can create objects such as a ball that has properties such as position, size, color and behaviors such as fall, roll and bounce. The ability to create objects means allows us to create programs that more closely resemble the real world. Such programs are easier to design and maintain, particularly in large software projects. In C++ we can create our own data types such as ball using the keyword class. A class contains data members such as position, size and color and function members such as fall, roll and bounce. The data and the methods are bound together as a single unit. The class forms a blueprint from which objects can be created. An object is an instance of a class. Many objects can be created from the same class in the same way that many cookies can be made from the same cookie cutter. The three fundamental characteristics of object oriented programming are
Encapsulation refers to the process of binding the data and the methods that work with the data together into a class. The details of how the methods are implemented are hidden from the user of the class. Typically, the user of a class must use the member functions to change the values of the data members in the class. This helps prevent accidentally changing the values of the data which can be a difficult problem to detect and fix in a large software project.
In the class declaration we choose the data members and functions to be included in the class. A class declaration has four main parts.
In this section we study some common member functions.
Table of operators that can be overloaded |
|
( ) | function call |
[ ] | array element |
-> | structure member pointer reference |
++ | increment |
-- | decrement |
* | indirection |
+,-,*,/ | arithmetic |
<, <=, >, >= | comparison |
&&, || | logical |
=,+=,-=,*=,’= | assignment |
Example: Employee class.
# include<iostream> |
Inheritance allows us to create new classes from existing classes. The original class is called the base class. The new class is called the derived class. The derived class inherits the data and function members from the base class and adds additional data and function members. A programmer can often find a class that is close to meeting her needs and then add some additional data or functions. The programmer does not need to be concerned with the implementation details of the base class and is free to focus only on the derived class. Using previously developed classes is an excellent form of software reuse (no need to reinvent the wheel). A base class such as mammal can be used to derive many classes such as cat, dog, horse and bear. The properties and behaviors common to all mammals should be placed in the base class and the remaining more specific properties and behaviors are distributed amongst the derived classes.
# include<iostream> |
Polymorphism refers to the ability to associate many meanings with one function name using dynamic binding. For example in the program below the base class Employee has a print( ) method and the derived classes Boss and HourlyWorker both have print( ) methods. The decision as to which of the print( ) methods is to be executed is made at run time.
# include<iostream> |