Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

C++ Program Example on Virtual Function and Runtime Polymorphism

C++ Program Example on Virtual Function and Runtime Polymorphism

✅ C++ Program to Demonstrate Virtual Function and Runtime Polymorphism

#include <iostream>
using namespace std;

class Base {
public:
    // Virtual function
    virtual void display() {
        cout << "Display from Base class\n";
    }
};

class Derived : public Base {
public:
    void display() {
        cout << "Display from Derived class\n";
    }
};

int main() {
    Base *ptr;      // Base class pointer
    Derived obj;    // Derived class object
    ptr = &obj;

    // Because 'display' is virtual, Derived version is called
    ptr->display(); 

    return 0;
}
  

πŸ“˜ Explanation:

This program demonstrates runtime polymorphism in C++ using a virtual function.

  • display() is declared as virtual inside the Base class.
  • The Derived class overrides the display() function.
  • A Base class pointer is used to call display() on a Derived object.
  • Because of dynamic binding, the Derived version executes at runtime.

🧾 Sample Output:

Display from Derived class
  

πŸ”‘ Keywords:

C++ virtual function example, runtime polymorphism, OOPs in C++, base and derived class example, dynamic binding

πŸ“Œ Hashtags:

#CPlusPlus #OOPs #VirtualFunction #RuntimePolymorphism #Programming

πŸ” Search Description:

Learn how virtual functions enable runtime polymorphism in C++. This example demonstrates how a base class pointer can call a derived class function using the virtual keyword.

Comments

Popular Posts

πŸŒ™