Professional Documents
Culture Documents
(OOPS) Concept
By
T.Srilatha,MSc,Mtech,(PhD)
Faculty of Computer Science
RBVRR Women’s College
Narayanaguda , Hyderabad
INDEX
Evolution of Software
Programming Paradigms
Differences b/w C and C++
Structure of C++ Program
Simple C++ Program
OOPs
– Objects
– Classes
– Data Abstraction
– Encapsulation
– Inheritance
– Polymorphism
EVOLUTION OF
SOFTWARE
Why we have to learn C++?
How is it going to benefit us?
C++
• C++ is a general purpose, compiler based and
object-oriented programming language.
OS [Ex: DOS,WIN,UNIX….]
Example of implementing
Abstraction
Advantages of Data Abstraction:
MEMBER
FUNCTIONS
DATA
DATA
MEMBER
FUNCTIONS MEMBER
FUNCTIONS
ENCAPSULATION
output: 5
• Encapsulation:
• In this program the
variable x is made private.
• This variable can be
accessed and manipulated
only using the functions
get() and set() which are
present inside the class.
• Thus we can say that here,
the variable x and the
functions get() and set() are
binded together which is
nothing but encapsulation.
Inheritance
• The capability of a class to derive properties
and characteristics from another class is
called Inheritance.
• 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 sub class is called Base Class or
Super class.
Why and when to use inheritance?
• Consider a group of vehicles. You need to create
classes for Bus, Car and Truck.
• The methods fuelAmount(), capacity(), applyBrakes() will
be same for all of the three classes.
• If we create these classes avoiding inheritance then we
have to write all of these functions in each of the three
classes as shown in below figure:
Anomalies
• You can clearly see that above process results in
duplication of same code 3 times.
• This increases the chances of error and data
redundancy.
• To avoid this type of situation, inheritance is used.
• If we create a class Vehicle and write these three
functions in it and
• Inherit the rest of the classes from the vehicle
class, then we can simply avoid the duplication of
data and increase re-usability.
Inheritance
Types of Inheritance in C++
1. Single Inheritance:
In single inheritance, a
class is allowed to
inherit from only one
class. i.e. one sub class
is inherited by one
base class only.
Example for Single
Inheritance
Output:
This is a Vehicle
2. Multiple Inheritance: Multiple Inheritance is a
feature of C++ where a class can inherit from more
than one class.
• (i.e) one sub class is inherited from more than
one base classes.
Multiple Inheritance
Output:
This is a Vehicle
This is a 4 wheeler
Vehicle
3.Multilevel Inheritance: In this type of inheritance,
a derived class is created from another derived class.
Multilevel Inheritance
output:
This is a Vehicle
Objects with 4 wheels are
vehicles
Car has 4 Wheels
4.Hierarchical Inheritance:
• In this type of inheritance, more than one sub class
is inherited from a single base class.
• (i.e) More than one derived class is created from a
single base class.
Hierarchical
Inheritance
Output:
This is a Vehicle
This is a Vehicle
5.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 inheritance:
Hybrid Inheritance
Output:
This is a Vehicle
Fare of Vehicle
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.
• Real life example of polymorphism, a person at a
same time can have different characteristic.
POLYMORPHISM
POLYMORPHISM
• Function Overloading: When there are
multiple functions with same name but different
parameters then these functions are said to
be overloaded.
• Functions can be overloaded by
– change in number of arguments or/and
– change in type of arguments.
Output:
Integer number: 5
Float number: 5.5
Integer number: 5 and float number: 5.5
• Operator Overloading: option to overload
operators.
• For example, we can make the operator (‘+’)
for string class to concatenate two strings.
• We know that this is the addition operator
whose task is to add two operands.
• So a single operator ‘+’ when placed between
integer operands , adds them and when placed
between string operands, concatenates them.
Output:
Count: 6
• Runtime polymorphism: This type of polymorphism
is achieved by Function Overriding or Virtual
Functions.
• If we want to build a large application that should
be scalable to add more features and to modify the
code.
• It becomes tedious with static binding or early
binding.
• It is not known which function will be invoked till
an object makes the function call during
program’s execution. This process is called late
binding or dynamic binding.
Virtual Base Classes
• Consider the situation Base
shown.
• Two copies of Base are D1 D2
included in D3.
• This causes ambiguity
when a member of Base
D3
is directly used by D3.
51
Virtual Base Classes (contd.)
class Base { class D3 : public D1, public D2
public: int i; {
}; // contains two copies of ‘i’
class D1 : public Base };
{ void main()
{
public:
D3 obj;
int j;
obj.i = 10; // ambiguous, compiler error
}; obj.j = 20; // no problem
class D2 : public Base obj.k = 30; // no problem
{ obj.D1::i = 100; // no problem
public: obj.D2::i = 200; // no problem
int k; }
}; 52
Virtual Base Classes (contd.)
class Base class D3 : public D1, public D2 {
{ // contains only one copy of ‘i’
public: int i; };
}; // no change in this class
class D1 : virtual public Base //definition
{ void main() {
public: int j; D3 obj;
}; obj.i = 10; // no problem
// activity of D1 not affected obj.j = 20; // no problem
class D2 : virtual public Base obj.k = 30; // no problem
{ obj.D1::i = 100;
public: int k; // no problem, overwrites ‘10’
}; obj.D2::i = 200;
// activity of D2 not affected // no problem, overwrites ‘100’
} 53
54
Introduction to Virtual Functions (contd.)
class base { void main()
public: {
virtual void show() base b1;
{ b1.show(); // base - (s.b.)
cout << “base\n”; derived d1;
} d1.show(); // derived – (s.b.)
}; base *pb = &b1;
class derived : public base pb->show(); // base - (d.b.)
{ derived *pb = &d1;
public: pb->show(); // derived (d.b.)
void show() { }
cout << “derived\n”; Here, s.b. = static binding 55
} }; d.b. = dynamic binding
56
Final Comments
• Run-time polymorphism is not automatically activated
in C++.
• We have to use virtual functions and base class pointers
to enforce and activate run-time polymorphism in C++.
• Early binding • Late binding
–Normal functions, –Virtual functions accessed via
overloaded functions a base class pointer
–Resolved at compile time –Resolved at run-time
–Very efficient –Quite flexible during run-time
–But lacks flexibility –But has run-time overhead;
slows down program
execution
58
Online Quiz on C++
• Quiz
• https://www.geeksforgeeks.org/c-plus-
plus-gq/operator-overloading-gq/
• https://www.sanfoundry.com/object-
oriented-programming-questions-answers-
online-test/
THANK
YOU