Skip to playerSkip to main content
  • 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
Transcript
00:00Hello and welcome to this video. I am Karnesh Jhuri and this video is about abstract classes
00:07in C++. Abstract classes have at least one pure virtual function, so they cannot be instantiated.
00:16We cannot create objects of an abstract class. Abstract classes are useful in defining interfaces.
00:24Interface classes are abstract classes with only pure virtual functions. Abstract classes
00:32are often base classes of class hierarchies. So, let's look at abstract classes in a little detail.
00:41Abstract classes have at least one pure virtual function. For example, Vehicle is a class with a
00:49constructor and member function print basic vehicle details, which print the vehicle name
00:58and its average city speed. Now, car is a type of vehicle. So, we can say class car is derived from
01:05class vehicle and class vehicle has a pure virtual function which is virtual void print auxiliary
01:14vehicle details. Constant function and the notation equal to zero specifies that this is a pure virtual
01:23function. Any subclass derived from vehicle must define this function. Let's look at the main function.
01:30We cannot create objects of class vehicle because it has a pure virtual function. It is an abstract class.
01:38Another important point. We are storing pointers to object car in variables v1, v2 and v3 which are pointers
01:48to class vehicle. Now, we could have had more subclasses like bike, van, truck and all pointers to these
01:57subclasses could have been stored in variables of type pointers to class vehicle. So, we have a list of
02:04pointers to class vehicle where we can store pointers to all the derived classes and we can scan this list
02:12and execute the virtual function print auxiliary vehicle details. The correct function as per the
02:19subclass of pointer is picked up and executed. This is due to the property of polymorphism. Whenever we have
02:27a virtual function in a class, we have polymorphism where we can refer to a subclass using a
02:34pointer of type base class. If we look at the class vehicle, it has a mix of members. It has data members,
02:43a public member function and a pure virtual function. But suppose we have a class with just pure virtual
02:50functions like this. Here the subscript operator overload function and the size function are both pure virtual
02:59functions as indicated by the equal to notation. There are no data members. It is an interface class.
03:08It specifies the container interface. A class derived from an abstract class container must implement
03:16the subscript operator overload function and the size function. Now, we don't have data members in the
03:23abstract class container but we can create objects of classes derived from the abstract class container
03:29and refer to these objects using pointers or references to the base class. So, it is important to have a
03:37virtual destructor in the base class container. The destructor in the base class is linked to the destructor in the
03:45derived classes. So, when an object of a derived class is deleted using a pointer to the base class,
03:53the deletion is completely done due to the linkage of destructors. First, the object of the derived class
04:00is deleted and then the pointer to the base class and there is no memory leakage. Let's look at the
04:08program. We have the abstract class container and then we have the vector container which is derived from
04:15the abstract class container. Vector container contains a vector of doubles. Since vector container is
04:22derived from the abstract class container, it must implement the subscript overload function and the size
04:29function which it does. It overrides the pure virtual functions of class container and now vector
04:38container is a concrete class. We can instantiate objects of it. Similarly, we have a list container containing
04:47a list of doubles and it also implements and overrides the pure virtual functions that is the subscript
04:56operator overload function and the size function of the base class container. Now, coming to the main
05:03function function, we create a vector container object with a vector of 10 doubles and we change values at
05:11indexes 0, 5 and 9 to 56 and we pass VC to the use container function. The use container function
05:20expects a reference to the base class container but it can also work with the reference to any subclass
05:27derived from the container class. The use container function prints the elements of the past container
05:34class. We do the same for an object of class list container which is also derived from the abstract
05:42class container. The point is that the use container function knows only about the base class container.
05:50It gets a reference to the base class container but we are passing references to objects of
05:55derived classes and the use container works fine. Abstract class container is a polymorphic type.
06:03For a polymorphic type, we can pass a derived class when the base class is expected. In the use container
06:11function, there are calls c dot size and c subscript operator index where c is the base class container.
06:20How is the correct size or subscript overload function chosen?
06:25The answer is that for each derived class, there is a virtual function table or vtable and each object
06:32has a pointer to the virtual function table for the derived class the object belongs to. So, pointer plus an
06:40index into the table are generated for each function call which are used for executing the write function
06:48function at runtime. So, for an object we see it is a pointer to the vtable of the derived class vector
06:55container and vtable for vector container has three elements each a pointer to the implementation of an
07:03overridden virtual function. There is a similar vtable for the list container derived class. We have seen
07:12how an interface class container comprising of pure virtual functions can be used as the base class
07:20for defining a vector container and a list container. You can find all this information
07:26at softpriok website. Here is the QR code and the link is there in the description.
07:33Please subscribe to softpriok channel. Click on the bell icon and enable notifications.
07:41Thanks very much for watching. Take care and stay safe.
07:53You
07:55You
07:57You
07:59You
08:01You
08:03You
08:05You
08:07You
08:09You
Be the first to comment
Add your comment