OOP Concepts in C++ (08-01-2021)
OOP Concepts in C++ (08-01-2021)
C C++
Development
C was developed by Dennis C++ was developed by Bjarne
Ritchie between the year 1969 Stroustrup in 1979.
and 1973 at AT&T Bell Labs.
Programming Platform
For the development of code, C++ is known as hybrid
C supports procedural language because C++ supports
programming. both procedural and object
oriented programming
paradigms.
Keywords
C contains 32 keywords. C++ contains 52 keywords.
Programming Features
language.
oriented programming.
Information Hiding
C does not support information
Data is hidden by the
hiding.
Encapsulation to ensure that
data structures and operators
are used as intended.
Overloading
Function and operator Function and operator
overloading is not supported in overloading is supported by C+
C. +.
Reference variable
Reference variables are not Reference variables are
supported by C. supported by C++.
Virtual Function
Virtual functions, Pure Virtual Virtual functions, Pure Virtual
functions are not supported by functions are supported by C+
C. +.
Friend Function
friend functions and Friend friend functions and Friend
Class are not supported by C. Classes are supported by C++.
Inheritance
C does not support
C++ supports inheritance.
inheritance.
Data Handling
C++ focuses on data instead of
Instead of focusing on data, C
focusing on method or
focuses on method or process.
procedure.
Memory Allocation
C provides malloc() and calloc() C++ provides new operator for
functions for dynamic memory memory allocation and delete
allocation, and free() for operator for memory de-
memory de-allocation. allocation.
Exception Handling
Exception handling is
Direct support for exception
supported by C++.
handling is not supported by C.
Input/Output
scanf() and printf() functions cin and cout are used for
are used for input/output in C. input/output in C++.
Programming Approach
object oriented programming
Structure/procedure oriented
languages like C++
programming languages like C
programming language follows
programming language follows
bottom up approach.
top down approach.
Class:
1. The building block of C++ that leads to Object-Oriented
programming is a Class.
2. A Class is a user-defined data type, which holds its own data
members and member functions, which can be accessed and used
by creating an instance of that class.
3. Together these data members and member functions define the
properties and behaviour of the objects in a Class.
4. A class is like a blueprint for an object.
Object:
1. An Object is an identifiable entity with some characteristics and
behaviour.
2. An Object is an instance of a Class.
3. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
4. Each object contains data and code to manipulate the data.
5. The central theme of a class: data members are hidden in the
private section, and can only be changed by the public member
functions.
Encapsulation:
1. Encapsulation is placing the data and the functions (that work on
that data) in the same place.
2. Object-oriented programming provides us a framework to place
the data and the relevant functions together in the same object.
3. By doing this, data is not easily accessible to the outside world.
4. In OOP we achieve encapsulation by making data members as
private and having public functions to access these data members.
5. As in encapsulation, the data in a class is hidden from other
classes, so it is also known as data-hiding.
Abstraction:
1. Abstraction is the process of hiding irrelevant information from
the user.
2. For Example, when we are driving the car, first we start the
engine by inserting a key. We are not aware of the process that
goes on in the background for starting the engine.
3. By using abstraction in our application, the end user is not
affected even if we change the internal implementation.
4. For example, a database system hides certain details of how data
is stored and created and maintained.
5. Data Abstraction may also be defined as the process of
identifying only the required characteristics of an object ignoring
the irrelevant details.
Polymorphism:
1. The ability to use an operator or function in different ways in
other words giving different meaning or functions to the
operators or functions is called polymorphism.
2. Poly refers to many.
3. Eg:-A person at the same time can have different characteristic.
Like a man at the same time is a father, a husband, an employee.
4. So the same person posses different behaviour in different
situations. This is called polymorphism.
5. Types of Polymorphism
a. Compile Time(Operator Overloading, Function Overloading)
b. Run Time(Virtual Function)
Inheritance:
1. Inheritance is the process of forming a new class from an
existing class, here existing class is called as base class, new
class is formed called as derived class.
2.Inheritance provides reusability of code.
3.The derived class is the specialized class for the base class.
1.Sub Class:=The class that inherits properties from another
class is called Sub class or Derived Class.
2. Super Class: =The class whose properties are inherited by
sub class is called Base Class or Super class.
Message Passing:=
1. Objects communicate with one another by sending and receiving
information to each other.
2. A message for an object is a request for execution of a
procedure and therefore will invoke a function in the receiving
object that generates the desired results.
3. Message passing involves specifying the name of the object, the
name of the function and the information to be sent.
#1) Reusability
1. OOP allows the existing code to be reused through inheritance.
#2) Modularity
1.As we modularize the program in OOP, it’s easy to modify the
program if a problem occurs or new feature or enhancement is to
be added.
#3) Flexibility
1. OOP helps us with flexible programming using the polymorphism
feature.