Created
          May 19, 2018 03:38 
        
      - 
      
 - 
        
Save axsddlr/908c1572802ba597b4d844cb51737969 to your computer and use it in GitHub Desktop.  
    Project 2 - C++ created by Ayysir - https://repl.it/@Ayysir/Project-2-C
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <iostream> | |
| #include <string> | |
| #include <vector> | |
| #include <math.h> | |
| class Shape { | |
| public: | |
| virtual float calculateArea() = 0; | |
| virtual ~Shape(){}; | |
| std::string name; | |
| }; | |
| class Rectangle : public Shape { | |
| private: | |
| float width, height; | |
| public: | |
| Rectangle() {} | |
| Rectangle(float w, float h) { | |
| width = w; | |
| height = h; | |
| name = "Rectangle"; | |
| } | |
| float calculateArea() { | |
| float area; | |
| area = width * height; | |
| return area; | |
| } | |
| }; | |
| class Circle : public Shape { | |
| private: | |
| float radius; | |
| public: | |
| Circle() {} | |
| Circle(float r) { | |
| radius = r; | |
| name = "Circle"; | |
| } | |
| float calculateArea() { | |
| float area; | |
| area = 3.141 * radius * radius; | |
| return area; | |
| } | |
| }; | |
| class Triangle : public Shape { | |
| private: | |
| float side1, side2, side3; | |
| public: | |
| Triangle() {} | |
| Triangle(float s1, float s2, float s3) { | |
| side1 = s1; | |
| side2 = s2; | |
| side3 = s3; | |
| name = "Triangle"; | |
| } | |
| float calculateArea() { | |
| float area; | |
| float s; | |
| s = (side1 + side2 + side3) / 2; | |
| area = sqrt(s * (s - side1) * (s - side2) * (s - side3)); | |
| return area; | |
| } | |
| }; | |
| int main() { | |
| std::vector<Shape*> shapes; | |
| shapes.push_back(new Rectangle(6, 2.5)); | |
| shapes.push_back(new Circle(4)); | |
| shapes.push_back(new Triangle(7, 8, 9)); | |
| for (int i = 0; i < shapes.size(); i++) { | |
| std::cout << "area of " << shapes[i]->name << " is " | |
| << shapes[i]->calculateArea() << std::endl; | |
| } | |
| for (int i = 0; i < shapes.size(); i++) { | |
| delete shapes[i]; | |
| } | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment