Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

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

๐ŸŒ™