0% found this document useful (0 votes)
18 views

Pure Virtual Functions and Abstract Classes in C++: Then Derived Class Also Becomes Abstract Class

An abstract class is a class that contains at least one pure virtual function, which is a virtual function declared without an implementation. The Animal class is abstract because it declares the move() function as pure virtual, since the implementation depends on the specific animal. Pure virtual functions ensure that derived classes provide an implementation, allowing an abstract class to define an interface for other classes to implement without defining the details itself.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Pure Virtual Functions and Abstract Classes in C++: Then Derived Class Also Becomes Abstract Class

An abstract class is a class that contains at least one pure virtual function, which is a virtual function declared without an implementation. The Animal class is abstract because it declares the move() function as pure virtual, since the implementation depends on the specific animal. Pure virtual functions ensure that derived classes provide an implementation, allowing an abstract class to define an interface for other classes to implement without defining the details itself.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Pure Virtual Functions and Abstract Classes in

C++
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.

Animal class doesn’t have implementation of move() (assuming that all


animals move), but all animals must know how to move. We cannot create
objects of abstract classes.

A pure virtual function (or abstract function) in C++ is a virtual function for
which we don’t have implementation, we only declare it.

A pure virtual function is declared by assigning 0 in declaration.

A pure virtual function is implemented by classes which are derived from a


Abstract class.

1) A class is abstract if it has at least one pure virtual function.


2) We can have pointers and references of abstract class type.
3) If we do not override the pure virtual function in derived class,
then derived class also becomes abstract class.
4) An abstract class can have constructors.
Scanned by CamScanner
Scanned by CamScanner

You might also like