Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

C++ Program for Hierarchical Inheritance (Parent → Child1, Child2)

 

C++ Program for Hierarchical Inheritance (Parent → Child1, Child2)

✅ C++ Program to Demonstrate Hierarchical Inheritance

#include <iostream>
using namespace std;

class parent {
  public:
    int a, b;
    void data() {
        cout << "Enter two numbers:\n";
        cin >> a >> b;
    }
};

class child1 : public parent {
  public:
    void add() {
        cout << "Addition of " << a << " and " << b << " is " << a+b << "\n";
    }
};

class child2 : public parent {
  public:
    void sub() {
        cout << "Subtraction of " << a << " and " << b << " is " << a-b << "\n";
    }
};

int main() {
    child1 obj1;
    obj1.data();
    obj1.add();

    child2 obj2;
    obj2.data();
    obj2.sub();
}
  

πŸ“˜ Explanation:

This program demonstrates the concept of hierarchical inheritance in C++. - A single parent class provides the data() function to take input. - Child1 inherits from parent and implements addition. - Child2 inherits from parent and implements subtraction. - Each child class reuses the input method from the parent and performs its own operation.

🧾 Sample Output:

Enter two numbers:
5 3
Addition of 5 and 3 is 8
Enter two numbers:
7 2
Subtraction of 7 and 2 is 5
  

πŸ”‘ Keywords:

C++ hierarchical inheritance, parent child program in C++, addition and subtraction program, inheritance in C++, OOP in C++, C++ object oriented programming

πŸ“Œ Hashtags:

#CPlusPlus #Inheritance #HierarchicalInheritance #OOP #CppExamples #CodingForBeginners

πŸ” Search Description:

This C++ program demonstrates hierarchical inheritance where multiple child classes inherit from a single parent class. Includes example code, explanation, and sample output.

Comments

Popular Posts

πŸŒ™