Introduction to Object Oriented Programming in C++
OOP refers to implementation of a program using OBJECTS. In object-oriented programming, a system can be viewed as objects that interact together to accomplish certain tasks.
Object oriented programming concepts
- Class
- Object
- Data abstraction
- Data encapsulation
- Inheritance
- Polymorphism
CLASSES: Structures is the key feature for understanding CLASS, because classes are syntactically an extension of C structures.
If fruit is defined as a class, then the statement
fruit mango; will create an object belonging to the class fruit.
OBJECT: The two concepts in OOP are: Class & Object.
- Objects are members of a class
- Defining a class doesn’t create any objects.
- Object may be a person, place, bank account, or a table of data etc.
- Objects are variables of type class
- When a program is executed, the objects interact by sending messages to one another
Suppose in Banking system, customer is one object (say ob1) and account is another object (say ob2). bb1 may send message to ob2 requesting for bank balance. Remember customer and account are software objects. Each object contains data and code to manipulate the data. Objects can interact without having to know details of each other’s data/code.
- OOP refers to the idea of combining data and functions.
- Member function = object’s function
- Member function provides the only way to access its data
- If you want to read a data, you call a member function of that object.
- Data and functions are encapsulated into a single entity.
- Only member functions access the data of an object, so writing, modifying, debugging and maintaining the program is easy.
Data Abstraction
Act of representing essential features without including background details or explanations. Classes use concept of abstraction. Real-life examples of abstraction are:
- Switch board
- Index of a book
- Car engine
You don’t need to internal wiring details of switch board in order to operate it. Classes provide this type of feature. You don’t need to know details of object in order to operate it or use it.
Data Encapsulation
The wrapping up of data and functions into a single unit is known as Encapsulation Data is not accessible to the outside world and only those functions which are wrapped (enclosed) in the class can access it. Functions provide interface between objects data and the program. This insulation of data from direct access by the program is called data hiding/ information hiding.
Programming paradigms
- Procedure Oriented Programming (POP)
- Object Oriented Programming (OOP)
Procedure Oriented Programming (POP)
- Primary focus on functions
- Large programs are divided into smaller programs known as functions
- A number of functions are written to accomplish these things
- Emphasis is on doing things
- Employs top-down approach in program design
- Examples: COBOL, FORTRAN, C
Object Oriented Programming (OOP)
- Primary focus on data
- Programs are divided into OBJECTS
- A number of objects are written to accomplish these things
- Emphasis is on data hiding
- Employs bottom-up approach in program design
- Examples: C++, Java, C#
OOPs:
- Removes some of the flaws encountered in object-oriented approach
- OOP treats data as a critical element in the program development and does not allow it to flow freely around the system
- It ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions
- Decomposition of problem into number of entities called objects and then builds data and functions of other objects
- Data of an object can be accessed only by the functions associated with that object
- Object = Data + Functions
- Object communicate each other through functions
- Whenever necessary, new data and functions can be easily added
Inheritance
Process of creating new classes from existing classes is known as INHERITANCE. This is done by deriving new class from existing class. The new class will inherit the capability of existing one, but it is free to add new features of its own. Imagine a child inherits features like: beauty of a mother and intelligence of a father.
Inheritance allows the program to reuse a class and does not introduce any undesirable side effect into rest of the classes.
Reusability
The concept of inheritance provides the idea of reusability. Once a class has been written, created and debugged, it can be distributed to other programmers for use in their own programs. This is reusability. A programmer can take existing class and without modifying it, add additional features and capabilities to it.
We can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes.
The bird sparrow is part of the class flying bird which is again a part of the class bird.
Polymorphism
Polymorphism refers to one name, many forms. Suppose the word square has two meanings. You can find square of a number. Also you can calculate area of a square. So, an operation may exhibit different behaviours in different instances. OOP supports this polymorphism feature. Another example is * operator. It has two purposes: one is multiplication and other as a pointer operator.
Benefits of OOP
- Better quality software development
- Eliminate redundant code through polymorphism
- Saving of software development time and productivity i.e., we can build programs from standard working modules
- OO systems can easily upgrade from small to large systems
- Software complexity can be easily managed
- Less maintenance cost