Sunday, February 25, 2018

C++ program : Polymorphism and Virtual Functions

#include <iostream>
using namespace std;
class Base {
public:
    virtual void show() = 0;
};
class Derived : public Base {
    int i;
public:
    Derived(int num) : i(num) { i = i * 2; }
void show(){cout<<i;}




};
int main() {
    int n;
    cin >> n;
    Derived d(n);
    Base &b = d;
    b.show();
    return 0;
}

Thanks
Happy Computing !

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows