Skip to playerSkip to main content
  • 6 weeks ago
Inheritance is a core concept in object oriented programming. C++ provides extensive facilities for inheritance which is explained. https://www.softprayog.in/programming/inheritance-in-cpp
Transcript
00:00Hello and welcome to this video. I am Karanis Jyoti and this video is about
00:05inheritance in C++. Inheritance is a core concept in object-oriented
00:12programming. Classes inherit properties and behaviors from the base classes they
00:19are derived from. In this video, we'll look at how inheritance works in C++.
00:27We'll also look at example programs that illustrate inheritance. So let's get
00:34started with inheritance in C++. Suppose we have a class vehicle. It is a
00:42constructor vehicle with parameters name and speed and there are two member
00:48functions print name and print average city speed. Vehicle has got two private
00:54data members, name which is a vehicle name and average city speed, the speed at
01:01which you can expect to drive in the city. Vehicle is a general concept. There are so
01:07many types of vehicles, scooters, bikes, cars, vans, trucks and so on. So we can have a
01:14specialized class for car and we can say class car is derived from class vehicle.
01:20We have a constructor for car with parameters name, speed, transmission, fuel type. The
01:29constructor for car calls the constructor for base class vehicle with parameters name and speed.
01:37Then it has got two private data members transmission and fuel. So the constructor
01:44initializes transmission and fuel and the two member functions transmission type and fuel type print transmission
01:54and fuel and then in the main function we create an object of class car my car with values car that
02:05is a name average city speed as 40 transmission is auto and fuel type as electric and we call
02:13the member functions. The first one my car dot print name prints the name of the vehicle.
02:19Now we have created an object of type car which inherits the member function from the base
02:28class vehicle and it prints the name of the vehicle. Similarly my car print average city speed prints the average
02:38city speed. This too is defined in vehicle it is inherited from vehicle and then my car transmission type
02:46and my car fuel type these are defined in class car and they are printed. So what we are seeing here is that
02:56class car is derived from class vehicle. Now both these classes vehicle and car they are concrete classes.
03:06So what we are having here is a concrete class derived from another concrete class and this is useful
03:13because we are able to use the concept of vehicle and we are able to use the code for the class vehicle
03:20and we don't have to write the code again. So it's kind of a incremental development where the class vehicle is thought first is
03:30conceptualized first and it is coded and then we are having the class car which is derived from class vehicle.
03:39Now we look at virtual functions which is an important concept in inheritance. A virtual function starts with the
03:46keyword virtual for example virtual void print vehicle details. Virtual function means that this is the default
03:56implementation of a function in the base class. This can be overridden in the derived class with an
04:04alternate implementation. Then there are pure virtual functions. Pure virtual functions are a special type of
04:10virtual functions. They are not implemented in the base class. Instead they are declared with a notation equal to zero.
04:19For example virtual int size constant equal to zero. They must be implemented in a derived class so that
04:29that derived class can become a concrete class and can instantiate objects. Back to our example we can see
04:37functions in the two classes vehicle based class and the car derived class. The functions print data about the
04:45object vehicle or object car. We can definitely combine the print name and print average speed
04:53functions in the class vehicle into a single function print vehicle details and we can make it a virtual
05:01function so that it can be overridden by a more specialized function in the class car.
05:07Print vehicle details in the class car calls print vehicle details in class vehicle and then it prints
05:15transmission and fuel. Cut to the main function. We make objects of class car, vehicle and again car
05:23and these are pointed by pointers v1, v2 and v3 respectively. v1, v2 and v3 are pointers to objects of
05:32class vehicle. But v1 stores pointers to class car and v3 also stores pointers to class car. So we can
05:40see that a pointer to a base class can also store pointers to objects of derived classes.
05:48And again in the for loop using a pointer to the base class vehicle we call virtual function print vehicle
05:56details. And in case of objects of class car print vehicle detail function of class car is called
06:04which prints all the attributes those stored in the base class vehicle and also those stored in the
06:10derived class car. If we had a few more derived classes like bike, van and truck each having its own
06:18implementation of print vehicle detail the correct function would have been called depending upon the
06:24subclass. Even though the pointer is of the base class vehicle we can say that the class vehicle is a
06:30polymorphic type and the objects pointed by pointer to class vehicle behave differently depending upon the
06:38subclass of the object. This concept is called polymorphism. We have seen inheritance concepts in C++ and also
06:49example programs. You can find all this information at the soft priyok website. Here's the qr code and link is there in the
06:58description. Please subscribe to soft priyok channel, click on the bell icon and enable notifications. Thanks very much for
07:07watching. Take care and stay safe.
Be the first to comment
Add your comment