C++ Programming
[Link]
OOPs Concept
⚫ Object-oriented programming – As the name suggests uses objects in
programming.
⚫ Object-oriented programming aims to implement real-world entities like
inheritance, hiding, polymorphism, etc. in programming.
⚫ The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function.
❖ Building blocks of OOPs :
• Class
• Objects
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
❖ Advantage of OOPs over Procedure-oriented
programming language
1. OOPs makes development and maintenance easier where as in
Procedure-oriented programming language it is not easy to manage if
code grows as project size grows.
2. OOPs provide data hiding whereas in Procedure-oriented programming
language a global data can be accessed from anywhere.
3. OOPs provide ability to simulate real-world event much more effectively.
We can provide the solution of real word problem if we are using the
Object-Oriented Programming language.
©Topperworld
C++ Programming
❖ Class
• A Class is a user-defined data type that has data members and member
functions.
• Data members are the data variables and member functions are the
functions used to manipulate these variables together these data
members and member functions define the properties and behavior of
the objects in a Class.
• In the above example of class Car, the data member will be speed limit,
mileage, etc and member functions can apply brakes, increase speed,
etc.
❖ Object
⚫ An Object is an identifiable entity with some characteristics and
behavior. An Object is an instance of a Class.
⚫ When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
Example:
// C++ Program to show the syntax/working of Objects as a
// part of Object Oriented PProgramming
#include <iostream>
using namespace std;
class person {
char name[20];
int id;
public:
void getdetails() {}
};
©Topperworld
C++ Programming
int main()
{
person p1; // p1 is a object
return 0;
}
❖ Encapsulation
• In normal terms, Encapsulation is defined as wrapping up data and
information under a single unit.
• In Object-Oriented Programming, Encapsulation is defined as binding
together the data and the functions that manipulate them.
❖ Abstraction
• Data abstraction is one of the most essential and important features of
object-oriented programming in C++.
©Topperworld
C++ Programming
• Abstraction means displaying only essential information and hiding the
details.
• Data abstraction refers to providing only essential information about
the data to the outside world, hiding the background details or
implementation.
Abstraction using Classes: We can implement Abstraction in C++
using classes. The class helps us to group data members and member
functions using available access specifiers. A Class can decide which
data member will be visible to the outside world and which is not.
Abstraction in Header files: One more type of abstraction in C++
can be header files.
❖ Polymorphism
• The word polymorphism means having many forms. In simple words,
we can define polymorphism as the ability of a message to be displayed
in more than one form.
• A person at the same time can have different characteristics. A man at
the same time is a father, a husband, and an employee. So the same
person possesses different behavior in different situations. This is
called polymorphism.
©Topperworld
C++ Programming
❖ Inheritance
The capability of a class to derive properties and characteristics from another
class is called Inheritance. Inheritance is one of the most important features of
Object-Oriented Programming.
➢ Sub Class: The class that inherits properties from another class is called
Sub class or Derived Class.
➢ Super Class: The class whose properties are inherited by a sub-class is
called Base Class or Superclass.
➢ Reusability: Inheritance supports the concept of “reusability”,
when we want to create a new class and there is already a class that includes
some of the code that we want, we can derive our new class from the existing
class. By doing this, we are reusing the fields and methods of the existing class.
❖ Dynamic Binding
• In dynamic binding, the code to be executed in response to the
function call is decided at runtime.
©Topperworld
C++ Programming
• C++ has virtual functions to support this. Because dynamic binding is
flexible, it avoids the drawbacks of static binding, which connected the
function call and definition at build time.
❖ Message Passing
⚫ Objects communicate with one another by sending and receiving
information.
⚫ A message for an object is a request for the execution of a procedure
and therefore will invoke a function in the receiving object that
generates the desired results.
⚫ Message passing involves specifying the name of the object, the name
of the function, and the information to be sent.
©Topperworld