Created
December 16, 2015 06:45
-
-
Save zehel2892/d529fde5137e69b886e7 to your computer and use it in GitHub Desktop.
Using pointer to base class to call functions from derived class using another independent class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class Base{ | |
public: | |
Base(){} | |
~Base(){} | |
virtual void Write(){ | |
cout<<"Base"<<endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
public: | |
Derived(){} | |
~Derived(){} | |
virtual void Write(){ | |
cout<<"Derived"<<endl; | |
} | |
}; | |
class Independent{ | |
public: | |
Independent(){} | |
~Independent(){} | |
void DoSomething(Base * obj) | |
{ | |
list.push_back(obj); | |
for(int a=0;a<list.size();a++) | |
{ | |
list[a]->Write(); | |
} | |
} | |
protected: | |
vector<Base *> list; | |
}; | |
int main() | |
{ | |
Independent i; | |
Base *d = new Derived(); | |
i.DoSomething(d); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment