Abstract Base Class & Pure Virtual Functions in C++ - C++ Tutorials For Beginners #58 - CodeWithHarry
Abstract Base Class & Pure Virtual Functions in C++ - C++ Tutorials For Beginners #58 - CodeWithHarry
Abstract Base Class & Pure Virtual Functions in C++ | C++ Tutorials for
Beginners #58
In this tutorial, we will discuss abstract base class and pure virtual functions in C++
To demonstrate the concept of abstract class and pure virtual function an example program is shown below.
class CWH{
protected:
string title;
float rating;
public:
title = s;
rating = r;
};
1. We created a class “CHW” which contains protected data members “title” which has “string” data type and “rating”
which has “float” data type.
2. The class “CWH” has a parameterized constructor which takes two parameters “s” and “r” and assign their values
to the data members “title” and “rating”
3. The class “CHW” has a pure virtual function void “display” which is declared by 0. The main thing to note here is
that as the “display” function is a pure virtual function it is compulsory to redefine it in the derived classes.
float videoLength;
public:
videoLength = vl;
void display(){
};
1. We created a class “CHWVideo” which is inheriting “CWH” class and contains private data members “videoLength”
which has “float” data type.
https://www.codewithharry.com/videos/cpp-tutorials-in-hindi-58 1/3
8/16/2021 Abstract Base Class & Pure Virtual Functions in C++ | C++ Tutorials for Beginners #58 - CodeWithHarry
2. The class “CWHVideo” has a parameterized constructor which takes three parameters “s”, “r” and “vl”. The
constructor of the base class is called in the derived class and the values of the variables “s” and “r” are passed to
it. The value of the parameter “vl” will be assigned to the data members “videoLength”
3. The class “CHWVideo” has a function void “display” which will print the values of the data members “title”, “rating”
and “videoLength”
int words;
public:
words = wc;
void display(){
};
1. We created a class “CHWText” which is inheriting “CWH” class and contains private data members “words” which
has “int” data type.
2. The class “CWHText” has a parameterized constructor which takes three parameters “s”, “r” and “wc”. The
constructor of the base class is called in the derived class and the values of the variables “s” and “r” are passed to
it. The value of the parameter “wc” will be assigned to the data members “words”
3. The class “CHWText” has a function void “display” which will print the values of the data members “title”, “rating”
and “words”
int main(){
string title;
int words;
vlen = 4.56;
rating = 4.89;
words = 433;
rating = 4.19;
CWH* tuts[2];
tuts[0] = &djVideo;
tuts[1] = &djText;
tuts[0]->display();
tuts[1]->display();
return 0;
1. We created a string variable “title”, float variables “rating”, “vlen” and integer variable “words”
https://www.codewithharry.com/videos/cpp-tutorials-in-hindi-58 2/3
8/16/2021 Abstract Base Class & Pure Virtual Functions in C++ | C++ Tutorials for Beginners #58 - CodeWithHarry
2. For the code with harry video class we have assigned “Django tutorial” to the string “title”, “4.56” to the float “vlen”
and “4.89” to the float “rating”.
3. An object “djVideo” is created of the data type “CWHVideo” and the variables “title”, “rating” and “vlen” are passed
to it.
4. For the code with harry text class we have assigned “Django tutorial text” to the string “title”, “433” to the integer
“words” and “4.19” to the float “rating”.
5. An object “djText” is created of the data type “CWHText” and the variables “title”, “rating” and “words” are passed to
it.
6. Two pointers array “tuts” is created of the “CWH” type
7. The address of the “djVideo” is assigned to “tuts[0]” and the address of the “djText” is assigned to “tuts[1]”
8. The function “display” is called using pointers “tuts[0]” and “tuts[1]”
The main thing to note here is that if we don’t override the pure virtual function in the derived class the compiler will
throw an error as shown in figure 1.
← Previous Next →
https://www.codewithharry.com/videos/cpp-tutorials-in-hindi-58 3/3