Since i started the series, I”ve covered a whole variety of topics ranging from the importance of classes, getters and setters and the various syntax and language complexities you guys need to know before delving into OOP seriously. Now we will get to topics which are a bit harder, and that starts off with inheritance in c .
Inheritance in C is an important tool for linking classes together and creating a good structure for your program. In real life, we see that there are many objects that have relationships with each other and share some traits. In C , we have already seen how we can make “friends” between classes and functions. Consider, however, two objects that ave a relationship in which one is derived, or inherited, from the other. A good example of this is in an organization made up of employees. Now the employees are divided into managers, workers, directors and on and on. When we are making a program dealing with this organization, we will make the “Employee” class, naturally. Then we will go and make the, e.g, “Manager” class. Now, as we know, a manager IS an employee, and both classes share some common traits and functionalities. So, every manager has a basse salary and every employee has a base salary so the data members, say, “float base salary” should ideally be in both classes. Instead of writing these traits or data members/functions down again in the Manager class, C allows us to use inheritance so that the Manager class adopts or inherits the functionalities of the Employee class. By making the Employee class the base class and the Managers class the derived class, we have used inheritance in C to cut down on a lot of best online casino work that we had to do. Let us see how.
Whenever two objects have the relationship “x is a y” or “x is y”, then inheritance should be used. Also, a class can be derived from more than one classes. The reason for using inheritance in C is simple: we can grant some derived classes basic access to the private or public data members or functions of our base class. The syntax for making this relationship is as follows:
</p> <p>. class derived_class_name: public base_class_name</p> <p>
The “public” keyword can be replaced with “private” or “protected”. This has some differences for each. Learn more about this in this tutorial for inheritance in C .
Let me point our now the main difference between protected and private data members. When a class inherits from another one, the members of the derived class can access the protected members inherited from the base class, but not its private members. So basically, a derived class has access to the public and protected interface of the base class, but not directly the private part.
A derived class inherits all base functions of the base class except for:
1) Constructors, destructors and copy constructors
2) Friend functions
3) Overloaded operator functions
So, we have to deal with this accordingly.
Whenever we write big programs in C , we will almost always make classes and use OOP. And almost whenever we use classes or its like, we will use inheritance. Thus, inheritance in C is one of the most powerful tools in the language. It helps cut down the amount of work we have to do in our code and makes it more efficient and reliable. It also helps us define a natural and realistic flow to our program.
Now that you know why, what and how inheritance is done, lets move on and make it clearer using examples. Stay tuned for the next article in this object oriented programming series for beginners!
Trackback from your site.