Object Oriented
Programming with C++
Nudar Mawla
Lecturer
Computer Science and Engineering
Dhaka City College
Chapter - 1
Principles of Object Oriented Programming
Computer Programming
Computer Programming is the process of creating a set of instructions that tell a computer
how to perform a task.
Process of writing, testing, debugging and maintaining the source code of Computer
Programs.
It can be classified into two major categories –
Low level languages
High level languages
Low Level Languages
Machine Language
Assembly Language
Unstructured
High Level Languages
Programming
Procedural
Programming
Structured
Programming
Object oriented
Programming
Object Oriented Programming (OOP)
Object-oriented programming is about creating objects that contain both data and functions.
refers to a type of computer programming (software design) in which programmers define the
data type of a data structure, and also the types of operations (functions) that can be applied to
the data structure.
provides a clear structure for the programs
helps to keep the C++ code reducing the repetition of code. You can extract out the codes that
are common for the application, and place them at a single place and reuse them instead of
repeating it.
makes it possible to create full reusable applications with less code and shorter development
time.
Basic Concepts of OOP
Objects
Class
Data Abstraction and Encapsulation *
Inheritance *
Polymorphism *
Dynamic Binding
Message Passing
Objects
It is a particular instance of a class.
Collection of data members and associated member functions.
Classes
Everything in C++ is associated with classes and objects, along with its attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as weight and colour,
and methods, such as drive and brake.
Attributes and methods are basically variables and functions that belongs to the class. These
are often referred to as "class members".
Classes and objects are the two main aspects of object-oriented programming.
When the individual objects are created, they inherit all the variables and functions from the
class.
Look at the following illustration to see the difference between class and objects:
Class Object
Fruits Mango
Apple
Banana
Data Abstraction
It refers to the act of representing essential features without including the background
details and explanations.
Encapsulation
The wrapping up of data and functions into a single unit (called Class) is known as
Encapsulation.
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must declare class variables/attributes as private (cannot be accessed from
outside the class).
Increased security of data.
Inheritance
Process by which objects of one class acquires the properties of objects of another class.
In C++, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:
1. derived class (child) - the class that inherits from another class.
2. base class (parent) - the class being inherited from.
To inherit from a class, use the : symbol.
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related
to each other by inheritance.
Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those
methods to perform different tasks.
This allows us to perform a single action in different ways
Dynamic Binding
Dynamic binding is also known as late binding or run-time binding.
Dynamic binding is an object oriented programming concept and it is related with
polymorphism and inheritance.
Dynamic binding(dispatch) means that a block of code executed with reference to a
procedure(method) call is determined at run time.
provides facility to specify that the compiler should match function calls with the correct
definition at the run time.
Dynamic binding is achieved using virtual functions.
Message Passing
The process of programming in which communication is involved is known as message
passing.
It allows execution of different codes using the same object at runtime.
Object – Oriented Languages
Depending upon the features the programming languages support, they can be classified into the
following categories –
Object based programming languages
Object oriented programming languages
Object based programming languages
It is the style of programming that primarily supports encapsulation and object identify. Major
features –
Data encapsulation
Data hiding and access mechanism
Automatic initialization and clear up of objects
Operator overloading
Example – ADA, Visual Basic, Asp.net
Object oriented programming languages
Object oriented programming languages = Object based features + Inheritance + Dynamic
Binding
Example – C++, Java, Python
Application of OOP
Real time system
Object oriented database
AI and expert systems
Neural network and parallel programming
CIM/ CAM/ CAD systems
Abstract Data type
When a class is used as a type, it is an abstract type that refers to a hidden representation.
an ADT is typically implemented as a class, and each instance of the ADT is usually an object
of that class
Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set
of value and a set of operation. It is called “abstract” because it gives an implementation-
independent view.
Top – Down Approach vs Bottom – up Approach
Structure/procedure oriented programming languages like C programming language follows
top down approach. Whereas object oriented programming languages like C++ and Java
programming language follows bottom up approach.
Top down approach begins with high level design and ends with low level design or
development. Whereas, bottom up approach begins with low level design or development and
ends with high level design.
In top down approach, main() function is written first and all sub functions are called from
main function. Then, sub functions are written based on the requirement. Whereas, in bottom
up approach, code is developed for modules and then these modules are integrated with main()
function.
Now-a-days, both approaches are combined together and followed in modern software design.