Hybrid inheritance

Combining various types of inheritance like multiple, simple, and hierarchical inheritance is known as hybrid inheritance.

In simple inheritance, one class is derived from a single class which is its base. In multiple inheritances, a class is derived from two classes, where one of the parents is also a derived class. In hierarchical inheritance, more than one derived class is created from a single base class.

In hybrid inheritance, there is a combination of one or more inheritance types. For instance, the combination of single and hierarchical inheritance. Therefore, hybrid inheritance is also known as multipath inheritance.




Single inheritance - Class B inherits class A. Thus an example of single inheritance.

Multiple inheritance - Class D is inherited from multiple classes( B and C shown above D). Thus an example of multiple inheritance.


Syntax

Class A   

{  

statement(s);  

};  

Class B: public A   

{  

statement(s);  

};  

Class C: public B   

{  

statement(s);  

};  

Class D: public B  

{  

statement(s);  

};  

 

Example

  Base Class: Pizza (stores pizza details)

  Intermediate Derived Class: Order (stores order details, inherits from Pizza)

  Derived Classes:

  • Customer (inherits from Order, manages customer details)
  • Delivery (inherits from both Order and Customer, handles delivery details)


Program:

#include <iostream>

using namespace std;


// Base Class: Pizza

class Pizza {

protected:

    string pizzaName;

    double price;

    string size;


public:

    // Constructor

    Pizza(string name, double p, string s) {

        pizzaName = name;

        price = p;

        size = s;

    }


    // Display Pizza Details

    void displayPizza() {

        cout << "\nPizza Name: " << pizzaName << endl;

        cout << "Size: " << size << endl;

        cout << "Price: Rs." << price << endl;

    }

};


// Intermediate Class: Order (Inheriting from Pizza)

class Order : public Pizza {

protected:

    int quantity;

    double totalPrice;


public:

    // Constructor

    Order(string name, double p, string s, int q) : Pizza(name, p, s) {

        quantity = q;

        totalPrice = price * quantity;

    }


    // Display Order Details

    void displayOrder() {

        displayPizza();

        cout << "Quantity: " << quantity << endl;

        cout << "Total Price: Rs." << totalPrice << endl;

    }

};


// Derived Class: Customer (Inheriting from Order)

class Customer : public Order {

protected:

    string customerName;

    string phoneNumber;


public:

    // Constructor

    Customer(string cname, string phone, string name, double p, string s, int q)

        : Order(name, p, s, q) {

        customerName = cname;

        phoneNumber = phone;

    }


    // Display Customer Details

    void displayCustomer() {

        cout << "\nCustomer Name: " << customerName << endl;

        cout << "Phone Number: " << phoneNumber << endl;

        displayOrder();

    }

};


// Derived Class: Delivery (Hybrid Inheritance)

class Delivery : public Customer {

private:

    string deliveryAddress;

    string deliveryStatus;


public:

    // Constructor

    Delivery(string cname, string phone, string name, double p, string s, int q, string address)

        : Customer(cname, phone, name, p, s, q) {

        deliveryAddress = address;

        deliveryStatus = "Pending";

    }


    // Mark as Delivered

    void markDelivered() {

        deliveryStatus = "Delivered";

    }


    // Display Delivery Details

    void displayDelivery() {

        displayCustomer();

        cout << "Delivery Address: " << deliveryAddress << endl;

        cout << "Delivery Status: " << deliveryStatus << endl;

    }

};


// Main Function

int main() {

    string cname, phone, pizzaName, size, address;

    int quantity;

    double price;


    cout << "Enter Customer Name: ";

    getline(cin, cname);

    cout << "Enter Phone Number: ";

    cin >> phone;

    cin.ignore(); // Ignore newline character


    cout << "Enter Pizza Name: ";

    getline(cin, pizzaName);

    cout << "Enter Pizza Size (Small/Medium/Large): ";

    cin >> size;

    cout << "Enter Price of Pizza: ";

    cin >> price;

    cout << "Enter Quantity: ";

    cin >> quantity;

    cin.ignore(); // Ignore newline character


    cout << "Enter Delivery Address: ";

    getline(cin, address);


    // Create a Delivery Order

    Delivery order(cname, phone, pizzaName, price, size, quantity, address);


    // Display Order Details

    order.displayDelivery();


    // Mark Order as Delivered

    char choice;

    cout << "\nDo you want to mark this order as delivered? (y/n): ";

    cin >> choice;

    if (choice == 'y' || choice == 'Y') {

        order.markDelivered();

        cout << "\nUpdated Order Details:\n";

        order.displayDelivery();

    }


    return 0;

}





Output:

Enter Customer Name: Prasanjeet

Enter Phone Number: 9471459250

Enter Pizza Name: Classic Margherita pizza

Enter Pizza Size (Small/Medium/Large): Medium

Enter Price of Pizza: 450

Enter Quantity: 4

Enter Delivery Address: VVIT Purnea


Customer Name: Prasanjeet

Phone Number: 9471459250


Pizza Name: Classic Margherita pizza

Size: Medium

Price: Rs.450

Quantity: 4

Total Price: Rs.1800

Delivery Address: VVIT Purnea

Delivery Status: Pending


Do you want to mark this order as delivered? (y/n): y


Updated Order Details:


Customer Name: Prasanjeet

Phone Number: 9471459250


Pizza Name: Classic Margherita pizza

Size: Medium

Price: Rs.450

Quantity: 4

Total Price: Rs.1800

Delivery Address: VVIT Purnea

Delivery Status: Delivered


Process returned 0 (0x0)   execution time : 95.515 s

Press any key to continue.


No comments:

Post a Comment