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

What is a superconductor?

 A superconductor is a material that can conduct electricity with zero electrical resistance when it is cooled below a specific, very cold t...