Skip to player
Skip to main content
Search
Connect
Watch fullscreen
Like
Bookmark
Share
More
Add to Playlist
Report
Abstract Classes in C++
SoftPrayog
Follow
5 months ago
Abstract classes contain at least one pure virtual function and cannot be instantiated. Other classes can be derived from abstract classes. https://www.softprayog.in/programming/abstract-classes-in-cpp
Category
đŸ¤–
Tech
Transcript
Display full video transcript
00:00
Hello and welcome to this video. I am Karnesh Jhuri and this video is about abstract classes
00:07
in C++. Abstract classes have at least one pure virtual function, so they cannot be instantiated.
00:16
We cannot create objects of an abstract class. Abstract classes are useful in defining interfaces.
00:24
Interface classes are abstract classes with only pure virtual functions. Abstract classes
00:32
are often base classes of class hierarchies. So, let's look at abstract classes in a little detail.
00:41
Abstract classes have at least one pure virtual function. For example, Vehicle is a class with a
00:49
constructor and member function print basic vehicle details, which print the vehicle name
00:58
and its average city speed. Now, car is a type of vehicle. So, we can say class car is derived from
01:05
class vehicle and class vehicle has a pure virtual function which is virtual void print auxiliary
01:14
vehicle details. Constant function and the notation equal to zero specifies that this is a pure virtual
01:23
function. Any subclass derived from vehicle must define this function. Let's look at the main function.
01:30
We cannot create objects of class vehicle because it has a pure virtual function. It is an abstract class.
01:38
Another important point. We are storing pointers to object car in variables v1, v2 and v3 which are pointers
01:48
to class vehicle. Now, we could have had more subclasses like bike, van, truck and all pointers to these
01:57
subclasses could have been stored in variables of type pointers to class vehicle. So, we have a list of
02:04
pointers to class vehicle where we can store pointers to all the derived classes and we can scan this list
02:12
and execute the virtual function print auxiliary vehicle details. The correct function as per the
02:19
subclass of pointer is picked up and executed. This is due to the property of polymorphism. Whenever we have
02:27
a virtual function in a class, we have polymorphism where we can refer to a subclass using a
02:34
pointer of type base class. If we look at the class vehicle, it has a mix of members. It has data members,
02:43
a public member function and a pure virtual function. But suppose we have a class with just pure virtual
02:50
functions like this. Here the subscript operator overload function and the size function are both pure virtual
02:59
functions as indicated by the equal to notation. There are no data members. It is an interface class.
03:08
It specifies the container interface. A class derived from an abstract class container must implement
03:16
the subscript operator overload function and the size function. Now, we don't have data members in the
03:23
abstract class container but we can create objects of classes derived from the abstract class container
03:29
and refer to these objects using pointers or references to the base class. So, it is important to have a
03:37
virtual destructor in the base class container. The destructor in the base class is linked to the destructor in the
03:45
derived classes. So, when an object of a derived class is deleted using a pointer to the base class,
03:53
the deletion is completely done due to the linkage of destructors. First, the object of the derived class
04:00
is deleted and then the pointer to the base class and there is no memory leakage. Let's look at the
04:08
program. We have the abstract class container and then we have the vector container which is derived from
04:15
the abstract class container. Vector container contains a vector of doubles. Since vector container is
04:22
derived from the abstract class container, it must implement the subscript overload function and the size
04:29
function which it does. It overrides the pure virtual functions of class container and now vector
04:38
container is a concrete class. We can instantiate objects of it. Similarly, we have a list container containing
04:47
a list of doubles and it also implements and overrides the pure virtual functions that is the subscript
04:56
operator overload function and the size function of the base class container. Now, coming to the main
05:03
function function, we create a vector container object with a vector of 10 doubles and we change values at
05:11
indexes 0, 5 and 9 to 56 and we pass VC to the use container function. The use container function
05:20
expects a reference to the base class container but it can also work with the reference to any subclass
05:27
derived from the container class. The use container function prints the elements of the past container
05:34
class. We do the same for an object of class list container which is also derived from the abstract
05:42
class container. The point is that the use container function knows only about the base class container.
05:50
It gets a reference to the base class container but we are passing references to objects of
05:55
derived classes and the use container works fine. Abstract class container is a polymorphic type.
06:03
For a polymorphic type, we can pass a derived class when the base class is expected. In the use container
06:11
function, there are calls c dot size and c subscript operator index where c is the base class container.
06:20
How is the correct size or subscript overload function chosen?
06:25
The answer is that for each derived class, there is a virtual function table or vtable and each object
06:32
has a pointer to the virtual function table for the derived class the object belongs to. So, pointer plus an
06:40
index into the table are generated for each function call which are used for executing the write function
06:48
function at runtime. So, for an object we see it is a pointer to the vtable of the derived class vector
06:55
container and vtable for vector container has three elements each a pointer to the implementation of an
07:03
overridden virtual function. There is a similar vtable for the list container derived class. We have seen
07:12
how an interface class container comprising of pure virtual functions can be used as the base class
07:20
for defining a vector container and a list container. You can find all this information
07:26
at softpriok website. Here is the QR code and the link is there in the description.
07:33
Please subscribe to softpriok channel. Click on the bell icon and enable notifications.
07:41
Thanks very much for watching. Take care and stay safe.
07:53
You
07:55
You
07:57
You
07:59
You
08:01
You
08:03
You
08:05
You
08:07
You
08:09
You
Be the first to comment
Add your comment
Recommended
7:11
|
Up next
Inheritance in C++
SoftPrayog
5 months ago
8:21
Concrete Classes in C++
SoftPrayog
6 months ago
18:12
Templates-in C++
SoftPrayog
5 weeks ago
12:57
Copy and move in C++
SoftPrayog
4 months ago
30:31
Data Types and Declarations in C++
SoftPrayog
9 months ago
0:29
Painted Storks
SoftPrayog
1 year ago
9:41
How to Backup a WordPress Site using the Command Line
SoftPrayog
1 year ago
10:47
How to take backups in Linux using the Command Line
SoftPrayog
1 year ago
0:59
VIM editor - copy, paste and delete operations
SoftPrayog
1 year ago
20:44
vi text editor in Linux
SoftPrayog
1 year ago
32:53
Socket programming in C
SoftPrayog
1 year ago
21:00
Signals in Linux
SoftPrayog
1 year ago
15:22
sed command in Linux
SoftPrayog
1 year ago
9:22
rsync file copy command
SoftPrayog
1 year ago
17:32
What is an Operating System?
SoftPrayog
1 year ago
15:40
ip command in Linux with examples
SoftPrayog
1 year ago
7:48
htop command in Linux
SoftPrayog
1 year ago
14:53
top command in Linux
SoftPrayog
1 year ago
14:27
grep command in Linux
SoftPrayog
1 year ago
30:34
Git Source Code Management Tutorial
SoftPrayog
1 year ago
10:50
diff, patch and vimdiff commands in Linux
SoftPrayog
1 year ago
31:06
Awk command in Linux
SoftPrayog
1 year ago
35:38
Time in Linux
SoftPrayog
1 year ago
13:52
Pthreads in C under Linux
SoftPrayog
1 year ago
0:51
Former Aide Claims She Was Asked to Make a ‘Hit List’ For Trump
Veuer
2 years ago
Be the first to comment