Advertisement
irmantas_radavicius

Untitled

Apr 25th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Shape {
  6. public:
  7. virtual double getArea() = 0;
  8. };
  9. class Square : public Shape {
  10. private:
  11. double a;
  12. public:
  13. Square(double a){
  14. this->a = a;
  15. }
  16. virtual double getArea(){
  17. return a*a;
  18. }
  19. };
  20. class Circle : public Shape {
  21. private:
  22. double r;
  23. public:
  24. Circle(double r){
  25. this->r = r;
  26. }
  27. virtual double getArea(){
  28. return 3.14159265358979323846*r*r;
  29. }
  30. };
  31.  
  32. void doSomething(Shape &r){
  33. cout << "We will compute the area" << endl;
  34. cout << "The area is " << r.getArea() << endl;
  35. }
  36.  
  37.  
  38. int main(){
  39.  
  40. cout << "Let's compute area." << endl;
  41.  
  42. Shape *s = NULL;
  43.  
  44. cout << "1 for square, 0 for circle?" << endl;
  45. int x;
  46. cin >> x;
  47.  
  48. if(x == 1){
  49. cout << "Great. Please enter edge length: " << endl;
  50. double edge;
  51. cin >> edge;
  52. s = new Square(edge);
  53. } else if(x == 0){
  54. cout << "Great. Please enter radius length: " << endl;
  55. double radius;
  56. cin >> radius;
  57. s = new Circle(radius);
  58. }
  59.  
  60. if(s != NULL){
  61. doSomething(*s);
  62. delete s;
  63. }
  64.  
  65. cout << "Good bye" << endl;
  66. return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement