generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapes.cpp
More file actions
60 lines (55 loc) · 1.85 KB
/
shapes.cpp
File metadata and controls
60 lines (55 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <cmath>
class Shape {
public:
virtual double area() = 0; // pure virtual function
virtual double perimeter() = 0; // pure virtual function
};
class Rectangle : public Shape { // Rectangle as a subclass of Shape
private:
double length; // Attribute
double width; // Attribute
public:
Rectangle(double length, double width) {
this->length = length;
this->width = width;
}
double area() {
return length * width;
}
double perimeter() {
return 2 * (length + width);
}
};
class Circle : public Shape { // Circle as a subclass of Shape
private:
double radius; // Attribute
public:
Circle(double radius) { // Constructor for Circle
this->radius = radius; // this is a pointer to the current object
}
double area() {
return 3.14159 * radius * radius; // Area = πr^2
}
double perimeter() {
return 2 * 3.14159 * radius; // Perimeter = 2πr
}
};
// Right angled triangles only
class Triangle : public Shape { // Triangle as a subclass of Shape
private:
double base; // Attribute
double height; // Attribute
double hypotenuse; // Attribute
public:
Triangle(double base, double height, double hypotenuse) { // Constructor for Triangle
this->base = base; // this is a pointer to the current object
this->height = height; // this is a pointer to the current object
this->hypotenuse = hypotenuse; // this is a pointer to the current object
}
double area() {
return 0.5 * base * height; // Area = 0.5 * base * height
}
double perimeter() {
return base + height + hypotenuse; // Perimeter = base + height + hypotenuse
}
};