BASIC OF OOPS
AND FEATURE
ANKIT CHAUHAN(21SCSE1040134)
NEERAJ MADDHESHIYA(21SCSE1040133)
SHEEL BUSHAN MISHRA(21SCSE1040135)
INDEX
1. What is OOPs………………….
2.Class
3.Objects
4.Encapsulation
5.Abstraction
6.Polymorphism
7.Inheritance
WHAT IS OOPS
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.
CLASS
• A class in C++ is the building block that leads to Object-Oriented programming. It 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. A C++ class is
like a blueprint for an object.
• Example:
Consider the Class of Cars. There may be many cars with different names and brand
but all of them will share some common properties like all of them will have 4
wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed
limits, mileage are their properties.
CODE
class Ankit {
public:
string myname=“Ankit”;
// Member Functions()
void printname()
{
cout << “programmer is: " << myname;
}
}; output: programmer is Ankit
OBJECTS
Declaring Objects: When a class is defined, only the specification for
the object is defined; no memory or storage is allocated. To use the
data and access functions defined in the class, you need to create
objects.
.
Syntax:
ClassName ObjectName;
Example:
Ankit An;
ENCAPSULATION
defined as wrapping up of data and information under a single unit. In Object
Oriented Programming, Encapsulation is defined as binding together the data and
the functions that manipulates them.
• We can not access any function from class directly. We need an object to
access that function which is using the member the variable of that class.
• The function which we are making inside the class ,it must use the all member
variable then only it is called encapsulation.
EXAMPLE
Consider a real life example of encapsulation, in a company there are different
sections like the accounts section, finance section, sales section etc. The finance
section handles all the financial transactions and keep records of all the data related to
finance. Similarly the sales section handles all the sales related activities and keep
records of all the sales. Now there may arise a situation when for some reason an
official from finance section needs all the data about sales in a particular month. In
this case, he is not allowed to directly access the data of sales section. He will first
have to contact some other officer in the sales section and then request him to give the
particular data. This is what encapsulation is. Here the data of sales section and the
employees that can manipulate them are wrapped under a single name “sales section”.
ABSTRACTION
Data abstraction is one of the most essential and important feature of object oriented programming in C++.
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. 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 outside world and which is not.
• Abstraction in Header files: One more type of abstraction in C++ can be header files. For example, consider
the pow() method present in math.h header file. Whenever we need to calculate power of a number, we simply
call the function pow() present in the math.h header file and pass the numbers as arguments without knowing
the underlying algorithm according to which the function is actually calculating power of numbers.
EXAMPLE
Consider a real life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of car or applying brakes will
stop the car but he does not know about how on pressing accelerator the speed is
actually increasing, he does not know about the inner mechanism of the car or
the implementation of accelerator, brakes etc in the car. This is what abstraction
is.
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 real-life example of polymorphism is a person
who at the same time can have different characteristics. Like a man at
the same time is a father, a husband and an employee. So the same
person exhibits different behavior in different situations. This is called
polymorphism. Polymorphism is considered as one of the important
features of Object-Oriented Programming.
TYPE OF POLYMORPHISM
polymorphism is mainly divided into two types:
1. Compile-time Polymorphism
2. Runtime Polymorphism
Compile-time Polymorphism
Function Overloading: When there are multiple functions with the same name but different parameters, then the
functions are said to be overloaded. Functions can be overloaded by changing the number of
arguments or/and changing the type of arguments.
Operator Overloading: C++ also provides the option to overload operators. For example, we can make use of the
addition operator (+) for string class to concatenate two strings. We know that the task of this operator is to add two
operands. So a single operator ‘+’, when placed between integer operands, adds them and when placed between string
operands,
Runtime Polymorphism
A virtual function is a member function which is declared within a base class and is re-
defined (overridden) by a derived class. When you refer to a derived class object using
a pointer or a reference to the base class, you can call a virtual function for that object
and execute the derived class’s version of the function.
Limitations of Virtual Functions:
• Slower: The function call takes slightly longer due to the virtual mechanism and
makes it more difficult for the compiler to optimize because it does not know exactly
which function is going to be called at compile time.
• Difficult to Debug: In a complex system, virtual functions can make it a little more
difficult to figure out where a function is being called from.
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.
Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class
created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent
class”. The derived class now is said to be inherited from the base class.
When we say derived class inherits the base class, it means, the derived class inherits all the properties of the base
class, without changing the properties of base class and may add new features to its own. These new features in the
derived class will not affect the base class. The derived class is the specialized class for the base class.
• Sub Class: The class that inherits properties from another class is called Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.
Role of Access Specifier
1. Public Mode: If we derive a subclass from a public base class. Then the public member of the base class will
become public in the derived class and protected members of the base class will become protected in the
derived class.
2. Protected Mode: If we derive a subclass from a Protected base class. Then both public members and protected
members of the base class will become protected in the derived class.
3. Private Mode: If we derive a subclass from a Private base class. Then both public members and protected
members of the base class will become Private in the derived class.
TYPES OF INHERITANCE
1.Single inheritance
2.Multilevel inheritance
3.Multiple inheritance
4.Hierarchical inheritance
5.Hybrid inheritance
SINGLE INHERITANCE:
In single inheritance, a class is allowed
to inherit from only one class. i.e. one
subclass is inherited by one base class
only.
Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};
MULTIPLE INHERITANCE:
Multiple Inheritance is a feature of C++ where
a class can inherit from more than one class.
i.e one subclass is inherited from more than
one base class.
Syntax:
class subclass_name : access_mode base_class1, access_mode
base_class2
{
// body of subclass
};
MULTILEVEL INHERITANCE:
In this type of inheritance, a derived class is
created from another derived class.
Syntax:
class C
{ ... .. ... };
class B:public C
{ ... .. ... };
class A: public B
{ ... ... ... };
HIERARCHICAL INHERITANCE:
In this type of inheritance, more than one subclass is
inherited from a single base class. i.e. more than one derived
class is created from a single base class.
Syntax:
class A
{ // body of the class A. }
class B : public A
{ // body of class B. }
class C : public A
{ // body of class C. }
class D : public A
{ // body of class D. }
HYBRID (VIRTUAL) INHERITANCE:
Hybrid Inheritance is implemented by combining more than one type
of inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance.
Below image shows the combination of hierarchical and multiple
inheritances: Syntax:
class A
{ // body of the class A. }
class B : public A
{ // body of class B. }
class C : public A
{ // body of class C. }
class D : public A
{ // body of class D. }
THANKYOU
CYBER ALFA