Before getting into the discussion of what are the object oriented programming techniques, let’s first look at what makes a programming language or a programming methodology as object oriented.
What makes a programming language object oriented?
Well, there are several fundamental pillars of object-oriented programming. These features stand out more than any other as far as object orientation goes. They are
- Encapsulation
- Data Hiding
- Overloading
- Polymorphism
- Grand-daddy of them all – Inheritance
Object Oriented Programming Techniques
So what are the techniques which we can use to make our program object oriented?
The answer is simple, any program which uses the above five methodologies or any of it, we can call that program object oriented. Any programming language that supports all of these features and facilitate the programmer in developing the program in these methodologies is called Object Oriented Programming Language i.e. C++, Java and C#.
The basic feature of the programming languages is to support for classes. For example, we can define and use a class in C++ as below:
class myClass{ public: int var1; int var2; };
And we can implement the inheritance technique in C++ as:
class newClass: myClass { public: int var1; int var2; };
Here newClass will inherit all the functionality of the myClass.
Encapsulation and Data Hiding means that we hide the functionality and object data from the unauthorized access i.e. we can hide the var1 and var2 from being used by newClass by making them private. In this way we can use var1 and var2 only in myClass.
Introduction to Object Oriented Programming and C++
Developers have used many programming methodologies since the introduction of computer systems. These included techniques such as modular programming, top-down programming, bottom-up programming, and structured programming. The primary motivation in each case has been the concern to handle the increasing complexity of reliable and maintainable programs. These techniques became popular among programmers over the last two decades. With the programming languages such as C, Structured programming became very popular and was the primary technique of the 1980s. Structured programming was a powerful tool that enabled programmers to make complex programs reasonably fast. However, as the programs grew more complex, even the structured approach failed to show the desired results, such as maintaining and reusing the code.
Object-Oriented Programming (OOP) is an approach to program organization and development that attempts to reduce some of the issues with conventional programming techniques. It is a new way of organizing and developing programs and has nothing to do with any particular programming language. However, not all languages are suitable to implement the OPP concepts or implement partial features of OOP.
Object- oriented programming was developed because limitations were discovered in earlier approach to programming To appreciate what OOP does, first all discuss what these limitations one and how they arose from traditional programming languages.
Procedure Oriented Programming
Conventional programming using high level languages such as COBOL, FORTRON, and C is commonly known as procedure-oriented programming. In the procedure-oriented approach, the problem is viewed as a sequence of things to be done, such as reading, calculating and printing. A number of functions are written to accomplish these tasks. The primary focus is on functions. A typical program structure for procedural programming is shown below.
The technique of hierarchical decomposition has been used to specify the tasks to be completed in order to solve a problem. Procedure-oriented programming basically consists of writing a list of instructions for the computer to follow, and organizing these instructions into groups known as functions. We normally use a flowchart to organize these actions and represent the flow of control from one action to another.
Very little attention is given to the data that are being used by various functions. What happens to the data? How are they affected by the functions that work on them?
Basic concepts of Object Oriented Programming
General concepts used extensively in object oriented programming are:
- Objects
- Classes
- Data encapsulation
- Data abstraction
- Inheritance
- Polymorphism
- Dynamic binding
- Message Passing
Benefits of Object Oriented Programming
OOP offers several benefits to both the program designer and the user. Object orientation contributes to the solution of many problems associated with the development and quality of software products. The new technology promises greater programmer productivity, better quality of software and lesser maintenance cost. The principal advantages are:
Through inheritance, we can eliminate redundant code and extend the use of existing classes. We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. It is possible to have multiple instances of an object to co-exist without any interference. It is easy to partition the work in project based on object. Object-oriented systems can be easily upgraded from small to large systems. Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler. Software complexity can be easily managed.
Introduction to C++
C++ is an object-oriented programming language built on the base of the C language. The C++ language is a super-set of the C language. You could write C++ programs like C programs (a few
of the programs in this tutorial are like that), or you could take advantage of the object oriented features of C++ to write object-oriented programs. What makes a programming language or programming methodology object oriented? Well, there are several indisputable pillars of object orientation. These features stand out more than any other as far as object orientation goes. They are encapsulation, data hiding, overloading, polymorphism, and the grand-daddy of them all: inheritance. Each of the pillars of object orientation will be discussed here briefly.
Data Encapsulation
C++ provides the facility to encapsulate data and the operations that manipulate that data, in an appropriate object. This enables the use of these collections of data and function, called objects , in programs other than the program for which they were originally created. With objects, just as with the traditional concept of subroutines, you make functional blocks of code. You still have language-supported abstractions such as scope and separate compilation available. This is a rudimentary form of encapsulation. Objects carry encapsulation a step further. With objects, you
define not only the way a function operates, or its implementation, but also the way an object can be accessed, or its interface.
Data Hiding
Related to the idea of encapsulation is the concept of data hiding. Encapsulation hides the data from other classes and functions in other classes. This makes programs more reliable, since publishing a specific interface to an object prevents inadvertent access to data in ways that were not designed or accounted for. In C++, the access to an object, and its encapsulated data and functions is treated very carefully, by the use of keywords private, protected, and public. One has the opportunity to make access specifications for data objects and functions as being private, or protected, or public while defining a class. Only when the declaration is made as public do other functions and objects have access to the object and its components without question. On the other hand, if the declaration happens to be as private, there is no possibility of such access. When the declaration given is as protected, then the access to data and functions in a class by others is
not as free as when it is public, nor as restricted as when it is private.
Inheritance
The primary distinction for C++ from C is that C++ has classes. Objects are defined in classes. Classes themselves can be data items in other classes, in which case one class would be an element of another class. Of course, then one class is a member, which brings with it its own data and functions, in the second class. This type of relationship is referred to as a “has-a” relationship: Object A has an Object B inside it.
A relationship between classes can be established not only by making one class a member of another but also by the process of deriving one class from another. One class can be derived from another class, which becomes its base class. Then a hierarchy of classes is established, and a sort of parent->child relationship between classes is established. The derived class inherits, from the base class, some of the data members and functions. This type of relationship is referred to as an “is-a” relationship. You could have class Rectangle be derived from class Shape, since Rectangle is a Shape. Naturally, if a class A is derived from a class B, and if B itself is derived from a class C, then A inherits from both B and C. A class can be derived from more than one class. This is how multiple inheritance occurs. Inheritance is a powerful mechanism for creating base functionality that is passed onto next generations for further enhancement or modification.
Derived Classes
When one class has some members declared in it as protected, then such members would be hidden from other classes, but not from the derived classes. In other words, deriving one class from another is a way of accessing the protected members of the parent class by the derived class. We then say that the derived class is inheriting from the parent class those members in the parent class that are declared as protected or public.
In declaring a derived class from another class, access or visibility specification can be made, meaning that such derivation can be public or the default case, private. Table shows the consequences of such specification when deriving one class from another.
Visibility of Base Class Members in Derived Class
Visibility of base class members in derived class is shown in the table below.
Derivation Specification | Base Class Specification | Derived Class Access |
---|---|---|
private(default) | private | none |
protected | full access, private in derived class | |
public | full access, public in derived class | |
public | private | none |
protected | full access, protected in derived class | |
public | full access, public in derived class |
Differences between C++ and Java
Both C++ and Java support object oriented features however there are certain features which Java does not support. For example, multiple inheritance is not supported by Java. The following code will show an error when compiling in Java compiler.
class MyClass1 { void function1() { System.out.println("MyClass1"); } } class MyClass2 { void function2() { System.out.println("MyClass2"); } } class MyClass extends MyClass1, MyClass2 { public static void main(String args[]) { System.out.println("MyClass"); } }
Multiple inheritance causes a diamond problem, which is one of the reasons Java does not support Multiple inheritance. However, C++ allows us to solve this problem by using virtual inheritance. To overcome memory issues with virtual inheritance, C++ uses delegates i.e., a delegate method from a class in another class, by using a common abstract base class.
Another important Object Oriented feature is Operator Overloading, which is not supported by Java. Java only support method overloading which is supported by C++ as well.