Virtual functions allow objects of derived classes to be referenced by pointers or references to the base class. This allows polymorphic behavior where calling code does not need to know the exact derived class, but the correct overridden function for that derived class will be called at runtime. Some key points:
- Virtual functions provide runtime polymorphism in C++. The correct function to call is determined by the actual object type, not the reference/pointer type.
- Pure virtual functions are declared in a base class but provide no definition - derived classes must override these to be instantiable.
- Constructors cannot be virtual but destructors can, and it is important to make base class destructors virtual to ensure proper cleanup
The document discusses different types of polymorphism in C++ including function overloading, operator overloading, and virtual functions. It also covers key concepts related to pointers in C++ like pointer declaration syntax, dereferencing pointers, pointer arithmetic, arrays of pointers, pointers to functions, pointers to class members, and pointers to derived class objects.
This document discusses pointers, virtual functions, and polymorphism in C++. It begins by defining pointers and how they can refer to and manipulate objects and memory addresses. It then explains virtual functions, which allow dynamic binding at runtime, and polymorphism. There are two types of polymorphism: compile-time polymorphism which includes function overloading, and runtime polymorphism using virtual functions. Virtual functions allow derived classes to override base class functions. The document also covers pure virtual functions, virtual constructors and destructors, and provides examples of how pointers and virtual functions enable polymorphism.
1. Pointers allow variables to store the address of other variables in memory. The & operator returns the address of a variable, which can be assigned to a pointer variable. Pointer variables are strongly typed based on the data type they point to.
2. The * operator dereferences a pointer to access or modify the variable being pointed to. Assigning a value to a dereferenced pointer modifies the original variable. Pointers can be assigned to each other as long as they point to compatible types.
3. Dynamic memory allocation with new creates objects in heap memory that persist until deleted. new returns a pointer to the new object. delete removes an object from the heap. Dangling pointers occur after deleting a
Polymorphism allows objects of different classes to respond differently to the same function call. It is an important feature of object-oriented programming that allows for a base class interface to be used for derived class objects. Polymorphism can be achieved through virtual functions. Early binding, also called static binding, is when the function call is resolved at compile time based on the type of the object. Late binding, or dynamic binding, is when the function call is resolved at runtime based on the actual type of the object. Pure virtual functions can only be declared in a base class and must be defined in derived classes, making the base class abstract.
This code will generate a compile time error as Base is an abstract class due to pure virtual function show(). An abstract class cannot be instantiated.
This document discusses pointers in C++. It defines pointers as variables that store memory addresses of other variables. It covers declaring and initializing pointers, using the address and dereference operators, pointer arithmetic, references, and passing pointers as function arguments. The document includes examples of pointer code and output to demonstrate these concepts.
The document outlines the course content for a C++ introductory course, including introductions to OOP concepts like classes and objects, pointers, functions, inheritance, and polymorphism. It also covers basic C++ programming concepts like I/O, data types, operators, and data structures. The course aims to provide students with fundamental C++ programming skills through explanations and examples of key C++ features.
Here is a C++ program that implements a Polynomial class with overloaded operators as specified in the question:
#include <iostream>
using namespace std;
class Term {
public:
int coefficient;
int exponent;
Term(int coeff, int exp) {
coefficient = coeff;
exponent = exp;
}
};
class Polynomial {
public:
Term* terms;
int numTerms;
Polynomial() {
terms = NULL;
numTerms = 0;
}
Polynomial(Term t[]) {
terms = t;
numTerms = sizeof(t)/sizeof(t[0]);
}
~Polynomial() {
delete[] terms;
}
Polynomial
Pointers,virtual functions and polymorphism cpprajshreemuthiah
This document discusses key concepts in object-oriented programming in C++ including polymorphism, pointers, pointers to objects and derived classes, virtual functions, and pure virtual functions. Polymorphism allows one name to have multiple forms through function and operator overloading as well as virtual functions. Pointers store the memory address of a variable rather than the data. Pointers can be used with objects, arrays, strings, and functions. Virtual functions allow calling a derived class version of a function through a base class pointer. Pure virtual functions define an abstract base class that cannot be instantiated.
The document discusses classes and objects in object-oriented programming. It defines a class as a blueprint for objects that bind data and functions together. A class defines data members and member functions. Objects are instances of a class that can access class data and functions. The document provides examples of defining a class called "test" with private and public members, and creating objects of the class to demonstrate accessing members.
This document provides an overview of pointers, polymorphism, inheritance, and other object-oriented programming concepts in C++. It defines pointers and describes how they store memory addresses. It explains runtime and compile-time polymorphism using method overriding and overloading. Inheritance hierarchies like single, multiple, and multilevel inheritance are covered. Virtual functions and base classes are defined as ways to implement polymorphism. Abstract classes with pure virtual functions are introduced.
1. The document discusses pointers in C++. Pointers are variables that store memory addresses and allow programs to indirectly access the value of the variable located at that address.
2. Pointers enable programs to dynamically allocate memory at runtime using operators like new and delete. They also allow accessing memory locations of variables and instructions.
3. Pointer usage can improve efficiency but must be used carefully as uninitialized or wild pointers can cause bugs and system crashes. Proper declaration and initialization of pointers using operators like * and & is important.
The document discusses C++ scope resolution operator (::) and pointers. It explains that :: is used to qualify hidden names and access variables or functions in the global namespace when a local variable hides it. It also discusses pointers, which are variables that store memory addresses. Pointers allow dynamic memory allocation and are useful for passing arguments by reference. Key pointer concepts covered include null pointers, pointer arithmetic, relationships between pointers and arrays, arrays of pointers, pointer to pointers, and passing/returning pointers in functions.
The document discusses pointers in C++. It defines pointers as variables that hold the memory addresses of other variables. It describes how to declare and initialize pointers, use pointer arithmetic and operators like & and *, pass pointers as function parameters, and dynamically allocate and free memory using pointers and operators like new and delete. Pointers allow programs to write efficiently, utilize memory properly, and dynamically allocate memory as needed.
Pointer is a variable that stores the memory address of another variable. It allows dynamic memory allocation and access of memory locations. There are three ways to pass arguments to functions in C++ - pass by value, pass by reference, and pass by pointer. Pass by value copies the value, pass by reference copies the address, and pass by pointer passes the address of the argument. Pointers can also point to arrays or strings to access elements. Arrays of pointers can store multiple strings. References are alternative names for existing variables and any changes made using the reference affect the original variable. Functions can return pointers or references.
This document provides an overview of pointers in C++. It defines pointers as variables that store the memory address of another variable. It discusses declaring and initializing pointer variables, pointer operators like & and *, pointer arithmetic, passing pointers to functions, arrays of pointers, strings of pointers, objects of pointers, and the this pointer. Advantages of pointers include efficient handling of arrays, direct memory access for speed, reduced storage space, and support for complex data structures. Limitations include slower performance than normal variables, inability to store values, needing null references, and risk of errors from incorrect initialization.
Day 1 of the training covers introductory C++ concepts like object-oriented programming, compilers, IDEs, classes, objects, and procedural programming concepts. Day 2 covers more advanced class concepts like constructors, destructors, static members, returning objects, and arrays of objects. Day 3 covers function and operator overloading.
I prepared this slides for the student of FSC BSC BS Computer Science Students. These Slides are very easy to read and understand the pointer logic used in C++ Programming.
All Topic related to pointer is discussed and examples are given
A pointer can point to an object by holding the object's address value. This is known as a "this pointer". A derived class inherits properties from its base class. A pointer to the base class can point to a derived class object, allowing access to both base and derived class members. Virtual functions ensure the correct overridden function is called at runtime based on the actual object type, rather than the pointer type. They are declared with the virtual keyword in the base class.
Pointer
Features of Pointers
Pointer Declaration
Pointer to Class
Pointer Object
The this Pointer
Pointer to Derived Classes and Base Class
Binding Polymorphisms and Virtual Functions
Introduction
Binding in C++
Virtual Functions
Rules for Virtual Function
Virtual Destructor
The document defines and provides examples of polymorphism in object-oriented programming. It discusses two types of polymorphism: static and dynamic. Static polymorphism is resolved at compile-time through function overloading and operator overloading, while dynamic polymorphism uses virtual functions and is resolved at run-time. Virtual functions, pure virtual functions, and abstract classes are also explained as key aspects of implementing dynamic polymorphism.
Parenting Teens: Supporting Trust, resilience and independencePooky Knightsmith
For more information about my speaking and training work, visit: https://www.pookyknightsmith.com/speaking/
SESSION OVERVIEW:
Parenting Teens: Supporting Trust, Resilience & Independence
The teenage years bring new challenges—for teens and for you. In this practical session, we’ll explore how to support your teen through emotional ups and downs, growing independence, and the pressures of school and social life.
You’ll gain insights into the teenage brain and why boundary-pushing is part of healthy development, along with tools to keep communication open, build trust, and support emotional resilience. Expect honest ideas, relatable examples, and space to connect with other parents.
By the end of this session, you will:
• Understand how teenage brain development affects behaviour and emotions
• Learn ways to keep communication open and supportive
• Explore tools to help your teen manage stress and bounce back from setbacks
• Reflect on how to encourage independence while staying connected
• Discover simple strategies to support emotional wellbeing
• Share experiences and ideas with other parents
This document discusses pointers in C++. It defines pointers as variables that store memory addresses of other variables. It covers declaring and initializing pointers, using the address and dereference operators, pointer arithmetic, references, and passing pointers as function arguments. The document includes examples of pointer code and output to demonstrate these concepts.
The document outlines the course content for a C++ introductory course, including introductions to OOP concepts like classes and objects, pointers, functions, inheritance, and polymorphism. It also covers basic C++ programming concepts like I/O, data types, operators, and data structures. The course aims to provide students with fundamental C++ programming skills through explanations and examples of key C++ features.
Here is a C++ program that implements a Polynomial class with overloaded operators as specified in the question:
#include <iostream>
using namespace std;
class Term {
public:
int coefficient;
int exponent;
Term(int coeff, int exp) {
coefficient = coeff;
exponent = exp;
}
};
class Polynomial {
public:
Term* terms;
int numTerms;
Polynomial() {
terms = NULL;
numTerms = 0;
}
Polynomial(Term t[]) {
terms = t;
numTerms = sizeof(t)/sizeof(t[0]);
}
~Polynomial() {
delete[] terms;
}
Polynomial
Pointers,virtual functions and polymorphism cpprajshreemuthiah
This document discusses key concepts in object-oriented programming in C++ including polymorphism, pointers, pointers to objects and derived classes, virtual functions, and pure virtual functions. Polymorphism allows one name to have multiple forms through function and operator overloading as well as virtual functions. Pointers store the memory address of a variable rather than the data. Pointers can be used with objects, arrays, strings, and functions. Virtual functions allow calling a derived class version of a function through a base class pointer. Pure virtual functions define an abstract base class that cannot be instantiated.
The document discusses classes and objects in object-oriented programming. It defines a class as a blueprint for objects that bind data and functions together. A class defines data members and member functions. Objects are instances of a class that can access class data and functions. The document provides examples of defining a class called "test" with private and public members, and creating objects of the class to demonstrate accessing members.
This document provides an overview of pointers, polymorphism, inheritance, and other object-oriented programming concepts in C++. It defines pointers and describes how they store memory addresses. It explains runtime and compile-time polymorphism using method overriding and overloading. Inheritance hierarchies like single, multiple, and multilevel inheritance are covered. Virtual functions and base classes are defined as ways to implement polymorphism. Abstract classes with pure virtual functions are introduced.
1. The document discusses pointers in C++. Pointers are variables that store memory addresses and allow programs to indirectly access the value of the variable located at that address.
2. Pointers enable programs to dynamically allocate memory at runtime using operators like new and delete. They also allow accessing memory locations of variables and instructions.
3. Pointer usage can improve efficiency but must be used carefully as uninitialized or wild pointers can cause bugs and system crashes. Proper declaration and initialization of pointers using operators like * and & is important.
The document discusses C++ scope resolution operator (::) and pointers. It explains that :: is used to qualify hidden names and access variables or functions in the global namespace when a local variable hides it. It also discusses pointers, which are variables that store memory addresses. Pointers allow dynamic memory allocation and are useful for passing arguments by reference. Key pointer concepts covered include null pointers, pointer arithmetic, relationships between pointers and arrays, arrays of pointers, pointer to pointers, and passing/returning pointers in functions.
The document discusses pointers in C++. It defines pointers as variables that hold the memory addresses of other variables. It describes how to declare and initialize pointers, use pointer arithmetic and operators like & and *, pass pointers as function parameters, and dynamically allocate and free memory using pointers and operators like new and delete. Pointers allow programs to write efficiently, utilize memory properly, and dynamically allocate memory as needed.
Pointer is a variable that stores the memory address of another variable. It allows dynamic memory allocation and access of memory locations. There are three ways to pass arguments to functions in C++ - pass by value, pass by reference, and pass by pointer. Pass by value copies the value, pass by reference copies the address, and pass by pointer passes the address of the argument. Pointers can also point to arrays or strings to access elements. Arrays of pointers can store multiple strings. References are alternative names for existing variables and any changes made using the reference affect the original variable. Functions can return pointers or references.
This document provides an overview of pointers in C++. It defines pointers as variables that store the memory address of another variable. It discusses declaring and initializing pointer variables, pointer operators like & and *, pointer arithmetic, passing pointers to functions, arrays of pointers, strings of pointers, objects of pointers, and the this pointer. Advantages of pointers include efficient handling of arrays, direct memory access for speed, reduced storage space, and support for complex data structures. Limitations include slower performance than normal variables, inability to store values, needing null references, and risk of errors from incorrect initialization.
Day 1 of the training covers introductory C++ concepts like object-oriented programming, compilers, IDEs, classes, objects, and procedural programming concepts. Day 2 covers more advanced class concepts like constructors, destructors, static members, returning objects, and arrays of objects. Day 3 covers function and operator overloading.
I prepared this slides for the student of FSC BSC BS Computer Science Students. These Slides are very easy to read and understand the pointer logic used in C++ Programming.
All Topic related to pointer is discussed and examples are given
A pointer can point to an object by holding the object's address value. This is known as a "this pointer". A derived class inherits properties from its base class. A pointer to the base class can point to a derived class object, allowing access to both base and derived class members. Virtual functions ensure the correct overridden function is called at runtime based on the actual object type, rather than the pointer type. They are declared with the virtual keyword in the base class.
Pointer
Features of Pointers
Pointer Declaration
Pointer to Class
Pointer Object
The this Pointer
Pointer to Derived Classes and Base Class
Binding Polymorphisms and Virtual Functions
Introduction
Binding in C++
Virtual Functions
Rules for Virtual Function
Virtual Destructor
The document defines and provides examples of polymorphism in object-oriented programming. It discusses two types of polymorphism: static and dynamic. Static polymorphism is resolved at compile-time through function overloading and operator overloading, while dynamic polymorphism uses virtual functions and is resolved at run-time. Virtual functions, pure virtual functions, and abstract classes are also explained as key aspects of implementing dynamic polymorphism.
Parenting Teens: Supporting Trust, resilience and independencePooky Knightsmith
For more information about my speaking and training work, visit: https://www.pookyknightsmith.com/speaking/
SESSION OVERVIEW:
Parenting Teens: Supporting Trust, Resilience & Independence
The teenage years bring new challenges—for teens and for you. In this practical session, we’ll explore how to support your teen through emotional ups and downs, growing independence, and the pressures of school and social life.
You’ll gain insights into the teenage brain and why boundary-pushing is part of healthy development, along with tools to keep communication open, build trust, and support emotional resilience. Expect honest ideas, relatable examples, and space to connect with other parents.
By the end of this session, you will:
• Understand how teenage brain development affects behaviour and emotions
• Learn ways to keep communication open and supportive
• Explore tools to help your teen manage stress and bounce back from setbacks
• Reflect on how to encourage independence while staying connected
• Discover simple strategies to support emotional wellbeing
• Share experiences and ideas with other parents
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxArshad Shaikh
Diptera, commonly known as flies, is a large and diverse order of insects that includes mosquitoes, midges, gnats, and horseflies. Characterized by a single pair of wings (hindwings are modified into balancing organs called halteres), Diptera are found in almost every environment and play important roles in ecosystems as pollinators, decomposers, and food sources. Some species, however, are significant pests and disease vectors, transmitting diseases like malaria, dengue, and Zika virus.
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...EduSkills OECD
Deborah Nusche, Senior Analyst, OECD presents at the OECD webinar 'Trends Spotting: Strategic foresight for tomorrow’s education systems' on 5 June 2025. You can check out the webinar on the website https://oecdedutoday.com/webinars/ Other speakers included: Deborah Nusche, Senior Analyst, OECD
Sophie Howe, Future Governance Adviser at the School of International Futures, first Future Generations Commissioner for Wales (2016-2023)
Davina Marie, Interdisciplinary Lead, Queens College London
Thomas Jørgensen, Director for Policy Coordination and Foresight at European University Association
This presentation was provided by Nicole 'Nici" Pfeiffer of the Center for Open Science (COS), during the first session of our 2025 NISO training series "Secrets to Changing Behavior in Scholarly Communications." Session One was held June 5, 2025.
How to Create an Event in Odoo 18 - Odoo 18 SlidesCeline George
Creating an event in Odoo 18 is a straightforward process that allows you to manage various aspects of your event efficiently.
Odoo 18 Events Module is a powerful tool for organizing and managing events of all sizes, from conferences and workshops to webinars and meetups.
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...parmarjuli1412
The document provides an overview of therapeutic communication, emphasizing its importance in nursing to address patient needs and establish effective relationships. THERAPEUTIC COMMUNICATION included some topics like introduction of COMMUNICATION, definition, types, process of communication, definition therapeutic communication, goal, techniques of therapeutic communication, non-therapeutic communication, few ways to improved therapeutic communication, characteristics of therapeutic communication, barrier of THERAPEUTIC RELATIONSHIP, introduction of interpersonal relationship, types of IPR, elements/ dynamics of IPR, introduction of therapeutic nurse patient relationship, definition, purpose, elements/characteristics , and phases of therapeutic communication, definition of Johari window, uses, what actually model represent and its areas, THERAPEUTIC IMPASSES and its management in 5th semester Bsc. nursing and 2nd GNM students
How to Manage & Create a New Department in Odoo 18 EmployeeCeline George
In Odoo 18's Employee module, organizing your workforce into departments enhances management and reporting efficiency. Departments are a crucial organizational unit within the Employee module.
Adam Grant: Transforming Work Culture Through Organizational PsychologyPrachi Shah
This presentation explores the groundbreaking work of Adam Grant, renowned organizational psychologist and bestselling author. It highlights his key theories on giving, motivation, leadership, and workplace dynamics that have revolutionized how organizations think about productivity, collaboration, and employee well-being. Ideal for students, HR professionals, and leadership enthusiasts, this deck includes insights from his major works like Give and Take, Originals, and Think Again, along with interactive elements for enhanced engagement.
This presentation has been made keeping in mind the students of undergraduate and postgraduate level. To keep the facts in a natural form and to display the material in more detail, the help of various books, websites and online medium has been taken. Whatever medium the material or facts have been taken from, an attempt has been made by the presenter to give their reference at the end.
In the seventh century, the rule of Sindh state was in the hands of Rai dynasty. We know the names of five kings of this dynasty- Rai Divji, Rai Singhras, Rai Sahasi, Rai Sihras II and Rai Sahasi II. During the time of Rai Sihras II, Nimruz of Persia attacked Sindh and killed him. After the return of the Persians, Rai Sahasi II became the king. After killing him, one of his Brahmin ministers named Chach took over the throne. He married the widow of Rai Sahasi and became the ruler of entire Sindh by suppressing the rebellions of the governors.
Rose Cultivation Practices by Kushal Lamichhane.pdfkushallamichhame
This includes the overall cultivation practices of Rose prepared by:
Kushal Lamichhane (AKL)
Instructor
Shree Gandhi Adarsha Secondary School
Kageshowri Manohara-09, Kathmandu, Nepal
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
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 ;
}
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.
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 ;
}
};
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 ;
}
};
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.
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.
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 .
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.
}
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.