Subscribe Us

Responsive Advertisement

Advertisement

pointer to derived Object

 


pointer to derived Object 




#include<bits/stdc++.h>

using namespace std;

class B

{

public:

    int a;

    void show()

    {

        cout<<"a="<<a<<endl;


    }

};

class D:public B

{

public :

    int b;

    void show()

    {

        cout<<"a="<<a<<" b="<<b<<endl;

    }

};

int main()

{

    B b;

    B *bp;

    D d;

    bp=&b;

    bp->a=10;

    bp->show();

    bp=&d;

    bp->a=20;

    //bp->b=30;

    bp->show();

    D *dp;

     dp=&d;

    dp->a=20;

    dp->b=30;

    dp->show();

    ((D*)bp)->b=100;

    ((D*)bp)->show();


    return 0;

}


Post a Comment

0 Comments