Advertisement
homer512

Polymorphism

Jan 22nd, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct Base {
  7.   int av;
  8.   virtual bool is (int value) const
  9.   {
  10.     return av == value;
  11.   }
  12.   virtual void print() const
  13.   {
  14.     cout << av << endl;
  15.   }
  16. };
  17.  
  18. struct A : public Base {
  19.   int bv;
  20.   virtual bool is (int value) const
  21.   {
  22.     return bv == value;
  23.   }
  24.   virtual void print () const
  25.   {
  26.     cout << bv << endl;
  27.   }
  28. };
  29. struct database {
  30.   vector <Base*> basev;
  31.   Base NBase;
  32.    
  33.   database () { NBase.av = 0; }
  34.    
  35.   Base* findb (int value) {
  36.     for (int i = 0; i < basev.size (); ++i) {
  37.       if(basev[i]->is (value));
  38.     return basev[i];
  39.     }
  40.     return &NBase;
  41.   }
  42. } db;
  43.    
  44. int main( ) {
  45.  
  46.   Base der;
  47.   der.av = 5;
  48.   db.basev.push_back (&der);
  49.  
  50.   Base* found = db.findb (5);
  51.   found->print ();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement