 Hello and welcome. In this session, we are going to talk about inheritance in C plus plus programming. Here is a brief overview of the lecture. We will start with some motivation for why we want to study inheritance in C plus plus programming. Then, we will look at two different approaches which might be used to solve the problem that we will talk about during the motivational part of the lecture. Then, we will also see how to define hierarchies of classes which is basically how you implement inheritance in C plus plus. Much of this lecture is motivated by the treatment in the book and introduction to programming through C plus plus by Abhiram G. Ranade. So, here is a simple example. We have different kinds of bank accounts. So, the information that is stored in bank accounts can be thought of as represented in some kind of hierarchy of accounts. So, here is a base bank account which basically contains all the information that should be present in any kind of bank account. Then, we have different kinds of bank accounts like a savings account or a current account or a term deposit account and a savings account in turn could be of two different types. For example, a minor savings account or an adult savings account. Similarly, a current account could again be of two different types for non-commercial entities and for commercial entities and so on. So, what we see here is that when we talk of bank accounts, it is not just one kind of bank account that we want to talk about. We might want to talk about a minor savings account or a commercial current account. However, the information that I need to store in a minor savings account and the information that I need to store in a commercial current account have some commonality. For example, both of them would have a bank account number, there would be a name associated with that account. So, there is some information that is there in the base account which is common to a minor savings account in a commercial current account. Then, there are some special information that are present in a minor savings account which are not present in a commercial current account. Similarly, there might be some information that is present in an adult savings account which is not present in a minor savings account and so on and so forth. So, really the information is stored in a hierarchical fashion when we want to talk about the hierarchy of bank accounts. Now, what we want to do is we want to define a hierarchy of classes for representing this hierarchy of information in bank accounts. So, here I have not shown the hierarchy below savings and current just so that it does not clutter the screen, but you can think of those as also being present. So, this is the hierarchy of bank accounts and a base account may have information like an ID for the account, the balance in the account and the name of the account holder. In addition, a savings account may also have the age of the account holder and the ATM card number associated with the savings account. A current account may have information about what is the maximum amount that can be saved in this account and what is the overdraft amount. A deposit account could similarly have information about what is the duration of the deposit and what is the principal sum in the deposit account. So, what you see is that there is common information like ID, balance and name which all the three types of accounts share and then there is some specific information like age and ATM which is specific for a savings account and is not necessarily present in a current account or a deposit account. So, this is the hierarchy of information that we want to represent using classes. So, we want to define a hierarchy of classes for representing this hierarchy of bank information. We have already seen that a savings account and a current account and a deposit account are different special cases of a base account and similarly I could have a minor account as a special case of a savings account and adult account is a special case of a savings account and similarly non-commercial and commercial a special cases of current account and just like here I had some additional information with the savings account in addition to the information with the base account. I may similarly have additional information with the minor account for example, the Garjan's name in addition to the information in a savings account and similarly an adult savings account may have some other kind of information that needs to be stored for example, the government ID number which may not be necessary when I have a minor savings account. Similarly, for a non-commercial current account I may want to store the work domain information in addition to all of the information that is stored with the current account and for a commercial current account I may want to store some other information. Let us say the company registration number in addition to all the information that is stored with the current account. So, this is really the hierarchy of information that we want to represent. So, how would we go about doing this? Well, one could think of a compositional way to do this. This is using all of the knowledge that we have already gained so far in our study of C++ programming. So, for example, I could define a base class which has 3 public data members. I will call them ID, balance and name and they have appropriate data types and then I could define a savings class which could have an object of the base class and then it could have additional data members like age and the ATM card number and you see that this object B is of the base class. So, within this object B I can store information about the ID, balance and the name and similarly I could have a class called current which also has an object of the class B and it has additional data members called amount and overdraft and this object of class B in the class current could store information about the ID, balance and name of the current account. So, in a program if I were to use an object of class savings and then if I were to refer to the ID associated with that object of class savings I will have to say S dot B dot ID and then access the ID data member of the object B in the object S. However, I could just say S dot age to refer to the age data member of the object S of class savings. Similarly, an object of class current if I were to access the ID information associated with that object I would have to first go to the data member B of the object C and then within the data member B which is an object of class base I can again access the data member ID. So, I have to use C dot B dot ID to refer to the ID associated with this object of class current. However, I could directly refer to C dot amount which is a data member of the class current C being an object of the class current. So, this is how I would use the different data members of the base class and the savings class in my program. So, this is called the compositional way of representing a hierarchy of information. The common information is saved or represented in a base class and then I keep instantiating objects of this base class in the different other accounts which are special cases of this base class. So, like a current account is a special case of a base account and the savings account is a special case of the base account. Now, there is an alternative way to do this and that is the primary topic of our study in this lecture which is called the inheritance way. So, I will first show you how we specify inheritance in C++ programming and then we will talk in more detail about what inheritance basically means and how we use inheritance in our programming. So, here we have the same base class and what I want to now do is I want to say that I want to have a savings class, but in that I do not want to instantiate an object of class base, but I just want to say that the savings class should inherit everything that the base class has and maybe it can have a few other data members. So, in C++ I would do it like this, I would say I want to declare the savings class and this colon public base is saying that this savings class is actually derived from this base class. So, it is going to inherit all the members of this base class and we will talk about what this public means in a subsequent lecture we could also have private or protected over here, but we will talk about the specifics of that in a subsequent lecture for the time being let us go with public. So, this declaration says that the savings class is derived from the base class and so it inherits whatever is there in the base class and in addition it has two public data members called age and ATM. Similarly, there is this current class which is also derived from the base class. So, it is going to inherit all the data members from the base class and in addition it has two additional data members called amount and overdraft. Now, you see the difference of what we are doing here with the compositional way in the compositional way I have to explicitly declare an object of class base inside the class savings. Whereas, here implicitly I am saying all the data members of the class base are inherited by the class savings when I write something like this indicating that savings is derived from the base class. So, consequently when I am doing my programming I can directly refer to s dot id because the data member id is inherited by the savings class from the base class and similarly I could talk about an object c of class current and I could directly talk about c dot id because the class current is a derived class from the base class and so it has inherited the data members from the base class and so there is a data member id which I can access in the class current just like c dot id is accessed over here. So, you see that whether I use the compositional way or the inheritance way to represent a hierarchy of information it leads to different ways of accessing the data members in the inheritance way I can directly refer to the object of savings class and say dot id although this id is a data member of the base class from which savings is derived and similarly I can directly talk about c dot id where c is an object of the class current and id is a data member in the base class from which c is derived. In contrast in the compositional way I have to write explicitly like this that in the object of class savings look at the data member b which is an object of class base and in that object look at the data member id and similarly here in the object of class current look at the data member b which is an object of class base and that object look at the data member id. So, as you can see if I have a deeply nested hierarchy of information Then, if I were to program in the compositional way, I would get these long lists of dots over here whereas, here I can directly access them as long as I know that the savings class is derived from the base class and the current class is derived from the base class. So, this is the difference in access style for the compositional way and the inheritance way. Now, there are more things that inheritance in C plus plus allows us to achieve and here is just another example. Suppose, I want to have some complex access control. Let us say my requirement is that the members ID and balance of the class base are to be accessible only from the class savings and not from other classes. So, I have the base class which has members data members ID and balance and there is the savings class which is supposed to be a special case of the base class, but I want the members ID and balance to be only accessible from the savings class and not from other classes. So, how do we achieve this? Well, in the compositional way, the way to achieve this is to say that here is your base class and because you do not want ID and balance to be accessible by other classes which might have objects of the base class, you want to declare ID and balance as private data members of base class. Declaring ID and balance as private data members effectively prevents other classes which might instantiate objects of the base class to access these data members. However, I do want the class savings to access this data members. So, what do I do? I declare that the class savings is a friend of the base class and then inside the class savings in which an object of the base class is declared, I can directly access the private data members of this object of the base class. For example, I can access B dot ID and B dot balance although ID and balance are private data members of the base class because I have declared the class savings as a friend class of the base class. Now, what would happen if I want to extend this privilege of accessing these two private data members ID and balance to also the current class? Well, in the compositional way, the way to do this would be to go ahead and change the declaration of base class to also include the current class as a friend class and then we could talk about having a current class which can directly access the private data members of the object B of the base class inside it. So, the current class has the object B of the base class and because the current class is declared as a friend of the base class, I can now directly access the private data members of the object B inside the current class. So, you see every time I want to extend this privilege to another class, I will have to go and really modify the base class to add this friend declaration. So, natural question to ask is that can we do this without actually modifying the base class every time, I want to add another class to which I want to extend this privilege of accessing some special members of the base class? So, natural question to ask is that can we explicitly say in our programming vocabulary that I want a class to have special privileges to access certain data members, certain attributes of another class and more specifically can I say that a particular class like the savings class or the current class is really derived from a base class and inherits all the attributes of the base class with some special privileges to access those attributes which other classes which are not derived from this base class will not have and this is what inheritance in C plus plus allows you to do. It allows you to define derived classes which are derived from base classes and these derived classes inherit all the attributes of the base classes and they have special privileges to access those attributes. So, in our example recall that we had this hierarchy of bank account information, now we want to represent this using inheritance in C plus plus. So, we will define a hierarchy of classes. So, we will declare a super class or a base class which is at the top of this hierarchy and it has the information which will be also accessible from every other class in this hierarchy and then at the next level we have the derived classes like the savings class is derived from the base class and so it inherits all the attributes of the base class it can access those attributes and it has some additional attributes along with it. Note that here the attributes of the base class have all been declared public we will see other kinds of access controls for the data members of the base class in a subsequent lecture. But, for this lecture we are looking at only public data members. So, savings inherits all the public data members of base and it has additional public data members similarly current inherits all the public data members and has additional data members and so is the case for deposit. So, these are derived classes which are derived from this base class and we could have additional classes which are derived from this derived classes. So, the minor class is derived from the savings class. So, as far as the minor class is concerned the savings class is its base class or super class, but the savings class itself is a derived class and the base class is its super class or base class. And what does this say that the minor class inherits everything from the savings class and in the process inherits everything from the base class and in addition it has some additional data members. So, I am saying inherits everything from the base class because we have public data members in all the classes here we will see in a later lecture that we can selectively inherit some of the members from the base class using other keywords here which define what kind of access control we have. So, here I have these classes which are derived from another derived class and similarly these classes could be derived from this derived class and so on. Another pictorial way of viewing this is to say that the minor class has everything that the savings class has and when I say everything that is because all the data members are public here. And similarly the savings class has everything that the base class has once again because all the data members of the base class are public and this is how we have the hierarchy of information. So, here is a complete program which uses this class hierarchy declarations that we just saw this is just to show how we can use these class hierarchies in a simple C plus plus program. So, here is our base class declaration here is the savings class declaration which is derived from the base class. So, it inherits all the data members of the base class and has two additional data members similarly the current class is derived from the base class it inherits all its data members and has some additional data members. And here is my main program in which I have instantiated an object of class savings. And therefore, this object has all the data members that are present in the base class and also the data members of the savings class. So, I can say s dot id is assigned 1 in which case I am really referring to the data member of the base class from which the savings class is derived. Or I could say s dot age is assigned 20 in which case I am accessing a data member of the savings class directly similarly c is an object of the current class and I could say c dot id is assigned 2 where I am really accessing a data member of the base class from which the current class is derived or I could say c dot amount is 15000 in which case I am accessing the data member amount of the current class directly. So, this is a simple c plus plus program which shows how you can access different data members of derived classes and their base classes. So, in summary in this lecture we looked at compositional versus inheritance based approaches to represent a hierarchy of information. And we also saw how to define a hierarchy of classes. Thank you for listening to this lecture.