Topic 3 Sadia Cyber
Topic 3 Sadia Cyber
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?
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
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
FATIMA HABIB
DEFINATION
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;
};
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.
OUTPUT
Class A
EXAMPLE #2
(Constructor Order in Virtual
Inheritance)
OUTPUT
Constructor A
Constructor B
Constructor C
Constructor D
DIFFERENCE B/W ALL
T H A N K Y O U !