Skip to main content

Posts

Showing posts with the label types of inheritance

What is inheritance? List different types of Inheritance supported in C++. Write an OOP showing multiple inheritance.

 Inheritance is a mechanism in object-oriented programming where a new class is derived from an existing class. The new class inherits all the properties and behavior of the existing class and can also add its own properties and behavior. Different types of Inheritance supported in C++ are: Single Inheritance: A derived class is derived from a single base class. Multiple Inheritance: A derived class is derived from multiple base classes. Hierarchical Inheritance: Multiple derived classes are derived from a single base class. Multilevel Inheritance: A derived class is derived from a base class, which is again derived from another base class. Hybrid Inheritance: Combination of multiple inheritance and hierarchical inheritance. Here is an example of multiple inheritance: #include <iostream> using namespace std; // Base class 1 class Shape {    public:       void setWidth(int w) {          width = w;       }   ...

Explain different types of inheritance with their syntax.

 In C++, inheritance is a mechanism that allows a class to inherit the properties of another class. There are four types of inheritance in C++: 1. Single Inheritance:     Single inheritance is the most common type of inheritance. In this type of inheritance, a derived class inherits properties of only one base class. The syntax for single inheritance is as follows: class Base {     // Base class members }; class Derived : public Base {     // Derived class members }; 2. Multiple Inheritance: In multiple inheritance, a derived class can inherit properties of more than one base class. The syntax for multiple inheritance is as follows: class Base1 {     // Base class 1 members }; class Base2 {     // Base class 2 members }; class Derived : public Base1, public Base2 {     // Derived class members }; 3. Hierarchical Inheritance: In hierarchical inheritance, a single base class serves as a parent to multiple derived classes. Th...