Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

C++ Program for Hybrid Inheritance (All Types Together)

C++ Program for Hybrid Inheritance (All Types Together)

✅ C++ Program Demonstrating Hybrid Inheritance

#include <iostream>
using namespace std;

// Multi-level inheritance
class values {
  public:
    int a, b;
    void display1() {
        cout << "Enter any two numbers:\n";
        cin >> a >> b;
    }
};
class add: public values {
  public:
    void display2() {
        cout << "Addition of " << a << " and " << b << " is " << a+b << "\n";
    }
};
class sub: public add {
  public:
    void display3() {
        cout << "Subtraction of " << a << " and " << b << " is " << a-b << "\n";
    }
};

// Multiple inheritance
class a {
  public:
    void display4() {
        cout << "Hi I am from class A:\n";
    }
};
class b {
  public:
    void display5() {
        cout << "Hi I am from class B:\n";
    }
};
class c: public a, public b {
  public:
    void display6() {
        cout << "Hi I am class C derived from A and B:\n";
    }
};

// Single inheritance
class parent1 {
  public:
    void display7() {
        cout << "Hi I am parent (Single Inheritance):\n";
    }
};
class child1: public parent1 {
  public:
    void display8() {
        cout << "Hi I am child from single inheritance:\n";
    }
};

// Hierarchical inheritance
class parent2 {
  public:
    void display9() {
        cout << "Hi I am parent2 (Hierarchical Inheritance):\n";
    }
};
class child2: public parent2 {
  public:
    void display10() {
        cout << "Hi I am child2 derived from parent2:\n";
    }
};
class child3: public parent2 {
  public:
    void display11() {
        cout << "Hi I am child3 derived from parent2:\n";
    }
};

int main() {
    // Multi-level inheritance
    sub obj;
    obj.display1();
    obj.display2();
    obj.display3();

    // Multiple inheritance
    c obj1;
    obj1.display4();
    obj1.display5();
    obj1.display6();

    // Single inheritance
    child1 obj2;
    obj2.display7();
    obj2.display8();

    // Hierarchical inheritance
    child2 obj3;
    obj3.display10();
    child3 obj4;
    obj4.display11();
}
  

๐Ÿ“˜ Explanation:

This program demonstrates Hybrid Inheritance in C++. Hybrid inheritance means a combination of different inheritance types:

  • Multi-level inheritance: class values → add → sub
  • Multiple inheritance: class c inherits from a and b
  • Single inheritance: class parent1 → child1
  • Hierarchical inheritance: class parent2 → child2 and child3

๐Ÿงพ Sample Output:

Enter any two numbers:
8 3
Addition of 8 and 3 is 11
Subtraction of 8 and 3 is 5
Hi I am from class A:
Hi I am from class B:
Hi I am class C derived from A and B:
Hi I am parent (Single Inheritance):
Hi I am child from single inheritance:
Hi I am child2 derived from parent2:
Hi I am child3 derived from parent2:
  

๐Ÿ”‘ Keywords:

C++ hybrid inheritance, multiple inheritance in C++, multi-level inheritance, hierarchical inheritance, single inheritance example, OOP in C++

๐Ÿ“Œ Hashtags:

#CPlusPlus #HybridInheritance #CppExamples #Inheritance #OOP #ProgrammingForBeginners

๐Ÿ” Search Description:

This C++ program demonstrates hybrid inheritance by combining multiple, multi-level, hierarchical, and single inheritance. Includes full code, explanation, and sample output.

Comments

Popular Posts

๐ŸŒ™