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

Sacred Thought

28 April 2024 Today I am going to explain verse 31 - 38 chapter two for you all. There is no opportunity better than a righteous war (सत्य औ...