Advertisement
irmantas_radavicius

Untitled

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