Skip to content

Instantly share code, notes, and snippets.

@axsddlr
Created May 19, 2018 03:35
Show Gist options
  • Save axsddlr/35e621d37258f6ecce08b89b8c8693b4 to your computer and use it in GitHub Desktop.
Save axsddlr/35e621d37258f6ecce08b89b8c8693b4 to your computer and use it in GitHub Desktop.
Project 2 - C++ created by Ayysir - https://repl.it/@Ayysir/Project-2-C
#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(3.5, 4.5));
shapes.push_back(new Circle(2));
shapes.push_back(new Triangle(4, 5, 6));
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