SlideShare a Scribd company logo
17/05/2025
Object Oriented Programming
W7 - L7
17/05/2025
Pointers
Overview:
Variable & MEMORY
 Computer memory is a collection of different memory locations.
 These memory locations are numbered sequentially.
 Each variable is created at a unique location in memory known as its address.
 A program may declare many variables.
 A variable declaration reserves specific amount of space in memory for a particular
variable.
 The variable name is used to refer to that memory location.
 It allows the users to access a value in the memory.
 The computer refers to the memory using address.
17/05/2025
 When we declare a variable three things are associated with that
variable
 Variable name
 Variable type
 Variable address
17/05/2025
Example:
Int main()
{
Int a;
Cout << “The address of a is: ” << &a;
Return 0;
}
17/05/2025
Can we store address of variable in
another variable?
Int main()
{
Int a;
Int b;
B = &a
Cout << “The address of a is: ” << &a;
Return 0;
}
17/05/2025
Pointer
 A pointer is a variable that is used to store a memory address.
 WHY we need Memory address when we can
use variable with their names?
 Pointers can access and manipulate data in
computer memory directly
17/05/2025
Advantages of Pointer:
 It can access the memory address directly.
 It can save memory
 It runs faster as it doesnot duplicate data.
17/05/2025
Example:
Int main()
{
Int n ;
Int *ptr ;
Cout << “enter an integer” ;
Cin >> n ;
Ptr = &n;
Cout << “the value of n is” << n ;
Cout << “the address of n” << ptr ;
Return 0;
}
17/05/2025
Void pointer(A void pointer is a pointer that has no associated data type with it. A void pointer
can hold address of any type and can be typecasted to any type)
// find output
Int main()
{
Int a = 10;
Float b = 10.5;
Char c = ‘a’ ;
Int *ptr;
Ptr = &a;
Cout << “address stored in pointer is : ” << ptr ;
}
17/05/2025
Find Error??
Int main()
{
Int a = 10;
Float b = 10.5;
Char c = ‘a’ ;
Int *ptr;
Ptr = &b;
Cout << “address stored in pointer is : ” << ptr ;
}
17/05/2025
Void Pointer
Int main()
{
Int a = 10;
Float b = 10.5;
Char c = ‘a’ ;
void *ptr;
Ptr = &a;
Cout<<“value of a is:” << a;
Cout << “address stored in pointer is : ” << ptr ;
Ptr = &b;
Cout<<“value of b is:” << b;
Cout << “address stored in pointer is : ” << ptr ;
Ptr = &c;
Cout<<“value of c is:” << c;
Cout << “address stored in pointer is : ” << ptr ;
17/05/2025
Output:
17/05/2025
Dereference Operator: ( * )
In computer programming, a dereference
operator, also known as an indirection
operator, operates on a pointer variable. It returns
the location value, in memory pointed to by the
variable's value.
17/05/2025
Example:
Int main()
{
Int a = 20 ;
Int *ptr ;
Ptr = &a;
Cout << the value stored in pointer is: “ << *ptr ;
}
17/05/2025
Output :
 the value stored in pointer is: 20
17/05/2025
Data manipulation using Pointers
Int main ()
{
Int a , b , s , *p1, *p2 ;
P1 = &a ;
P2 = &b;
Cout << Enter an Integer : “ ;
Cin >> *p1;
Cout << Enter an Integer : “ ;
Cin >> *p2;
S = *p1 + *p2 ;
Cout << “sum is : ” << s;
}
17/05/2025
This pointer
 To know the address of current object , we use this pointer.
 Used to distinguished our data members and local variables when
both are declared with the same name.
 We identify the data member using this pointer
17/05/2025
Example:
Class person
{
Int age ;
String name ;
Person(int age , string name)
{
Age = age ; //compiler confusion
Name = name ;
}
Void printvalue()
{
Cout << “age” << age ;
Cout << “name” << name ;
}
};
17/05/2025
Int main ()
{
Person a(45 , “ABC”);
a.printvalue();
}
17/05/2025
Output:
 Garbage value
17/05/2025
Solution : this Pointer
Class person
{
Int age ;
String name ;
Person(int age , string name)
{
This ->Age = age ;
This ->Name = name ;
Void printvalue()
{
Cout << “age” << age ;
Cout << “name” << name ;
}
};
17/05/2025
Int main ()
{
Person a(35 , “abc”);
a.printvalue();
}
17/05/2025
Output :
 35
 abc
17/05/2025
Task:
 we have two data members num and ch.
 In member function setMyValues() we have two local variables having same name
as data members name.
 Create display function to show both values.
 In such case if you want to assign the local variable value to the data members
then you won’t be able to do until unless you use this pointer, because the compiler
won’t know that you are referring to object’s data members unless you use this
pointer.
17/05/2025
Solution:
#include <iostream>
using namespace std;
class Demo
{
private:
int num;
char ch;
public:
void setMyValues(int num, char ch)
{
this->num =num;
this->ch=ch;
}
void displayMyValues()
{
cout<<num<<endl; cout<<ch;
}
};
int main()
{
Demo obj;
obj.setMyValues(100, 'A');
obj.displayMyValues();
return 0;
}
17/05/2025
Polymorphism
 One name multiple forms
 Poly means many and morphism means forms
 The behaviour of the object can be implemented at rum time.
1. Compile time polymorphism
 Function overloading / method overloading / constructor overloading /
 operator overloading
2. Run time polymorphism
 virtual function
 Pointer to objects:
 pointer can also refer to an object of a class. The member of an object can
be accessed through pointers by using the symbol ->(member access
operator)
 Ptr->member
17/05/2025
Overview: // early binding compiler
already knows which function to call
Class A
{
Public: void show()
{
Cout<< “base class” ;
}
};
Derived Class
Class B: public A
{
Public: void show()
{
Cout<< “Derived
class” ;
}
};
Int main()
{
B obj;
Obj.show();
}
Output:
17/05/2025
Overview: // early binding
Class A
{
Public: void show()
{
Cout<< “base class” ;
}
};
Derived Class
Base class
Class B: public A
{
Public: void show()
{
Cout<< “Derived
class” ;
}
};
Int main()
{
B obj;
Obj.show();
Obj.A.show();
}
Output:
17/05/2025
Overview: // late Binding
Class A
{
Public: void show()
{
Cout<< “base class” ;
}
};
base class
Class B: public A
{
Public: void show()
{
Cout<< “Derived
class” ;
}
};
Int main()
{
A *ptr ;
A obj;
Ptr = &obj ;
Ptr - > show() ;
}
Output:
17/05/2025
Virtual Base class / Virtual Function :
 Key word virtual
 Virtual function is re defined in derived class.
 When a virtual function is defined in base class,
then the pointer to base class is created. Now
on the basis of type of object assigned the
respective class function is called.
 This is also called run time polymorphism.
17/05/2025
Virtual Function
 A virtual function (also known as virtual methods) is a member
function that 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 method.
17/05/2025
Example: Class B : public A
{
Public:
Void show()
{
Cout << “Child Class B” ;
}
};
Class A
{
Public:
Void show()
{
Cout << “parent Class A” ;
}
};
Class C : public A
{
Public:
Void show()
{
Cout << “Child Class C” ;
}
};
17/05/2025
Int main ()
{
A obj1 ;
B obj2 ;
C obj3;
A *ptr;
Ptr = &obj1 ;
Ptr-> show() ;
Ptr = &obj2 ;
Ptr->show();
Ptr = &obj3;
Ptr->show();
}
Pointer can store address of child class , but
with the same pointer if we want to access
child class member that also exist in parent
class , this will always call member function of
parent class.
17/05/2025
Output:
17/05/2025
Using virtual keyword:
One function is providing multiple functionalities depends on which
type of address , pointer is holding.
17/05/2025
Example: Class B : public A
{
Public:
Void show()
{
Cout << “Child Class B” ;
}
};
Class A
{
Public:
Virtual Void show()
{
Cout << “parent Class A” ;
}
};
Class C : public A
{
Public:
Void show()
{
Cout << “Child Class C” ;
}
};
17/05/2025
Int main ()
{
A obj1 ;
B obj2 ;
C obj3;
A *ptr;
Ptr = &obj1 ;
Ptr-> show() ;
Ptr = &obj2 ;
Ptr->show();
Ptr = &obj3;
Ptr->show();
}
17/05/2025
Output:
17/05/2025
Working:
 When a member function is declared as a virtual in parent class and is
called with pointer , the compiler checks the type of object referred by
the pointer. It executes the member function according to the type of
object not by type of pointer.
 The type of pointer in each call is different so the compiler executes
different function each time.
17/05/2025
Binding:
 Binding refers to the process of converting identifiers (such as variable and
performance names into addresses.)
 Binding is done for each variable and functions.
 For functions, it means that matching the call with the right function
definition call by the compiler.
 Binding can be performed in two ways:
 Early binding/compile time binding/ static binding(As the name indicates
compiler directly associate an address to the function call. In early binding,
compiler knows at the time of compilation which block of code is to be
executed upon a function call.)
 Late binding/ Dynamic binding(in late binding, compiler does not know at
the time of compilation that which block of code is to be executed upon a
function call. Late binding is attained by using virtual functions.)
17/05/2025
Early Binding
 Is also called compile time binding or static binding
 As the name indicates, compiler directly associate an address to
the function call.
 In early binding , compiler knows at the time of compilation which
block of code is to be executed upon a function call.
17/05/2025
Example: Early Binding
Class A
{
Public:
Void show()
{
Cout << “parent Class A” ;
}
};
Class B : Class A
{
Public:
Void show()
{
Cout << “Derived Class B” ;
}
};
Int main()
{
B obj ;
Obj.show();
Obj.A.show();
}
17/05/2025
Late Binding
 In late binding compiler doesn’t know at the time of compilation that
which block of code is to be executed upon a function call.
 Late binding is attained using virtual function .
17/05/2025
Example :
Class parent
{
Public:
Virtual void show()
{
Cout<< “parent
class” ;
}
};
Class child1: public parent
{
Public:
void show()
{
Cout<< “child1 class” ;
}
};
17/05/2025
Class child2: public parent
{
Public:
void show()
{
Cout<< “child2 class” ;
}
};
17/05/2025
Int main()
{
Parent *ptr[5] ; // array of pointers
//This pointer can store address of child class address .
Int op , i ;
// for object creation
Cout << “Enter 1 for parent , 2 for child1 & 3 for
child2” ;
17/05/2025
For (I = 0 ; I < 5 ; i++)
{
Cout << “which object to create ?” ;
Cin >> op ;
If (op == 1)
Ptr[i] = new parent ; // create new object of parent
Else if (op ==2)
Ptr[i] == new child1;
Else (op ==3)
Ptr[i] == new child2;
}
17/05/2025
For (I = o ; I <5 ; i++)
{
Ptr[i] -> show();
It will call that function whose class object address is stored in pointer
array.
}
17/05/2025
17/05/2025
 What if we remove virtual keyword?
 What will be the output ??
17/05/2025
Abstract class(Model class) and
pure Virtual function
 No definition of virtual function assigned with zero.
 We can’t create the object of parent class having pure virtual
function
 If we don’t override the function in child class that was present in
abstract class then the child class will also become abstract class.
17/05/2025
Pure virtual function
17/05/2025
Output:
Child1 class…
Child2 class…

More Related Content

Similar to Object oriented programming slides for presentation (20)

pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
rattaj
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
Pointer in C++_Somesh_Kumar_Dewangan_SSTCPointer in C++_Somesh_Kumar_Dewangan_SSTC
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
drsomeshdewangan
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCEPOINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Pointers
PointersPointers
Pointers
Prof. Dr. K. Adisesha
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
Rajiv Gupta
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
Abdullah Jan
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Prof .Pragati Khade
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
prabhat kumar
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
rattaj
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
Pointer in C++_Somesh_Kumar_Dewangan_SSTCPointer in C++_Somesh_Kumar_Dewangan_SSTC
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
drsomeshdewangan
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCEPOINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
Rajiv Gupta
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
Abdullah Jan
 

Recently uploaded (20)

Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Ad

Object oriented programming slides for presentation

  • 2. 17/05/2025 Pointers Overview: Variable & MEMORY  Computer memory is a collection of different memory locations.  These memory locations are numbered sequentially.  Each variable is created at a unique location in memory known as its address.  A program may declare many variables.  A variable declaration reserves specific amount of space in memory for a particular variable.  The variable name is used to refer to that memory location.  It allows the users to access a value in the memory.  The computer refers to the memory using address.
  • 3. 17/05/2025  When we declare a variable three things are associated with that variable  Variable name  Variable type  Variable address
  • 4. 17/05/2025 Example: Int main() { Int a; Cout << “The address of a is: ” << &a; Return 0; }
  • 5. 17/05/2025 Can we store address of variable in another variable? Int main() { Int a; Int b; B = &a Cout << “The address of a is: ” << &a; Return 0; }
  • 6. 17/05/2025 Pointer  A pointer is a variable that is used to store a memory address.  WHY we need Memory address when we can use variable with their names?  Pointers can access and manipulate data in computer memory directly
  • 7. 17/05/2025 Advantages of Pointer:  It can access the memory address directly.  It can save memory  It runs faster as it doesnot duplicate data.
  • 8. 17/05/2025 Example: Int main() { Int n ; Int *ptr ; Cout << “enter an integer” ; Cin >> n ; Ptr = &n; Cout << “the value of n is” << n ; Cout << “the address of n” << ptr ; Return 0; }
  • 9. 17/05/2025 Void pointer(A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typecasted to any type) // find output Int main() { Int a = 10; Float b = 10.5; Char c = ‘a’ ; Int *ptr; Ptr = &a; Cout << “address stored in pointer is : ” << ptr ; }
  • 10. 17/05/2025 Find Error?? Int main() { Int a = 10; Float b = 10.5; Char c = ‘a’ ; Int *ptr; Ptr = &b; Cout << “address stored in pointer is : ” << ptr ; }
  • 11. 17/05/2025 Void Pointer Int main() { Int a = 10; Float b = 10.5; Char c = ‘a’ ; void *ptr; Ptr = &a; Cout<<“value of a is:” << a; Cout << “address stored in pointer is : ” << ptr ; Ptr = &b; Cout<<“value of b is:” << b; Cout << “address stored in pointer is : ” << ptr ; Ptr = &c; Cout<<“value of c is:” << c; Cout << “address stored in pointer is : ” << ptr ;
  • 13. 17/05/2025 Dereference Operator: ( * ) In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, in memory pointed to by the variable's value.
  • 14. 17/05/2025 Example: Int main() { Int a = 20 ; Int *ptr ; Ptr = &a; Cout << the value stored in pointer is: “ << *ptr ; }
  • 15. 17/05/2025 Output :  the value stored in pointer is: 20
  • 16. 17/05/2025 Data manipulation using Pointers Int main () { Int a , b , s , *p1, *p2 ; P1 = &a ; P2 = &b; Cout << Enter an Integer : “ ; Cin >> *p1; Cout << Enter an Integer : “ ; Cin >> *p2; S = *p1 + *p2 ; Cout << “sum is : ” << s; }
  • 17. 17/05/2025 This pointer  To know the address of current object , we use this pointer.  Used to distinguished our data members and local variables when both are declared with the same name.  We identify the data member using this pointer
  • 18. 17/05/2025 Example: Class person { Int age ; String name ; Person(int age , string name) { Age = age ; //compiler confusion Name = name ; } Void printvalue() { Cout << “age” << age ; Cout << “name” << name ; } };
  • 19. 17/05/2025 Int main () { Person a(45 , “ABC”); a.printvalue(); }
  • 21. 17/05/2025 Solution : this Pointer Class person { Int age ; String name ; Person(int age , string name) { This ->Age = age ; This ->Name = name ; Void printvalue() { Cout << “age” << age ; Cout << “name” << name ; } };
  • 22. 17/05/2025 Int main () { Person a(35 , “abc”); a.printvalue(); }
  • 24. 17/05/2025 Task:  we have two data members num and ch.  In member function setMyValues() we have two local variables having same name as data members name.  Create display function to show both values.  In such case if you want to assign the local variable value to the data members then you won’t be able to do until unless you use this pointer, because the compiler won’t know that you are referring to object’s data members unless you use this pointer.
  • 25. 17/05/2025 Solution: #include <iostream> using namespace std; class Demo { private: int num; char ch; public: void setMyValues(int num, char ch) { this->num =num; this->ch=ch; } void displayMyValues() { cout<<num<<endl; cout<<ch; } }; int main() { Demo obj; obj.setMyValues(100, 'A'); obj.displayMyValues(); return 0; }
  • 26. 17/05/2025 Polymorphism  One name multiple forms  Poly means many and morphism means forms  The behaviour of the object can be implemented at rum time. 1. Compile time polymorphism  Function overloading / method overloading / constructor overloading /  operator overloading 2. Run time polymorphism  virtual function  Pointer to objects:  pointer can also refer to an object of a class. The member of an object can be accessed through pointers by using the symbol ->(member access operator)  Ptr->member
  • 27. 17/05/2025 Overview: // early binding compiler already knows which function to call Class A { Public: void show() { Cout<< “base class” ; } }; Derived Class Class B: public A { Public: void show() { Cout<< “Derived class” ; } }; Int main() { B obj; Obj.show(); } Output:
  • 28. 17/05/2025 Overview: // early binding Class A { Public: void show() { Cout<< “base class” ; } }; Derived Class Base class Class B: public A { Public: void show() { Cout<< “Derived class” ; } }; Int main() { B obj; Obj.show(); Obj.A.show(); } Output:
  • 29. 17/05/2025 Overview: // late Binding Class A { Public: void show() { Cout<< “base class” ; } }; base class Class B: public A { Public: void show() { Cout<< “Derived class” ; } }; Int main() { A *ptr ; A obj; Ptr = &obj ; Ptr - > show() ; } Output:
  • 30. 17/05/2025 Virtual Base class / Virtual Function :  Key word virtual  Virtual function is re defined in derived class.  When a virtual function is defined in base class, then the pointer to base class is created. Now on the basis of type of object assigned the respective class function is called.  This is also called run time polymorphism.
  • 31. 17/05/2025 Virtual Function  A virtual function (also known as virtual methods) is a member function that 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 method.
  • 32. 17/05/2025 Example: Class B : public A { Public: Void show() { Cout << “Child Class B” ; } }; Class A { Public: Void show() { Cout << “parent Class A” ; } }; Class C : public A { Public: Void show() { Cout << “Child Class C” ; } };
  • 33. 17/05/2025 Int main () { A obj1 ; B obj2 ; C obj3; A *ptr; Ptr = &obj1 ; Ptr-> show() ; Ptr = &obj2 ; Ptr->show(); Ptr = &obj3; Ptr->show(); } Pointer can store address of child class , but with the same pointer if we want to access child class member that also exist in parent class , this will always call member function of parent class.
  • 35. 17/05/2025 Using virtual keyword: One function is providing multiple functionalities depends on which type of address , pointer is holding.
  • 36. 17/05/2025 Example: Class B : public A { Public: Void show() { Cout << “Child Class B” ; } }; Class A { Public: Virtual Void show() { Cout << “parent Class A” ; } }; Class C : public A { Public: Void show() { Cout << “Child Class C” ; } };
  • 37. 17/05/2025 Int main () { A obj1 ; B obj2 ; C obj3; A *ptr; Ptr = &obj1 ; Ptr-> show() ; Ptr = &obj2 ; Ptr->show(); Ptr = &obj3; Ptr->show(); }
  • 39. 17/05/2025 Working:  When a member function is declared as a virtual in parent class and is called with pointer , the compiler checks the type of object referred by the pointer. It executes the member function according to the type of object not by type of pointer.  The type of pointer in each call is different so the compiler executes different function each time.
  • 40. 17/05/2025 Binding:  Binding refers to the process of converting identifiers (such as variable and performance names into addresses.)  Binding is done for each variable and functions.  For functions, it means that matching the call with the right function definition call by the compiler.  Binding can be performed in two ways:  Early binding/compile time binding/ static binding(As the name indicates compiler directly associate an address to the function call. In early binding, compiler knows at the time of compilation which block of code is to be executed upon a function call.)  Late binding/ Dynamic binding(in late binding, compiler does not know at the time of compilation that which block of code is to be executed upon a function call. Late binding is attained by using virtual functions.)
  • 41. 17/05/2025 Early Binding  Is also called compile time binding or static binding  As the name indicates, compiler directly associate an address to the function call.  In early binding , compiler knows at the time of compilation which block of code is to be executed upon a function call.
  • 42. 17/05/2025 Example: Early Binding Class A { Public: Void show() { Cout << “parent Class A” ; } }; Class B : Class A { Public: Void show() { Cout << “Derived Class B” ; } }; Int main() { B obj ; Obj.show(); Obj.A.show(); }
  • 43. 17/05/2025 Late Binding  In late binding compiler doesn’t know at the time of compilation that which block of code is to be executed upon a function call.  Late binding is attained using virtual function .
  • 44. 17/05/2025 Example : Class parent { Public: Virtual void show() { Cout<< “parent class” ; } }; Class child1: public parent { Public: void show() { Cout<< “child1 class” ; } };
  • 45. 17/05/2025 Class child2: public parent { Public: void show() { Cout<< “child2 class” ; } };
  • 46. 17/05/2025 Int main() { Parent *ptr[5] ; // array of pointers //This pointer can store address of child class address . Int op , i ; // for object creation Cout << “Enter 1 for parent , 2 for child1 & 3 for child2” ;
  • 47. 17/05/2025 For (I = 0 ; I < 5 ; i++) { Cout << “which object to create ?” ; Cin >> op ; If (op == 1) Ptr[i] = new parent ; // create new object of parent Else if (op ==2) Ptr[i] == new child1; Else (op ==3) Ptr[i] == new child2; }
  • 48. 17/05/2025 For (I = o ; I <5 ; i++) { Ptr[i] -> show(); It will call that function whose class object address is stored in pointer array. }
  • 50. 17/05/2025  What if we remove virtual keyword?  What will be the output ??
  • 51. 17/05/2025 Abstract class(Model class) and pure Virtual function  No definition of virtual function assigned with zero.  We can’t create the object of parent class having pure virtual function  If we don’t override the function in child class that was present in abstract class then the child class will also become abstract class.

Editor's Notes

  • #10: we declare integer type pointer here If we want to store float type address we need float type pointer
  • #13: We can access the value from n pointer adreess