NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

oops in c++

1) inheritance and type of inheritance ?
- > Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class.

-> When we say derived class inherits the base class, it means, the derived class inherits all the properties of the base class, without changing the properties of base class and may add new features to its own. These new features in the derived class will not affect the base class. The derived class is the specialized class for the base class.

base or derived class difference
- Sub Class: The class that inherits properties from another class is called Subclass or Derived Class.
- Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.

* example
class Animal {
// eat() function
// sleep() function
};

class Dog : public Animal {
// bark() function
};

* Why and when to use inheritance?
- Consider a group of vehicles. You need to create classes for Bus, Car, and Truck. The methods fuelAmount(), capacity(), applyBrakes() will be the same for all three classes. If we create these classes avoiding inheritance then we have to write all of these functions in each of the three classes as shown below figure:

class truck class car class bike
fuel() fuel() fuel()
capacity() capacity() capacity()
break() break() break()

-> You can clearly see that the above process results in duplication of the same code 3 times. This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used. If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability. Look at the below diagram in which the three classes are inherited from vehicle class.

* type of inheritance
1) single inheritance
2)multiple inheritance
3)multilevel inheritance
4) hierachal inheritance
5) hybrid inheritance

1) single inheritance
-> this type of inheritance in where one ways class is being is inherited by another class that is derived class.
-> Single inheritance involves a derived class inheriting from only one base class. It forms a linear hierarchy, where each class has only one parent class. Here's an example:

class Base {
// Base class members
};

class Derived : public Base {
// Derived class members
};

1) multiple inheritance
-> one derived class is inherited from more than one base class is called multiple inheritance.
-> Multiple inheritance allows a derived class to inherit from multiple base classes. It enables a class to combine features from different sources. However, it can lead to the diamond problem, where ambiguity arises due to overlapping features.
- example
class Base1 {
// Base1 class members
};

class Base2 {
// Base2 class members
};

class Derived : public Base1, public Base2 {
// Derived class members
};

3)multilevel
-> in this type of inheritance one class is inherited another class and then this another class is being inherit by another class then it is called multilevel inheritance.
-> Multilevel inheritance involves a chain of inheritance, where a derived class inherits from another derived class. It forms a hierarchical structure, enabling classes to specialize further.
- exAMPLE
class Base {
// Base class members
};

class Derived1 : public Base {
// Derived1 class members
};

class Derived2 : public Derived1 {
// Derived2 class members
};

4)herical
in this type of inheritance in which are one base class is being inherited multiple derived class then it is called hierchal inheritance.
-> Hierarchical inheritance occurs when multiple derived classes inherit from a single base class. It allows for specialization and diversification of classes.
- example
class Base {
// Base class members
};

class Derived1 : public Base {
// Derived1 class members
};

class Derived2 : public Base {
// Derived2 class members
};

5)hybrid inheritance
- in this type of inheritance when any two type of inheritance into one then it is called hybrid inheritance.
-> Hybrid inheritance combines multiple types of inheritance, such as single, multiple, and hierarchical. It results in complex class relationships and should be used judiciously.
-example
class Base1 {
// Base1 class members
};

class Base2 {
// Base2 class members
};

class Derived1 : public Base1 {
// Derived1 class members
};

class Derived2 : public Base1, public Base2 {
// Derived2 class members
};


2) virtual function ?

- its is declared in base class with keyword virtual and redefined in derived class.
-> virtual function are dynamic.
-> they are always declared in base class and overridden in child class.
-> they called during runtime.
-> virtual function cannot be static.
->Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime polymorphism.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.