fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class shape
  5. {
  6. public:
  7. shape()
  8. {
  9. cout<<"shape constructed"<<endl;
  10. }
  11. virtual double getarea()
  12. {
  13. return 0;
  14. }
  15. virtual ~shape()
  16. {
  17. cout<< "shape destroyed"<<endl;
  18. }
  19. };
  20.  
  21. class circle:public shape
  22. {
  23. int rad;
  24. public:
  25. circle(int r)
  26. {
  27. rad=r;
  28. cout<< "circle constructed"<<endl;
  29. }
  30. double getarea()
  31. {
  32. double ar=3.14*rad*rad;
  33. //cout<< "circle area: "<<ar<<endl;
  34. return ar;
  35. }
  36. ~circle()
  37. {
  38. cout<< "circle destroyed"<<endl;
  39. }
  40. };
  41.  
  42. class rect:public shape
  43. {
  44. int b,h;
  45. public:
  46. rect(int bs,int ht)
  47. {
  48. b=bs;
  49. h=ht;
  50. cout<< "rectangle constructed"<<endl;
  51. }
  52. double getarea()
  53. {
  54. double ar=b*h;
  55. //cout<< "rect area: "<<ar<<endl;
  56. return ar;
  57. }
  58. ~rect()
  59. {
  60. cout<< "rectangle destroyed"<<endl;
  61. }
  62. };
  63.  
  64. int main()
  65. {
  66. int r;
  67. cout<< "radius: ";
  68. cin>>r;
  69.  
  70. shape *s1=new circle(r);
  71.  
  72. int a,b;
  73. cout<< "base and height: ";
  74. cin>>a>>b;
  75. shape *s2=new rect(a,b);
  76.  
  77. cout<<"rect: "<< s2->getarea()<<endl;
  78. cout<< "circle: "<<s1->getarea()<<endl;
  79. delete s1;
  80.  
  81.  
  82. delete s2;
  83.  
  84.  
  85. }
  86.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
radius: shape constructed
circle constructed
base and height: shape constructed
rectangle constructed
rect: 1.50874e+09
circle: 0
circle destroyed
shape destroyed
rectangle destroyed
shape destroyed