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 Multi-Level Inheritance (Grandparent → Parent → Child)

C++ Program for Multi-Level Inheritance (Grandparent → Parent → Child)

✅ C++ Program to Demonstrate Multi-Level Inheritance

#include <iostream>
using namespace std;

class grandparent {
  public:
    void display1() {
        cout << "Hello I am your grandparent:\n";
    }
};

class parent : public grandparent {
  public:
    void display2() {
        cout << "Hello I am parent:\n";
    }
};

class child : public parent {
  public:
    void display3() {
        cout << "Hello I am child:\n";
    }
};

int main() {
    child obj;
    obj.display1();
    obj.display2();
    obj.display3();
}
  

๐Ÿ“˜ Explanation:

This program demonstrates the concept of multi-level inheritance in C++. - The grandparent class defines display1(). - The parent class inherits from grandparent and adds display2(). - The child class inherits from parent and adds display3(). - Thus, an object of the child class can access functions from all three classes.

๐Ÿงพ Sample Output:

Hello I am your grandparent:
Hello I am parent:
Hello I am child:
  

๐Ÿ”‘ Keywords:

C++ multi-level inheritance example, grandparent parent child program, inheritance in C++, OOP in C++, C++ object oriented programming

๐Ÿ“Œ Hashtags:

#CPlusPlus #Inheritance #MultiLevelInheritance #OOP #CppExamples #CodingForBeginners

๐Ÿ” Search Description:

This C++ program demonstrates multi-level inheritance where a child class inherits from a parent, which in turn inherits from a grandparent class. Includes example code, explanation, and sample output.

Comments

Popular Posts

๐ŸŒ™