Skip to player
Skip to main content
Search
Connect
Watch fullscreen
1
Bookmark
Share
More
Add to Playlist
Report
Inheritance in C++
SoftPrayog
Follow
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
Category
🤖
Tech
Transcript
Display full video transcript
00:00
Hello and welcome to this video. I am Karanis Jyoti and this video is about
00:05
inheritance in C++. Inheritance is a core concept in object-oriented
00:12
programming. Classes inherit properties and behaviors from the base classes they
00:19
are derived from. In this video, we'll look at how inheritance works in C++.
00:27
We'll also look at example programs that illustrate inheritance. So let's get
00:34
started with inheritance in C++. Suppose we have a class vehicle. It is a
00:42
constructor vehicle with parameters name and speed and there are two member
00:48
functions print name and print average city speed. Vehicle has got two private
00:54
data members, name which is a vehicle name and average city speed, the speed at
01:01
which you can expect to drive in the city. Vehicle is a general concept. There are so
01:07
many types of vehicles, scooters, bikes, cars, vans, trucks and so on. So we can have a
01:14
specialized class for car and we can say class car is derived from class vehicle.
01:20
We have a constructor for car with parameters name, speed, transmission, fuel type. The
01:29
constructor for car calls the constructor for base class vehicle with parameters name and speed.
01:37
Then it has got two private data members transmission and fuel. So the constructor
01:44
initializes transmission and fuel and the two member functions transmission type and fuel type print transmission
01:54
and fuel and then in the main function we create an object of class car my car with values car that
02:05
is a name average city speed as 40 transmission is auto and fuel type as electric and we call
02:13
the member functions. The first one my car dot print name prints the name of the vehicle.
02:19
Now we have created an object of type car which inherits the member function from the base
02:28
class vehicle and it prints the name of the vehicle. Similarly my car print average city speed prints the average
02:38
city speed. This too is defined in vehicle it is inherited from vehicle and then my car transmission type
02:46
and my car fuel type these are defined in class car and they are printed. So what we are seeing here is that
02:56
class car is derived from class vehicle. Now both these classes vehicle and car they are concrete classes.
03:06
So what we are having here is a concrete class derived from another concrete class and this is useful
03:13
because we are able to use the concept of vehicle and we are able to use the code for the class vehicle
03:20
and 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:30
conceptualized first and it is coded and then we are having the class car which is derived from class vehicle.
03:39
Now we look at virtual functions which is an important concept in inheritance. A virtual function starts with the
03:46
keyword virtual for example virtual void print vehicle details. Virtual function means that this is the default
03:56
implementation of a function in the base class. This can be overridden in the derived class with an
04:04
alternate implementation. Then there are pure virtual functions. Pure virtual functions are a special type of
04:10
virtual functions. They are not implemented in the base class. Instead they are declared with a notation equal to zero.
04:19
For example virtual int size constant equal to zero. They must be implemented in a derived class so that
04:29
that derived class can become a concrete class and can instantiate objects. Back to our example we can see
04:37
functions in the two classes vehicle based class and the car derived class. The functions print data about the
04:45
object vehicle or object car. We can definitely combine the print name and print average speed
04:53
functions in the class vehicle into a single function print vehicle details and we can make it a virtual
05:01
function so that it can be overridden by a more specialized function in the class car.
05:07
Print vehicle details in the class car calls print vehicle details in class vehicle and then it prints
05:15
transmission and fuel. Cut to the main function. We make objects of class car, vehicle and again car
05:23
and these are pointed by pointers v1, v2 and v3 respectively. v1, v2 and v3 are pointers to objects of
05:32
class vehicle. But v1 stores pointers to class car and v3 also stores pointers to class car. So we can
05:40
see that a pointer to a base class can also store pointers to objects of derived classes.
05:48
And again in the for loop using a pointer to the base class vehicle we call virtual function print vehicle
05:56
details. And in case of objects of class car print vehicle detail function of class car is called
06:04
which prints all the attributes those stored in the base class vehicle and also those stored in the
06:10
derived class car. If we had a few more derived classes like bike, van and truck each having its own
06:18
implementation of print vehicle detail the correct function would have been called depending upon the
06:24
subclass. Even though the pointer is of the base class vehicle we can say that the class vehicle is a
06:30
polymorphic type and the objects pointed by pointer to class vehicle behave differently depending upon the
06:38
subclass of the object. This concept is called polymorphism. We have seen inheritance concepts in C++ and also
06:49
example programs. You can find all this information at the soft priyok website. Here's the qr code and link is there in the
06:58
description. Please subscribe to soft priyok channel, click on the bell icon and enable notifications. Thanks very much for
07:07
watching. Take care and stay safe.
Be the first to comment
Add your comment
Recommended
7:46
|
Up next
Abstract Classes in C++
SoftPrayog
5 weeks ago
11:04
Inheritance 2020 explained by an idiot
High Boi
3 years ago
12:57
Copy and move in C++
SoftPrayog
1 week ago
8:21
Concrete Classes in C++
SoftPrayog
3 months ago
30:31
Data Types and Declarations in C++
SoftPrayog
5 months ago
0:29
Painted Storks
SoftPrayog
9 months ago
9:41
How to Backup a WordPress Site using the Command Line
SoftPrayog
9 months ago
10:47
How to take backups in Linux using the Command Line
SoftPrayog
9 months ago
0:59
VIM editor - copy, paste and delete operations
SoftPrayog
9 months ago
20:44
vi text editor in Linux
SoftPrayog
9 months ago
32:53
Socket programming in C
SoftPrayog
9 months ago
21:00
Signals in Linux
SoftPrayog
9 months ago
15:22
sed command in Linux
SoftPrayog
9 months ago
9:22
rsync file copy command
SoftPrayog
9 months ago
17:32
What is an Operating System?
SoftPrayog
9 months ago
15:40
ip command in Linux with examples
SoftPrayog
9 months ago
7:48
htop command in Linux
SoftPrayog
9 months ago
14:53
top command in Linux
SoftPrayog
9 months ago
14:27
grep command in Linux
SoftPrayog
9 months ago
30:34
Git Source Code Management Tutorial
SoftPrayog
9 months ago
10:50
diff, patch and vimdiff commands in Linux
SoftPrayog
9 months ago
31:06
Awk command in Linux
SoftPrayog
9 months ago
35:38
Time in Linux
SoftPrayog
9 months ago
13:52
Pthreads in C under Linux
SoftPrayog
9 months ago
7:58
ps command in Linux
SoftPrayog
10 months ago
Be the first to comment