Advertisement
irmantas_radavicius

Untitled

Apr 23rd, 2024
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 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.  
  39. int main(){
  40.  
  41.     cout << "Let's compute area." << endl;
  42.  
  43.     Shape *s = NULL;
  44.  
  45.     cout << "1 for square, 0 for circle?" << endl;
  46.     int x;
  47.     cin >> x;
  48.  
  49.     if(x == 1){
  50.         cout << "Great. Please enter edge length: " << endl;
  51.         double edge;
  52.         cin >> edge;
  53.         s = new Square(edge);
  54.     } else if(x == 0){
  55.         cout << "Great. Please enter radius length: " << endl;
  56.         double radius;
  57.         cin >> radius;
  58.         s = new Circle(radius);
  59.     }
  60.  
  61.     if (s != NULL){
  62.         cout << "We will compute the area" << endl;
  63.         cout << "The area is " << s->getArea() << endl;
  64.         delete s;
  65.     }
  66.  
  67.     cout << "Good bye" << endl;
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement