fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class plant
  5. {
  6. public:
  7. int height;
  8. virtual int grow(int)= 0;
  9. };
  10.  
  11. class seaweed: public plant
  12. {
  13. int grow(int x)
  14. {
  15. return x*5;
  16. }
  17. };
  18. class oak: public plant
  19. {
  20. int grow(int x)
  21. {
  22. return x*1.1;
  23. }
  24. };
  25.  
  26. seaweed seaweed1;
  27. oak oaktree1;
  28.  
  29. plant *listingArray[2] = {&seaweed1,&oaktree1};
  30.  
  31. int main()
  32. {
  33. seaweed1.height=1;
  34. oaktree1.height=1;
  35. for (int i=0; i<2; i++)
  36. {
  37. listingArray[i]->height=1;
  38. }
  39.  
  40. for (int years=0; years<50; years++)
  41. {
  42. for (int i=0; i<2; i++)
  43. {
  44. listingArray[i]->height=listingArray[i]->grow(listingArray[i]->height);
  45. }
  46. cout << "after "<<years<<" years:\n oak = "<<oaktree1.height<<"\nseaweed = "<<seaweed1.height<<endl;
  47. }
  48. return 0;
  49. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
after 0 years:
 oak = 1
seaweed = 5
after 1 years:
 oak = 1
seaweed = 25
after 2 years:
 oak = 1
seaweed = 125
after 3 years:
 oak = 1
seaweed = 625
after 4 years:
 oak = 1
seaweed = 3125
after 5 years:
 oak = 1
seaweed = 15625
after 6 years:
 oak = 1
seaweed = 78125
after 7 years:
 oak = 1
seaweed = 390625
after 8 years:
 oak = 1
seaweed = 1953125
after 9 years:
 oak = 1
seaweed = 9765625
after 10 years:
 oak = 1
seaweed = 48828125
after 11 years:
 oak = 1
seaweed = 244140625
after 12 years:
 oak = 1
seaweed = 1220703125
after 13 years:
 oak = 1
seaweed = 1808548329
after 14 years:
 oak = 1
seaweed = 452807053
after 15 years:
 oak = 1
seaweed = -2030932031
after 16 years:
 oak = 1
seaweed = -1564725563
after 17 years:
 oak = 1
seaweed = 766306777
after 18 years:
 oak = 1
seaweed = -463433411
after 19 years:
 oak = 1
seaweed = 1977800241
after 20 years:
 oak = 1
seaweed = 1299066613
after 21 years:
 oak = 1
seaweed = -2094601527
after 22 years:
 oak = 1
seaweed = -1883073043
after 23 years:
 oak = 1
seaweed = -825430623
after 24 years:
 oak = 1
seaweed = 167814181
after 25 years:
 oak = 1
seaweed = 839070905
after 26 years:
 oak = 1
seaweed = -99612771
after 27 years:
 oak = 1
seaweed = -498063855
after 28 years:
 oak = 1
seaweed = 1804648021
after 29 years:
 oak = 1
seaweed = 433305513
after 30 years:
 oak = 1
seaweed = -2128439731
after 31 years:
 oak = 1
seaweed = -2052264063
after 32 years:
 oak = 1
seaweed = -1671385723
after 33 years:
 oak = 1
seaweed = 233005977
after 34 years:
 oak = 1
seaweed = 1165029885
after 35 years:
 oak = 1
seaweed = 1530182129
after 36 years:
 oak = 1
seaweed = -939023947
after 37 years:
 oak = 1
seaweed = -400152439
after 38 years:
 oak = 1
seaweed = -2000762195
after 39 years:
 oak = 1
seaweed = -1413876383
after 40 years:
 oak = 1
seaweed = 1520552677
after 41 years:
 oak = 1
seaweed = -987171207
after 42 years:
 oak = 1
seaweed = -640888739
after 43 years:
 oak = 1
seaweed = 1090523601
after 44 years:
 oak = 1
seaweed = 1157650709
after 45 years:
 oak = 1
seaweed = 1493286249
after 46 years:
 oak = 1
seaweed = -1123503347
after 47 years:
 oak = 1
seaweed = -1322549439
after 48 years:
 oak = 1
seaweed = 1977187397
after 49 years:
 oak = 1
seaweed = 1296002393