0% found this document useful (0 votes)
3 views25 pages

Topic 3 Sadia Cyber

The presentation covers key concepts in C++ related to virtual functions, pure virtual functions, abstract classes, and virtual base classes. It explains the importance of virtual functions for achieving runtime polymorphism and provides examples demonstrating these concepts. Additionally, it addresses the use of pure virtual functions to create abstract classes and the implications of virtual inheritance in preventing ambiguity in multiple inheritance scenarios.

Uploaded by

abishahamid2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

Topic 3 Sadia Cyber

The presentation covers key concepts in C++ related to virtual functions, pure virtual functions, abstract classes, and virtual base classes. It explains the importance of virtual functions for achieving runtime polymorphism and provides examples demonstrating these concepts. Additionally, it addresses the use of pure virtual functions to create abstract classes and the implications of virtual inheritance in preventing ambiguity in multiple inheritance scenarios.

Uploaded by

abishahamid2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

WELCOME

TO OUR PRESENTATION

~ BY IMAN FATIMA
OUR AGENDA

VIRTUAL FUNCTIONS
PURE VIRTUAL FUNCTIONS
OBJECTIVE
ABSTRACT CLASS
VIRTUAL BASE CLASS
VIRTUAL FUNCTIONS

IMAN FATIMA
LITTLE BIT ABOUT POLYMORPHISM
WHY WE USE VIRTUAL FUNCTIONS?

We use virtual functions when we want runtime


polymorphism, which means:

The program decides at runtime which version of the


function to call — based on the actual object, not just the
pointer type.
.
EXAMPLE
suppose you’re using a base class pointer (Base* ptr), but that pointer is pointing to
a derived class object.

Without virtual:
➡️ It will always call the base class version of the function.

With virtual:
It will call the derived class version — the correct one!
DEFINATION
DEFINATION

A virtual function is a member function


An abstract class is a special type of
that is
class thatdeclared
cannot using theown
make its virtual
object,
keyword
and in the base
it is meant to beclass and can
inherited be
by other
classes.
overridden by the derived class.

It contains at least one pure virtual


SYNTAX
function, which means:
“This function
class Base { has no body here. The
child
public:
class must write its own version.”
virtual void functionName() {
// base class code
}
};
EXAMPLE # 1
Write a C++ program to demonstrate
runtime polymorphism using virtual
functions. Create a base class with a
virtual display() function and a derived
class that overrides this function. In the
main() function, use a base class
pointer to refer to a derived class object
and call the display() function to show
how dynamic binding works. Also
display both base and derived class
variables in the derived class’s
display() function.
EXAMPLE # 2
Write a C++ program to demonstrate
runtime polymorphism using a
virtual function. Create a base class
Animal with a virtual function
makeSound() and a derived class
Dog that overrides this function. In
the main() function, use a base class
pointer to refer to a derived class
object and call the makeSound()
function to show how dynamic
binding works.
PURE VIRTUAL FUNCTIONS

MARYAM BIBI
WHAT IS A PURE VIRTUAL
FUNCTION ?
A virtual function that has no body in the base class, is known as pure virtual
function.

SYNTAX
virtual return_type function_name(arguments)=0;
KEY FEATURES OF A PURE VIRTUAL FUNCTION
⁠Declared using = 0 Makes a class abstract ⁠Must be overridden
It has no implementation in Any class containing at
All derived (child) classes
the base class. least one pure virtual
must override the pure
function becomes an
Example: virtual function, otherwise
abstract class, and you
virtual void show() = 0; they also become abstract.
cannot create objects of
that class
#include <iostream>
using namespace std;

class Shape {
public: EXAMPLE
virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape { Write a C++ program to demonstrate the use of pure virtual
public: functions. Create an abstract base class Shape with a pure virtual
void draw() { function draw(), and a derived class Circle that overrides this
cout << "Drawing a circle!" << endl; function. In the main() function, use a base class pointer to point
} to a derived class object and call the draw() function to show how
}; abstraction and runtime polymorphism are used.

int main() {
Shape* shape_ptr; // Base class pointer
Circle obj_circle; // Derived class object

shape_ptr = &obj_circle; // Base class pointer points to derived


class object
OUTPUT
shape_ptr->draw(); // Calls derived class function Drawing a circle!
return 0;
}
ABSTRACT CLASS

FATIMA HABIB
DEFINATION

An abstract class is a special type of


class that cannot make its own object,
and it is meant to be inherited by other
classes.
It contains at least one pure virtual
function, which means:
“This function has no body here. The
child class must write its own version.”
SOME IMPORTANT POINTS:
01 02 03
“We use abstract classes “If a class has at least one
when we want to make function like this: virtual We cannot make
a general structure, but void fun() = 0;, then it
objects of an abstract
the exact behavior becomes abstract.”
class.”
should be decided by Focus on = 0 and virtual —
child classes.” both are important

04 05
"In a C++ abstract class, you
“Child classes must
can have both methods and
override the pure virtual variables.
functions, otherwise they But only methods can be made
will also become abstract
abstract.” You can't make variables
abstract in C++"
EXAMPLE # 1 #include <iostream>
using namespace std;

class Animal
{
public:
virtual void sound() = 0; // Virtual Pure Function.
Write a C++ program to demonstrate the use of pure };
virtual functions. Create an abstract base class Animal
with a pure virtual function sound(), and a derived class class Cat : public Animal
Cat that overrides this function. In the main() function, {
create an object of the derived class and call the sound() public:
function to show how abstraction works. void sound()
{
cout << "Meow, meow!" << endl;
}
};

int main()
{
OUTPUT Cat MyObj;
MyObj.sound();
Meow, meow! return 0;
}
#include <iostream>
using namespace std;

class Motivation

EXAMPLE # 2
{
public:
virtual void quote() = 0;
};

class Quote1 : public Motivation


{ Write a C++ program to demonstrate pure virtual
public:
void quote() functions with multiple derived classes. Create an
{
cout << "If you can dream it, you can do it." << endl; abstract base class Motivation with a pure virtual
};
}
function quote(). Derive two classes Quote1 and
class Quote2 : public Motivation
Quote2 that override this function to print different
{ motivational quotes. In the main() function, create
public:
void quote() objects of both derived classes and call the quote()
{
cout << "The future belongs to those who believe in the beauty of their dreams." << endl; function to display their respective outputs.
}
};

int main()
{ OUTPUT
Quote1 MyObj1; If you can dream it, you can do it.
Quote2 MyObj2;
The future belongs to those who believe in the
MyObj1.quote();
MyObj2.quote(); beauty of their dreams.
return 0;
}
EXAMPLE # 3 SOLUTION
#include <iostream>
using namespace std;

class Parent
{
Write a C++ program to demonstrate the public:
virtual int square() = 0;
concept of pure virtual functions. Create an };
abstract base class Parent with a pure virtual
class Child : public Parent
function square(), and a derived class Child that {
overrides this function to return the square of a public:
int square()
number. In the main() function, create an object {
of the derived class and call the square() int a = 4;
return a * a;
function. }
};

int main()
OUTPUT {
Child MyObj;
16 cout << MyObj.square();
return 0;
}
}
VIRTUAL BASE CLASS

SADIA KAUSAR
DEFINATION
DEFINATION

An abstract
A virtual class is a special
base class in C++type
is aof
class that cannot make its own object,
andconcept
it is meantused in
to be inheritedmultiple
by other
inheritance to prevent duplication
classes.
of a base class when it's inherited
It contains at least one pure virtual
multiple
function, times
which through different
means:
“This function
paths in an has no body here. The
inheritanceConclusion
child class must write its own version.”
B and C both inherit from A.

D inherits from both B and C.

Now, without virtual inheritance:

D ends up with two copies of class A.

This leads to ambiguity when


accessing members of A from D.
EXAMPLE # 1
(With Virtual Base Class)

Write a C++ program to demonstrate how virtual


inheritance solves the ambiguity caused by the
diamond problem. Create a base class A with a
function show(). Derive two classes B and C from
A using virtual inheritance, and then derive class
D from both B and C. Create an object of class D
in the main() function and call the show() function
without ambiguity.

OUTPUT
Class A
EXAMPLE #2
(Constructor Order in Virtual
Inheritance)

Write a C++ program to demonstrate the


concept of virtual inheritance and constructor
execution order in a diamond-shaped
inheritance structure. Create a base class A
with a constructor. Derive two classes B and C
virtually from A, and a fourth class D from
both B and C. Observe and explain the order in
which constructors are called when an object
of class D is created.

OUTPUT
Constructor A
Constructor B
Constructor C
Constructor D
DIFFERENCE B/W ALL
T H A N K Y O U !

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy