Sunday, February 25, 2018

C++ : Dynamic Binding

#include <iostream>
using namespace std;
class A {
    int n;
protected:
   A(int i) : n(i) { }
   virtual int get() = 0;
   virtual void print() = 0;
};

int A::get() {
    return n;
}

class B : private A {
protected:
    B(int i) : A(i) {}
int get(){ return A::get();}
};  // End of class B

class C : public B {
public:
    C(int i) : B(i) {}
    void print() {
        cout << get() ;
    }
};

int main(){
    int n;
    cin >> n;
    C *p = new C(n);
    p->print();
    return 0;
}

Thanks
Happy Programming !

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