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 indisputable pillars of object orientation.
These features stand out more than any other as far as object orientation goes. They are
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. And any programming language which 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, C#.
The basic feature will be the support for classes. Like we can define and use a class in C++.
And we can implement the inheritance technique like
class newClass: myClass
So newClass will inherit all the functionality of the myClass.
Encapsulation and Data Hiding means that we hide the functionality and our object data from the unauthorized access, i.e we can hide the var1 and var2 from being used by newClass by making the private. In this way we can use var1 and var2 only in myClass.
Introduction to Object Oriented Programming and C++
Since the invention of the computers, many programming approaches have been tried. 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 programs that are reliable and maintainable. These techniques became popular among programmers over the last two decades. With the advent of languages such as C, Structured programming became very popular and was the main technique of the 1980s. Structured programming was a powerful tool that enabled programmers to make moderately complex programs fairly easily. However, as the programs grew larger, even the structured approach failed to show the desired results in terms of bug-free, easy-to-maintain, and reusable programs.
Object Oriented Programming (OOP) is an approach to program organization and development that attempts to eliminate some of the pitfalls of conventional programming methods by in incorporating the best of structured programming features with several powerful new concepts. It is a new way of organizing and developing programs and has nothing to do with any particular language. However, not all languages are suitable to implement the OPP concepts easily.
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.
While we concentrate on the development of functions, 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:
- Data encapsulation
- Data abstraction
- 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.
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.
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.
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.
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
Table 2.1 Visibility of Base Class – Members in Derived Class
|Derivation Specification||Base Class Specification||Derived Class Access|
|protected||full access, private in derived class|
|public||full access, public in derived class|
|protected||full access, protected in derived class|
|public||full access, public in derived class|