SlideShare a Scribd company logo
Overview of OOP
1
2
Structured Vs Object Oriented
Programming
1.Function Oriented
2.Procedure Abstraction
3.Does not support
External Interface
4.Free flow of Data
5.Also called FOP
1.Object Oriented
2.Procedure & Data abstraction
3.Supports External Interface
4.Secured Data & not freely
flows
5.Also called OOP
3
Elements of Object oriented programming
• Modeling the real world problem as close as
possible to the users perspective.
• Interacting easily with computational environment.
• Constructing reusable software components
and easily extendable libraries.
• Easily modifying and components extending
implementations of without having to record
everything from scratch.
4
Elements of Object oriented programming
Definition of OOP:
“Object oriented programming is a programming
methodology that associates data structures with a set of
operators which act upon it.”
5
Elements of Object oriented programming
• Objects
• Classes
• Encapsulation
• Data Abstraction
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
6
Objects
• OOP uses objects as its fundamental building blocks.
• Objects are the basic run-time entities in
an object-oriented system.
• Every object is associated with data and functions which
define meaningful operations on that object.
• Object is a real world existing entity.
• Object is an Instance of a particular class.
7
Object
Attributes
Operation
Operation
Operation
Operation
8
Example: StudentObject
st_name
st_id
branch
semester
Enroll()
Displayinfo()
Performa
nce()
Result()
9
Class
• Class is a collection of similar objects.
Class
 To define a member function outside the class definition we have to use the
scope resolution operator:: along with the class name and function name.
 Programming Example for scope resolution operator:
 class Geeks
 {
 public:
 string geekname; int id;
 // printname is not defined inside class definition
 void printname();
 // printid is defined inside class definition void printid()
 {
 cout <<"Geek id is: "<<id;
 }
 };
10
Scope Resolution Operator:
 void Geeks::printname()
 {
 cout <<"Geekname is: "<<geekname;
 }
 int main() { Geeks obj1;
 obj1.geekname = "xyz"; obj1.id=15; obj1.printname();
 cout << endl; obj1.printid();

return 0;
 }
 Geekname is: xyz Geek id is: 15
11
 Constructor in C++ is a special method that is
invoked automatically at the time of object
creation. Constructor is a special type of member
function that is used to initialize the data members
for an object of a class automatically, when an
object of the same class is created. Constructor is
invoked at the time of object creation. It constructs
the values i.e. provides data for the object which is
why it is known as constructors.
12
Constructor:
• The name of the constructor is the same as its
class name.
• Constructors are mostly declared in the public
section of the class though they can be declared
in the private section of the class.
• Constructors do not return values; hence they do
not have a return type.
• A constructor gets called automatically when we
create the object of the class.
13
Characteristics of Constructors in C++
class student
{
int rno;
char name[50]; double fee; public: student(int a)
{
A=5;
cout<<"Enter the RollNo:"; cin>>rno;
cout<<"Enter the Name:"; cin>>name;
cout<<"Enter the Fee:"; cin>>fee; }
void display()
{
cout<<endl<<rno<<"t"<<name<<"t"<<fee;
}
};
int main()
{
student s; //constructor gets called automatically when we create the object of the
class s.display();
return 0;
}
14
Constructors can be classified based on in which
situations they are being used. There are 4 types of
constructors in C++:
Default Constructor: No parameters. They are
used to create an object with default values.
Parameterized Constructor: Takes parameters.
Used to create an object with specific initial values.
Copy Constructor: Takes a reference to another
object of the same class. Used to create a copy of
an object.
15
Types of Constructors
Like a constructor, Destructor is also a member function
of a class that has the same name as the class name
preceded by a tilde(~) operator. It helps to deallocate the
memory of an object. It is called while the object of the
class is freed or deleted. In a class, there is always a
single destructor without any parameters so it can’t be
overloaded. It is always called in the reverse order of the
constructor. if a class is inherited by another class and
both the classes have a destructor then the destructor of
the child class is called first, followed by the destructor of
the parent or base class.
16
Destructor
#include <iostream>
using namespace std;
class Z
{
public:
// constructor
Z()
{
cout<<"Constructor called"<<endl;
}
// destructor
~Z()
{
cout<<"Destructor called"<<endl;
}
}; 17
int main()
{
Z z1; // Constructor Called
int a = 1;
if(a==1)
{
Z z2; // Constructor Called
} // Destructor Called for z2
} // Destructor called for z1
18
19
Constructors, Destructors &
Inheritance
• Constructor functions are executed in
their order of derivation.
• Destructor functions are executed in
reverse order of derivation.
20
Message Passing
“The process of invoking an operation on an object. In
response to a message the corresponding method is
executed in the object”.
21
Message Passing
FacultyObject
StudentObject
MgmtObject Performance
Result
Performance
22
Inheritance
• “Inheritance is the mechanism to provides the
power of reusability and extensibility.”
• “Inheritance is the process by
which one object can acquire the properties of
another object.”
23
Inheritance
Point
Line
24
Inheritance
Fruit
Orange Mango Banana
25
Inheritance
Polygon
Triangle Rectangle
26
Inheritance
Polygon
Triangle RightAngle
Triangle
27
Base Class
• “Base class is a class which defines those
qualities common to all objects to be derived
from the base.”
• The base class represents the most general
description.
• A class that is inherited is referred
to as a base class.
28
Derived Class
• “The classes derived from the base
class are usually referred to as derived
classes.”
• “A derived class includes all features of the
generic base class and then adds qualities
specific to the derived class.”
• The class that does the inheriting is called the
29
Inheritance
Note:
Derived class can be used as a base class for
another derived class.
• In C++, inheritance is achieved by allowing one
class to incorporate another class into its
declaration.
30
Inheritance
• Syntax:
class derived_class: Acesss_specifier base_class
{ };
• Example:
class CRectangle: public Cpolygon{
class CTriangle: public Cpolygon{
};
};
31
Inheritance & Access Specifier
Access public protected private
Members of the
same class
Yes Yes Yes
Members of derived
classes
Yes Yes No
Non-members Yes No No
32
Public base class Inheritance
• All public members of the base class become
public members of the derived class.
• All protected members of the base class
become protected members of the derived
class.
33
Private base class Inheritance
• All public and protected members of the base
class become private members of the derived
class.
• But private members of the base class remain
private to base class only, not accessible to
the derived class.
34
Protected Members of Base Class
• Member is notaccessible by othernon
member elements of the program.
• The base class' protected members become
protected members of the derived class and
are, therefore, accessible by the derived class.
35
Protected Base-Class Inheritance
• All public and protected members of the base
class become protected members of the
derived class.
• All public members of the base class become
unavailable to main() function.
• All private members of the base class become
unavailable to the derived class.
36
Inheritance & Access Specifier
Access public protected private
Members of the
same class
Yes Yes Yes
Members of derived
classes
Yes Yes No
Non-members Yes No No
• Syntax:
class derived: Acess_specifier base1,
Acess_specifier base2
{ };
• Example:
class Orange: Acess_specifier Yellow,
Acess_specifier Red
{ };
37
Inheriting Multiple Base Classes
38
Inheriting Multiple Base Classes
Orange
Red
Yellow
39
Inheritance
Fruit
Mango
Malgoba
Mango
Mallika
Mango
40
Constructors, Destructors &
Inheritance
• When an object of a derived class is created,
if the base class contains a constructor, it will
be called first, followed by the derived class'
constructor.
• When a derived object is destroyed, its
destructor is called first, followed by the base
class' destructor.
41
Passing Parameters to Base-Class
Constructors
• Making use of an expanded form of the
derived class's constructor declaration, we
can pass arguments to one or more base-class
constructors.
• Syntax:
derived-constructor(arg-list) : base1(arg-list),
base2(arg-list), … baseN(arg-list)
{ // body of derived constructor }
• As we are arguments to a base-class constructor
are passed via arguments of the derived class'
constructor.
• Even if a derived class‘ constructor does not use
any arguments, we need to declare a constructor
as if the base class requires it.
• The arguments passed to the derived class are
simply passed along to the base class constructor.
42
Passing Parameters to Base-Class
Constructors
43
Granting Access
• When a base class is inherited as private:
- all public and protected members of that
class become private members of the derived
class.
• But in some certain circumstances, we want to
restore one or more inherited members to
their original access specification.
44
Granting Access
• To accomplish this :
using
access declaration
45
Granting Access
• using statement:
is designed primarily to support namespaces.
• Access declaration: restores an
inherited member access specification
• Syntax:
• base_class_name::member;
46
Granting Access
• Access declaration is done under the
appropriate access heading in the derived
class’ declaration.
Note:
No type declaration is required.
47
Granting Access
class base {
public:
int j;
};
class derived: private base {
public:
// here is access declaration
base::j;
};
48
Program for Implementation
student
Result_on_Web
private
Program to implement
the given hierarchy
assuming proper properties
for student class. The
Result class computes the
result of every student.
The Result_on_Web class
displays the result upon
getting the grant for USN
Result
protected
49
Hierarchy of Classes
i
i
j & k
j k
2 copies
50
Hierarchy of Classes
Remedy…..?
51
Virtual Base Classes
• Used to prevent multiple copies of the base
class from being present in an object derived
from those objects by declaring the base class
as virtual when it is inherited.
• Syntax:
class derived : virtual public base
{ . . . };
52
Virtual Functions
• “A virtual function is a member function that
is declared within a base class and redefined
by a derived class.”
• Virtual functions implements the "one
interface, multiple methods" philosophy
under polymorphism.
53
Virtual Functions
• The virtual function within base class defines
the form of the interface to that function
• Each redefinition of the virtual function by a
derived class implements its operation as it
relates specifically to the derived class. That
is, the redefinition creates a specific method.
54
Virtual Functions
• To create a virtual function, precede the
function‘s declaration in the base class with
the keyword virtual.
• Example:
class base {
public:
virtual void member_func(){ }
};
55
Virtual Functions
Base
Derived1
Virtual function
override
Derived2
override
56
Virtual Functions
• When accessed "normally" virtual functions
behave just like any other type of class
member function.
• But virtual functions’ importance and capacity
lies in supporting the run-time polymorphism
when they accessed via a pointer.
57
Virtual Functions
• How to implement run-time polymorphism?
- create base-class pointer can be used to point
to an object of any class derived from that base
- initialize derived object(s) to base class object.
• Based upon which derived class objects’
assignment to the base class pointer, c++
determines which version of the virtual function
to be called. And this determination is made at
run time.
58
Virtual Functions
• The redefinition of a virtual function by a
derived class appears similar to function
overloading?
• No
• The prototype for a redefined virtual function
must match exactly the prototype specified in
the base class.
59
Virtual Functions
Restrictions:
• All aspects of its prototype must be the same as base
class virtual function.
• Virtual functions are of non-static members.
• Virtual functions can not be friends.
• Constructor functions cannot be virtual.
• But destructor functions can be virtual.
•NOTE:
Function overriding is used to describe virtual function
redefinition by a derived class.
60
Destructor functions can be virtual?
• Yes.
• In large projects, the destructor of the derived
class was not called at all.
• This is where the virtual mechanism comes
into our rescue. By making the Base class
Destructor virtual, both the destructors will be
called in order.
61
Function overriding
“A function overriding is a process in which a
member function that is declared within a
base class and redefined by a derived class to
implement the "one interface, multiple
methods" philosophy under polymorphism.”
62
Calling a Virtual Function Through a
Base Class Reference
• Sincereference is an implicit pointer,it
can be used to access virtual function.
• When a virtual function is called through a
base-class reference, the version of the
function executed is determined by the object
being referred to at the time of the call.
63
The Virtual Attribute Is Inherited
• When a virtual function is inherited, its virtual
nature is also inherited.
Base
Derived1
Derived2
Virtual function
override
override
64
Virtual Functions Are Hierarchical
• Virtual functions are also hierarchical
in nature.
• This means that when a derived class fails to
override a virtual function then first
redefinition found in reverse order of
derivation is used.
65
Virtual Functions Are Hierarchical
Base
Derived1
Derived2
Virtual function
override
66
Virtual Functions Are Hierarchical
Base
Derived1
Virtual function
override
Derived2
67
Pure Virtual Functions
• “A pure virtual function is a virtual function
that has no definition within the base class.”
• To declare a pure virtual function:
Syntax:
virtual rtype func-name(parameter-list) = 0;
68
Pure Virtual Functions
• When a virtual function is made pure, any
derived class must provide its definition.
• If the derived class fails to override the pure
virtual function, a compile-time error will
result.
NOTE:
When a virtual function is declared as pure, then all
derived classes must override it.
69
Abstract Classes
• “A class that contains at least one pure
virtual
function then it is said to be abstract class.”
• No objects of an abstract class be created.
• Abstract class constitutes an incomplete type
that is used as a foundation for derived.
classes.
70
Using Virtual Functions
• We can achieve the most powerful and
flexible ways to implement the "one interface,
multiple methods“.
• We can create a class hierarchy that moves
from general to specific (base to derived).
71
Using Virtual Functions
• We can define all common features and
interfaces in a base class.
• Specific actions can be implemented only by
the derived class.
• We can add new case easily.
72
Encapsulation
“Mechanism that associates the code and the data it
manipulates into a single unit and keeps them safe from
external interference and misuse.”
73
Encapsulation
Class: student
Attributes: st_name, st_id,
branch, semester
Functions: Enroll()
Displayinfo() Result()
Performance()
74
Data Abstraction
“A data abstraction is a simplified view of an object
that includes only features one is interested in while
hides away the unnecessary details.”
“Data abstraction becomes an abstract data type
(ADT)or a user-defined type.”
75
C++ Implementation
class class_name
{
Attributes;//Properties
Operations;//Behaviours
};
76
C++ Implementation
class student
{
char st_name[30];
char st_id[10];
char branch[10];
char semester[10];
Void Enroll( );
Void Displayinfo( );
Voide Result( );
Void Performance( );
};
class stack
{
int stck[SIZE];
int tos;
void init();
void push(int i);
int pop();
};
77
Polymorphism
• Polymorphism means that the same thing can
exist in two forms.
• “Polymorphism is in short the ability to call
different functions by just using one type of
function call.”
78
Polymorphism
+
Dynamic Binding
“ Dynamic Binding is the process of linking of the code
associated with a procedure call at the run-time”.
79
80
Early vs. Late Binding
• “Early binding refers to events that occur at
compile time.”
• Early binding occurs when all information
needed to call a function is known at compile
time.
• Examples :
function calls ,overloaded function calls, and
overloaded operators.
81
Early vs. Late Binding
• “Late binding refers to function calls that are
not resolved until run time.”
• Late binding can make for somewhat slower
execution times.
• Example:
virtual functions

More Related Content

Similar to asic computer is an electronic device that can receive, store, process, and output data. (20)

PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
PPTX
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
PPT
Oop lec 4(oop design, style, characteristics)
Asfand Hassan
 
PPTX
C++ Presen. tation.pptx
mohitsinha7739289047
 
PPTX
Oop objects_classes
sidra tauseef
 
PPTX
05 Object Oriented Concept Presentation.pptx
ToranSahu18
 
PPTX
Object oriented programming. (1).pptx
baadshahyash
 
PPTX
Inheritance, Polymorphism, and Virtual Functions.pptx
MrNikhilMohanShinde
 
PPSX
Object oriented programming 2
Aadil Ansari
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PDF
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 
PPTX
Class and object
prabhat kumar
 
PPT
UNIT I (1).ppt
VGaneshKarthikeyan
 
PPT
UNIT I (1).ppt
VGaneshKarthikeyan
 
PPTX
Unit ii
donny101
 
PPT
OOP.ppt
Tanmay Dhatrak
 
PPTX
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
PPT
Bca 2nd sem u-2 classes & objects
Rai University
 
PPTX
Inheritance
sourav verma
 
PDF
OOPs theory about its concepts and properties.
ssuser1af273
 
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
Oop lec 4(oop design, style, characteristics)
Asfand Hassan
 
C++ Presen. tation.pptx
mohitsinha7739289047
 
Oop objects_classes
sidra tauseef
 
05 Object Oriented Concept Presentation.pptx
ToranSahu18
 
Object oriented programming. (1).pptx
baadshahyash
 
Inheritance, Polymorphism, and Virtual Functions.pptx
MrNikhilMohanShinde
 
Object oriented programming 2
Aadil Ansari
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 
Class and object
prabhat kumar
 
UNIT I (1).ppt
VGaneshKarthikeyan
 
UNIT I (1).ppt
VGaneshKarthikeyan
 
Unit ii
donny101
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
Bca 2nd sem u-2 classes & objects
Rai University
 
Inheritance
sourav verma
 
OOPs theory about its concepts and properties.
ssuser1af273
 

Recently uploaded (20)

PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
smart lot access control system with eye
rasabzahra
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Design Thinking basics for Engineers.pdf
CMR University
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
smart lot access control system with eye
rasabzahra
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Ad

asic computer is an electronic device that can receive, store, process, and output data.

  • 2. 2 Structured Vs Object Oriented Programming 1.Function Oriented 2.Procedure Abstraction 3.Does not support External Interface 4.Free flow of Data 5.Also called FOP 1.Object Oriented 2.Procedure & Data abstraction 3.Supports External Interface 4.Secured Data & not freely flows 5.Also called OOP
  • 3. 3 Elements of Object oriented programming • Modeling the real world problem as close as possible to the users perspective. • Interacting easily with computational environment. • Constructing reusable software components and easily extendable libraries. • Easily modifying and components extending implementations of without having to record everything from scratch.
  • 4. 4 Elements of Object oriented programming Definition of OOP: “Object oriented programming is a programming methodology that associates data structures with a set of operators which act upon it.”
  • 5. 5 Elements of Object oriented programming • Objects • Classes • Encapsulation • Data Abstraction • Inheritance • Polymorphism • Dynamic Binding • Message Passing
  • 6. 6 Objects • OOP uses objects as its fundamental building blocks. • Objects are the basic run-time entities in an object-oriented system. • Every object is associated with data and functions which define meaningful operations on that object. • Object is a real world existing entity. • Object is an Instance of a particular class.
  • 9. 9 Class • Class is a collection of similar objects. Class
  • 10.  To define a member function outside the class definition we have to use the scope resolution operator:: along with the class name and function name.  Programming Example for scope resolution operator:  class Geeks  {  public:  string geekname; int id;  // printname is not defined inside class definition  void printname();  // printid is defined inside class definition void printid()  {  cout <<"Geek id is: "<<id;  }  }; 10 Scope Resolution Operator:
  • 11.  void Geeks::printname()  {  cout <<"Geekname is: "<<geekname;  }  int main() { Geeks obj1;  obj1.geekname = "xyz"; obj1.id=15; obj1.printname();  cout << endl; obj1.printid();  return 0;  }  Geekname is: xyz Geek id is: 15 11
  • 12.  Constructor in C++ is a special method that is invoked automatically at the time of object creation. Constructor is a special type of member function that is used to initialize the data members for an object of a class automatically, when an object of the same class is created. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object which is why it is known as constructors. 12 Constructor:
  • 13. • The name of the constructor is the same as its class name. • Constructors are mostly declared in the public section of the class though they can be declared in the private section of the class. • Constructors do not return values; hence they do not have a return type. • A constructor gets called automatically when we create the object of the class. 13 Characteristics of Constructors in C++
  • 14. class student { int rno; char name[50]; double fee; public: student(int a) { A=5; cout<<"Enter the RollNo:"; cin>>rno; cout<<"Enter the Name:"; cin>>name; cout<<"Enter the Fee:"; cin>>fee; } void display() { cout<<endl<<rno<<"t"<<name<<"t"<<fee; } }; int main() { student s; //constructor gets called automatically when we create the object of the class s.display(); return 0; } 14
  • 15. Constructors can be classified based on in which situations they are being used. There are 4 types of constructors in C++: Default Constructor: No parameters. They are used to create an object with default values. Parameterized Constructor: Takes parameters. Used to create an object with specific initial values. Copy Constructor: Takes a reference to another object of the same class. Used to create a copy of an object. 15 Types of Constructors
  • 16. Like a constructor, Destructor is also a member function of a class that has the same name as the class name preceded by a tilde(~) operator. It helps to deallocate the memory of an object. It is called while the object of the class is freed or deleted. In a class, there is always a single destructor without any parameters so it can’t be overloaded. It is always called in the reverse order of the constructor. if a class is inherited by another class and both the classes have a destructor then the destructor of the child class is called first, followed by the destructor of the parent or base class. 16 Destructor
  • 17. #include <iostream> using namespace std; class Z { public: // constructor Z() { cout<<"Constructor called"<<endl; } // destructor ~Z() { cout<<"Destructor called"<<endl; } }; 17
  • 18. int main() { Z z1; // Constructor Called int a = 1; if(a==1) { Z z2; // Constructor Called } // Destructor Called for z2 } // Destructor called for z1 18
  • 19. 19 Constructors, Destructors & Inheritance • Constructor functions are executed in their order of derivation. • Destructor functions are executed in reverse order of derivation.
  • 20. 20 Message Passing “The process of invoking an operation on an object. In response to a message the corresponding method is executed in the object”.
  • 22. 22 Inheritance • “Inheritance is the mechanism to provides the power of reusability and extensibility.” • “Inheritance is the process by which one object can acquire the properties of another object.”
  • 27. 27 Base Class • “Base class is a class which defines those qualities common to all objects to be derived from the base.” • The base class represents the most general description. • A class that is inherited is referred to as a base class.
  • 28. 28 Derived Class • “The classes derived from the base class are usually referred to as derived classes.” • “A derived class includes all features of the generic base class and then adds qualities specific to the derived class.” • The class that does the inheriting is called the
  • 29. 29 Inheritance Note: Derived class can be used as a base class for another derived class. • In C++, inheritance is achieved by allowing one class to incorporate another class into its declaration.
  • 30. 30 Inheritance • Syntax: class derived_class: Acesss_specifier base_class { }; • Example: class CRectangle: public Cpolygon{ class CTriangle: public Cpolygon{ }; };
  • 31. 31 Inheritance & Access Specifier Access public protected private Members of the same class Yes Yes Yes Members of derived classes Yes Yes No Non-members Yes No No
  • 32. 32 Public base class Inheritance • All public members of the base class become public members of the derived class. • All protected members of the base class become protected members of the derived class.
  • 33. 33 Private base class Inheritance • All public and protected members of the base class become private members of the derived class. • But private members of the base class remain private to base class only, not accessible to the derived class.
  • 34. 34 Protected Members of Base Class • Member is notaccessible by othernon member elements of the program. • The base class' protected members become protected members of the derived class and are, therefore, accessible by the derived class.
  • 35. 35 Protected Base-Class Inheritance • All public and protected members of the base class become protected members of the derived class. • All public members of the base class become unavailable to main() function. • All private members of the base class become unavailable to the derived class.
  • 36. 36 Inheritance & Access Specifier Access public protected private Members of the same class Yes Yes Yes Members of derived classes Yes Yes No Non-members Yes No No
  • 37. • Syntax: class derived: Acess_specifier base1, Acess_specifier base2 { }; • Example: class Orange: Acess_specifier Yellow, Acess_specifier Red { }; 37 Inheriting Multiple Base Classes
  • 38. 38 Inheriting Multiple Base Classes Orange Red Yellow
  • 40. 40 Constructors, Destructors & Inheritance • When an object of a derived class is created, if the base class contains a constructor, it will be called first, followed by the derived class' constructor. • When a derived object is destroyed, its destructor is called first, followed by the base class' destructor.
  • 41. 41 Passing Parameters to Base-Class Constructors • Making use of an expanded form of the derived class's constructor declaration, we can pass arguments to one or more base-class constructors. • Syntax: derived-constructor(arg-list) : base1(arg-list), base2(arg-list), … baseN(arg-list) { // body of derived constructor }
  • 42. • As we are arguments to a base-class constructor are passed via arguments of the derived class' constructor. • Even if a derived class‘ constructor does not use any arguments, we need to declare a constructor as if the base class requires it. • The arguments passed to the derived class are simply passed along to the base class constructor. 42 Passing Parameters to Base-Class Constructors
  • 43. 43 Granting Access • When a base class is inherited as private: - all public and protected members of that class become private members of the derived class. • But in some certain circumstances, we want to restore one or more inherited members to their original access specification.
  • 44. 44 Granting Access • To accomplish this : using access declaration
  • 45. 45 Granting Access • using statement: is designed primarily to support namespaces. • Access declaration: restores an inherited member access specification • Syntax: • base_class_name::member;
  • 46. 46 Granting Access • Access declaration is done under the appropriate access heading in the derived class’ declaration. Note: No type declaration is required.
  • 47. 47 Granting Access class base { public: int j; }; class derived: private base { public: // here is access declaration base::j; };
  • 48. 48 Program for Implementation student Result_on_Web private Program to implement the given hierarchy assuming proper properties for student class. The Result class computes the result of every student. The Result_on_Web class displays the result upon getting the grant for USN Result protected
  • 49. 49 Hierarchy of Classes i i j & k j k 2 copies
  • 51. 51 Virtual Base Classes • Used to prevent multiple copies of the base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. • Syntax: class derived : virtual public base { . . . };
  • 52. 52 Virtual Functions • “A virtual function is a member function that is declared within a base class and redefined by a derived class.” • Virtual functions implements the "one interface, multiple methods" philosophy under polymorphism.
  • 53. 53 Virtual Functions • The virtual function within base class defines the form of the interface to that function • Each redefinition of the virtual function by a derived class implements its operation as it relates specifically to the derived class. That is, the redefinition creates a specific method.
  • 54. 54 Virtual Functions • To create a virtual function, precede the function‘s declaration in the base class with the keyword virtual. • Example: class base { public: virtual void member_func(){ } };
  • 56. 56 Virtual Functions • When accessed "normally" virtual functions behave just like any other type of class member function. • But virtual functions’ importance and capacity lies in supporting the run-time polymorphism when they accessed via a pointer.
  • 57. 57 Virtual Functions • How to implement run-time polymorphism? - create base-class pointer can be used to point to an object of any class derived from that base - initialize derived object(s) to base class object. • Based upon which derived class objects’ assignment to the base class pointer, c++ determines which version of the virtual function to be called. And this determination is made at run time.
  • 58. 58 Virtual Functions • The redefinition of a virtual function by a derived class appears similar to function overloading? • No • The prototype for a redefined virtual function must match exactly the prototype specified in the base class.
  • 59. 59 Virtual Functions Restrictions: • All aspects of its prototype must be the same as base class virtual function. • Virtual functions are of non-static members. • Virtual functions can not be friends. • Constructor functions cannot be virtual. • But destructor functions can be virtual. •NOTE: Function overriding is used to describe virtual function redefinition by a derived class.
  • 60. 60 Destructor functions can be virtual? • Yes. • In large projects, the destructor of the derived class was not called at all. • This is where the virtual mechanism comes into our rescue. By making the Base class Destructor virtual, both the destructors will be called in order.
  • 61. 61 Function overriding “A function overriding is a process in which a member function that is declared within a base class and redefined by a derived class to implement the "one interface, multiple methods" philosophy under polymorphism.”
  • 62. 62 Calling a Virtual Function Through a Base Class Reference • Sincereference is an implicit pointer,it can be used to access virtual function. • When a virtual function is called through a base-class reference, the version of the function executed is determined by the object being referred to at the time of the call.
  • 63. 63 The Virtual Attribute Is Inherited • When a virtual function is inherited, its virtual nature is also inherited. Base Derived1 Derived2 Virtual function override override
  • 64. 64 Virtual Functions Are Hierarchical • Virtual functions are also hierarchical in nature. • This means that when a derived class fails to override a virtual function then first redefinition found in reverse order of derivation is used.
  • 65. 65 Virtual Functions Are Hierarchical Base Derived1 Derived2 Virtual function override
  • 66. 66 Virtual Functions Are Hierarchical Base Derived1 Virtual function override Derived2
  • 67. 67 Pure Virtual Functions • “A pure virtual function is a virtual function that has no definition within the base class.” • To declare a pure virtual function: Syntax: virtual rtype func-name(parameter-list) = 0;
  • 68. 68 Pure Virtual Functions • When a virtual function is made pure, any derived class must provide its definition. • If the derived class fails to override the pure virtual function, a compile-time error will result. NOTE: When a virtual function is declared as pure, then all derived classes must override it.
  • 69. 69 Abstract Classes • “A class that contains at least one pure virtual function then it is said to be abstract class.” • No objects of an abstract class be created. • Abstract class constitutes an incomplete type that is used as a foundation for derived. classes.
  • 70. 70 Using Virtual Functions • We can achieve the most powerful and flexible ways to implement the "one interface, multiple methods“. • We can create a class hierarchy that moves from general to specific (base to derived).
  • 71. 71 Using Virtual Functions • We can define all common features and interfaces in a base class. • Specific actions can be implemented only by the derived class. • We can add new case easily.
  • 72. 72 Encapsulation “Mechanism that associates the code and the data it manipulates into a single unit and keeps them safe from external interference and misuse.”
  • 73. 73 Encapsulation Class: student Attributes: st_name, st_id, branch, semester Functions: Enroll() Displayinfo() Result() Performance()
  • 74. 74 Data Abstraction “A data abstraction is a simplified view of an object that includes only features one is interested in while hides away the unnecessary details.” “Data abstraction becomes an abstract data type (ADT)or a user-defined type.”
  • 76. 76 C++ Implementation class student { char st_name[30]; char st_id[10]; char branch[10]; char semester[10]; Void Enroll( ); Void Displayinfo( ); Voide Result( ); Void Performance( ); }; class stack { int stck[SIZE]; int tos; void init(); void push(int i); int pop(); };
  • 77. 77 Polymorphism • Polymorphism means that the same thing can exist in two forms. • “Polymorphism is in short the ability to call different functions by just using one type of function call.”
  • 79. Dynamic Binding “ Dynamic Binding is the process of linking of the code associated with a procedure call at the run-time”. 79
  • 80. 80 Early vs. Late Binding • “Early binding refers to events that occur at compile time.” • Early binding occurs when all information needed to call a function is known at compile time. • Examples : function calls ,overloaded function calls, and overloaded operators.
  • 81. 81 Early vs. Late Binding • “Late binding refers to function calls that are not resolved until run time.” • Late binding can make for somewhat slower execution times. • Example: virtual functions