Topics covered:

Object Oriented Programming (OOP)

OOP Concepts

Object

Class

Abstraction

Encapsulation

Inheritance

Polymorphism

Overloading

Message Passing

Object Oriented Programming (OOP)

OOP is an approach to the solution of problems, in which all computations are performed, in the context of an objects.

The purpose of writing a program is to solve a problem.

Solving a problem consists of multiple activities:

Understand the problem

Design a solution

Refine the solution

Implement the solution

Test the solution

OOP Concepts

C++ fully supports object-oriented programming.

The main pillars of object-oriented development are as follows:

Object

Class

Abstraction

Encapsulation

Inheritance

Polymorphism

Overloading

Message Passing

Object

An Object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program.

Object is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.

Example: Person

Class

An object is defined by a class.

A Class is the blueprint of an object.

The class uses methods to define the behaviors of an object.

A class represents a concept, and an object represents the embodiment of that concept.

Multiple objects can be created from the same class.

Abstraction

In OOP, abstraction is a central principle.

Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency.

Hiding non-essential features and showing essential features.

Data abstraction refers to, providing only essential information to the outside word and hiding their background details.

Real World example:

TV Remote Buttons: We are just seeing the buttons; we don't see the button circuits. The circuits and wiring all are hidden.

Encapsulation

Encapsulation is the hiding of an internal mechanisms and data structures of a software component behind a defined interface.

Encapsulation offers

Protection

Consistency

Allows change

This is a process of binding or wrapping data and the code that operates on the data into a single entity.

This keeps the data safe from outside interference and misuse.

One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code outside the wrapper.

Real World Example:

Cars and owners: all the functions of cars are encapsulated from the owners.

Owner can start the car using method “Key” but cannot modify engine details.

Inheritance

Inheritance is the process by which new classes called derived classes are created from existing classes called base classes.

One of the most useful aspects of object-oriented programming is code reusability.

As the name suggests inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.

Classes can be organized into hierarchies.

Some OOP terms are commonly described as:

Superclass: Parent class

Subclass: Child class

Base class: Parent class

Derived class: Child class

In the figure below, Base Class is Fruit, Apple and Banana are Derived Classes.

The acquisition of the properties of one class (the base or parent class) by another class (the derived or child class) is called as Inheritance.

Real World Example:

We make a class called "Human" that represents our physical characteristics. It's a generic class that could represent you, me or anyone in the world. Its state keeps track of things like number of legs, number of arms, and blood type. It has behaviors like to eat, sleep, and walk.

Human is good for getting an overall sense of what makes us all the same but it can't for instance tell about gender differences. For that we'd need to make two new class types called "Man" and "Woman". The state and behaviors of these two classes will differ from each other in a lot of ways except for the ones that they inherit from Human.

Types of Inheritance

Single Inheritance

One super and one sub class

Multiple Inheritance

Many super and one sub class

Multi-level Inheritance

The sub class of one level forms the super class of another level

Hierarchical Inheritance

One super and many sub classes

Hybrid Inheritance

Multiple and multi-level combined

Polymorphism

The term Polymorphism literally means “one name multiple forms. 

A polymorphic reference is a variable that can refer to different types of objects at different points in time.

The method invoked through a polymorphic reference can change from one invocation to the next.

In short polymorphism is

One interface

Multiple implementations

Inheritance

Method overloading

There are two types of polymorphism:

RUNTIME polymorphism which includes virtual functions etc. 

COMPILE TIME polymorphism which includes function and operator overloading.

Real World Example:

Person is polymorphic in that they have different roles in different situations:

At Home: Husband/son

In the Office: Employer

In Public: Good Citizen

Overloading

Overloading allows functions in computer languages such as C++ to have the same name but with different parameters.

C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

Operator Overloading:

Operator overloading allows you to pass different variable types to the same function and produce different results.

Function Overloading:

Function overloading is the practice of declaring the same function with different signatures.

The same function name will be used with different number of parameters and parameters of different types.

Message Passing

Message passing is a method by which an object sends data to another object or requests other object to invoke method.

This is also known as interfacing.

It acts like a messenger from one object to other object to convey specific instructions.


Translate

CR Zaman. Powered by Blogger.

Recent Posts

Popular Posts