fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class A {
  7. public:
  8. int va1;
  9. int va2;
  10. string va3;
  11.  
  12. virtual void fa1(int x1, string x2) { }
  13. int fa2 (bool y1, double y2) { cout <<"call A::fa2()"<<endl;}
  14. A() { }
  15. virtual ~A() { }
  16. };
  17. class B : public A {
  18. public:
  19. bool ab1;
  20. double ab2;
  21. long double ab3;
  22.  
  23. bool fb1(double x1) { }
  24. long double fb2() { cout << "call B::fb2()"<<endl;}
  25. B(int z1) : ab2(z1) { }
  26. virtual ~B() { }
  27. };
  28. class C : public A {
  29. public:
  30. int ac1;
  31.  
  32. long double fc1() { }
  33. virtual void fa1(int x1, string x2) { }
  34. C() { }
  35. virtual ~C() { }
  36. };
  37.  
  38. int main() {
  39. vector<shared_ptr<A>> v;
  40.  
  41. v.push_back(make_shared<A>());
  42. v.push_back(make_shared<B>(2));
  43. v.push_back(make_shared<C>());
  44.  
  45. for (shared_ptr<A> pa : v) {
  46. pa->fa2(true,0.07);
  47. shared_ptr<B> pb = dynamic_pointer_cast<B>(pa);
  48. if (pb) {
  49. cout << "Object is also a B"<<endl;
  50. pb->fb2();
  51. } else cout << "Object is not a B"<<endl;
  52. cout <<endl;
  53. }
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
call A::fa2()
Object is not a B

call A::fa2()
Object is also a B
call B::fb2()

call A::fa2()
Object is not a B