fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class monitor
  5. {
  6. public:
  7. virtual int price() = 0; // virtual, meaning ignore and go straight to other subclasses,
  8. //if the type WAS NOT nineteenInchMonitor, it would use this.
  9. };
  10.  
  11. class nineteenInchMonitor : public monitor
  12. {
  13. int price()
  14. {
  15. return 99; // return price
  16. }
  17. };
  18.  
  19. class twentyInchMonitor : public monitor
  20. {
  21. int price()
  22. {
  23. return 129;
  24. }
  25. };
  26.  
  27. int sell (monitor *mon)
  28. {
  29. return mon->price(); // get price() of smallMonitor, type nineteenInchMonitor
  30. }
  31.  
  32. int main()
  33. {
  34. nineteenInchMonitor smallMonitor; twentyInchMonitor bigMonitor; // makes nineteenInchMonitor, type smallMonitor
  35. cout << sell (&bigMonitor) << endl; // pass pointer onto sell
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
129