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

Sacred Thought

26 April 2024  Dear friends, I write the explanation of two verses of Geets for all of you, I hope you all will like it and benefit from it....