fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. struct IGetA
  7. {
  8. virtual int getA() = 0;
  9. };
  10.  
  11. class Base : public IGetA
  12. {
  13. protected:
  14. int a;
  15. public:
  16. int getA()
  17. {
  18. return a;
  19. }
  20. };
  21.  
  22. class Derived : public Base
  23. {
  24. protected:
  25. int b;
  26. };
  27.  
  28. class Set
  29. {
  30. protected:
  31. IGetA* dataArray;
  32. public:
  33. void run1()
  34. {
  35. int c = 5;
  36. dataArray = new Derived[c];
  37. cout << dataArray[3].getA(); // case 1
  38. }
  39. };
  40.  
  41. void run2(IGetA* b, int index )
  42. {
  43. cout << b[index].getA();
  44. }
  45.  
  46. int main()
  47. {
  48. Set set1;
  49. set1.run1();//case 1
  50.  
  51. Derived set2[5];
  52. run2(set2, 3);// case 2
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 2812KB
stdin
Standard input is empty
stdout
0134514845