GAME OF CODE’22 — DAY 6
Object-Oriented Programming — II
So, till now you must have developed a basic understanding of programming in C++. The syntax, Basic I/O streams, Functions, Arrays, Looping and Conditioning, Structures, and Basic OOPs
Still, wondering what’s more? 🤔
Well, there’s a lot ahead, programming is an ever-developing field you’ll always find new and much more efficient ways to execute the same thing, and today we’ll be looking at two very important concepts of programming which are — Inheritance and Polymorphism.
Umm Inheritance, well this sounds like something I already know….?
You might already be aware of what inheritance is, from a real-life example a child inherits some of its traits from his/her parents, in object-oriented programming inheritance, also means something similar to inheritance in real life. So, let’s deep dive into the concepts of Inheritance 😊
Inheritance:
In C++, Inheritance allows classes to inherit or in other words have access to Data members (Attributes) and methods or functions (Behaviour) from other classes. We call the class from which attributes and behaviors are inherited the Base Class (parent class) and the class which inherits these properties as the Derived Classes (child class).
For example, In the case of Car which is parent class as Car have some attributes like 4 wheels, two lights in front & two at back, brakes, etc., there could be multiple models of the car, these multiple models are child class as these models have some unique element with them but all of the different models possess some the common properties of the parent class Car.
Here, the car is a parent class with some attributes and properties such as brand and rotating wheels which are being inherited by the child class X5 which have all the properties of the parent class with some extra properties like the model’s name.
C++ supports five types of inheritance:
- Single inheritance — defined as the inheritance in which a derived class is inherited from only one base class.
- Multiple inheritance — the process of deriving a new class that inherits the attributes from two or more classes.
- Hierarchical inheritance — defined as the process of deriving more than one class from a base class.
- Multilevel inheritance — the process of deriving a class from another derived class.
- Hybrid inheritance — the combination of more than one type of inheritance.
Polymorphism:
Polymorphism is combined with two words which are Poly which means many and Morph means forms. For example, a car model can have many different modes (behaviors) such as off-roading, racing mode or city-driving mode, etc. Here the same model of car behaves differently with different modes according to situations.
In C++ Polymorphism is divided into two types-
· Compile Time Polymorphism
· Run Time Polymorphism
Let us discuss each one of them in detail now-
Compile-time polymorphism-
Compile-time polymorphism uses Method Overloading, which occurs when multiple methods have the same name but a different number of arguments or parameters in the method call. In simple words, functions with the same name but a different number of parameters result in method overloading and compile-time polymorphism.
Runtime Polymorphism-
Runtime polymorphism uses method overriding, in which a child class implements the same method which is inherited from the parent class in a different form. In simple words, it overrides the pre-written methods by the parent class.
The method mode in the above code has the same name in both parent and in child class but contains different information this is an example of runtime polymorphism.
Conclusion:
Object-Oriented Programming helps us to structure our code into a more reusable form and reduce redundancy in our code.
So, what we’ve learned in OOPs?
We have learned about concepts of Classes and Objects, Abstraction, Encapsulation, Inheritance, and Polymorphism which are in general known as the Four Pillars of OOPs. Now you can use this skill set to code better and efficiently build better applications!
Looking Forward to learning more?
Here are some Useful Resources which might help-
https://www.javatpoint.com/cpp-inheritance
Thanks for reading
Keep Learning, Keep Growing :)
Enjoy Coding!!!