0% found this document useful (0 votes)
10 views13 pages

Virtual Functions Data

Uploaded by

maanviverma516
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)
10 views13 pages

Virtual Functions Data

Uploaded by

maanviverma516
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/ 13

Pointer to derived class

• In C++, we can declare a pointer that points to the


base class as well as derive class.
• One of the key features of class inheritance is that a
pointer to a derived class is type-compatible with a
pointer to its base class.
Output
Pointer to derived class
Output
Virtual Functions
Virtual Functions
Rules of Virtual Functions
Pure Virtual Functions
• Pure virtual functions are used:
• if a function doesn't have any use in the base class
• but the function must be implemented by all its derived
classes
• A virtual function is not used for performing any task. It only
serves as a placeholder.
• When the function has no definition, such function is known
as "do-nothing" function.
• The "do-nothing" function is known as a pure virtual
function. A pure virtual function is a function declared in
the base class that has no definition relative to the base
class.
Pure Virtual Functions
Abstract Class
Sometimes implementation of all function cannot be provided in a base class because we don’t know
the implementation. Such a class is called abstract class. For example, let Shape be a base class.
We cannot provide implementation of function draw() in Shape, but we know every derived class
must have implementation of draw(). Similarly an Animal class doesn’t have implementation of
move() (assuming that all animals move), but all animals must know how to move.

• A class that contains atleast one pure virtual function is known as an abstract class.
• We cannot create objects of an abstract class.
• However, we can derive classes from them, and use their data members and member
functions (except pure virtual functions).
Abstract Class and Pure Virtual Functions
Abstract Class and Pure Virtual Functions

You might also like