Skip to content

Commit 3f9603b

Browse files
committed
Adding examples
1 parent 6e07d8b commit 3f9603b

14 files changed

+868
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <vector>
4+
5+
class Shape
6+
{
7+
public:
8+
enum class Type
9+
{
10+
RECTANGLE,
11+
TRIANGLE,
12+
CIRCLE
13+
};
14+
15+
Shape() = default;
16+
17+
virtual double getArea() const = 0;
18+
virtual Shape *clone() const = 0;
19+
virtual Type getType() const = 0;
20+
21+
virtual ~Shape() = default;
22+
};
23+
24+
class Rectangle : public Shape
25+
{
26+
public:
27+
Rectangle() = default;
28+
Rectangle(const double &width, const double &height) : width(width), height(height) {}
29+
30+
double getWidth() const
31+
{
32+
return this->width;
33+
}
34+
35+
double getHeight() const
36+
{
37+
return this->height;
38+
}
39+
double getArea() const override
40+
{
41+
return this->width * height;
42+
}
43+
44+
Shape *clone() const override
45+
{
46+
return new Rectangle(*this);
47+
}
48+
49+
Type getType() const override
50+
{
51+
return Type::RECTANGLE;
52+
}
53+
54+
private:
55+
double width;
56+
double height;
57+
};
58+
59+
class Triangle : public Shape
60+
{
61+
public:
62+
Triangle() = default;
63+
Triangle(const double &a, const double &b, const double &c) : a(a), b(b), c(c) {}
64+
65+
double getA() const
66+
{
67+
return this->a;
68+
}
69+
70+
double getB() const
71+
{
72+
return this->b;
73+
}
74+
75+
double getC() const
76+
{
77+
return this->c;
78+
}
79+
80+
double getArea() const override
81+
{
82+
double p = (a + b + c) / 2;
83+
return sqrt(p * (p - a) * (p - b) * (p - c));
84+
}
85+
86+
Shape *clone() const override
87+
{
88+
return new Triangle(*this);
89+
}
90+
91+
Type getType() const override
92+
{
93+
return Type::TRIANGLE;
94+
}
95+
96+
private:
97+
double a;
98+
double b;
99+
double c;
100+
};
101+
102+
class Circle : public Shape
103+
{
104+
public:
105+
const double PI = 3.14;
106+
Circle(const double &radius = 0) : radius(radius) {}
107+
108+
double getRadius() const
109+
{
110+
return this->radius;
111+
}
112+
double getArea() const override
113+
{
114+
return PI * radius * radius;
115+
}
116+
117+
Shape *clone() const override
118+
{
119+
return new Circle(*this);
120+
}
121+
122+
Type getType() const override
123+
{
124+
return Type::CIRCLE;
125+
}
126+
127+
private:
128+
double radius;
129+
};
130+
131+
class Shapes
132+
{
133+
private:
134+
std::vector<Shape *> shapes;
135+
136+
public:
137+
Shapes() = default;
138+
139+
void add(const Shape *const shape)
140+
{
141+
this->shapes.push_back(shape->clone());
142+
}
143+
144+
Shape *operator[](const int index) const
145+
{
146+
return this->shapes[index];
147+
}
148+
149+
unsigned size() const
150+
{
151+
return this->shapes.size();
152+
}
153+
154+
~Shapes()
155+
{
156+
for (auto *shape : shapes)
157+
{
158+
delete shape;
159+
}
160+
}
161+
};
162+
163+
int main()
164+
{
165+
Shapes shapes;
166+
167+
Rectangle rectangle(3, 5);
168+
shapes.add(&rectangle);
169+
170+
Triangle triangle(3, 4, 5);
171+
shapes.add(&triangle);
172+
173+
Circle circle(5);
174+
shapes.add(&circle);
175+
176+
for (int i = 0; i < shapes.size(); i++)
177+
{
178+
// bad practise, this is not a polymorphic code
179+
if (shapes[i]->getType() == Shape::Type::RECTANGLE)
180+
{
181+
std::cout << "This rectangle's area is: ";
182+
}
183+
else if (shapes[i]->getType() == Shape::Type::TRIANGLE)
184+
{
185+
std::cout << "This triangle's area is: ";
186+
187+
Triangle *asTriangle = (Triangle *)shapes[i];
188+
std::cout << asTriangle->getA();
189+
}
190+
else if (shapes[i]->getType() == Shape::Type::CIRCLE)
191+
{
192+
std::cout << "This circle's area is: ";
193+
}
194+
std::cout << shapes[i]->getArea() << std::endl;
195+
}
196+
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
class Vehicle
5+
{
6+
private:
7+
char *make;
8+
char *model;
9+
int horsepower;
10+
11+
public:
12+
Vehicle() {}
13+
Vehicle(const Vehicle &objectToCopyFrom) {}
14+
Vehicle &operator=(const Vehicle &objectToCopyFrom) {}
15+
const char *getMake() const {}
16+
const char *getModel() const {}
17+
const int getHorsepower() const {}
18+
virtual void honk()
19+
{
20+
std::cout << "A vehicle honking\n";
21+
}
22+
virtual ~Vehicle() {}
23+
};
24+
25+
class Car : public Vehicle
26+
{
27+
private:
28+
int seats;
29+
30+
public:
31+
Car() : seats{1} {}
32+
void honk()
33+
{
34+
std::cout << "A car honking\n";
35+
}
36+
const int getSeats() const {}
37+
};
38+
39+
class Motorcycle : public Vehicle
40+
{
41+
private:
42+
int capacityLuggage;
43+
44+
public:
45+
Motorcycle() : capacityLuggage{0} {}
46+
const int getCapacity() const {}
47+
void honk()
48+
{
49+
std::cout << "A motorcycle has no horn\n";
50+
}
51+
};
52+
53+
int main()
54+
{
55+
Vehicle v;
56+
Car c;
57+
Motorcycle m;
58+
v.honk();
59+
c.honk();
60+
m.honk();
61+
Vehicle *v1 = new Motorcycle(), *v2 = new Car();
62+
v1->honk();
63+
v2->honk();
64+
// std::vector<Vehicle *> vehicles{v1, v2, &v};
65+
// for (size_t i = 0; i < vehicles.size(); i++)
66+
// {
67+
// vehicles[i]->honk();
68+
// }
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "Circle.h"
2+
3+
const double PI = 3.1415;
4+
5+
Circle::Circle(int x, int y, double radius): Shape(1), radius(radius){
6+
SetPoint(0, x, y);
7+
}
8+
9+
double Circle::GetArea() const{
10+
return PI * radius * radius;
11+
}
12+
13+
double Circle::GetPer() const{
14+
return 2 * PI * radius;
15+
}
16+
17+
bool Circle::IsPointIn(int x, int y) const{
18+
return GetPointAtIndex(0).GetDistance(Shape::Point(x, y)) <= radius;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __CIRCLE_H_
2+
#define __CIRCLE_H_
3+
4+
#include "Shape.h"
5+
6+
class Circle : public Shape{
7+
8+
public:
9+
Circle(int x, int y, double radius);
10+
11+
double GetArea() const override;
12+
double GetPer() const override;
13+
bool IsPointIn(int x, int y) const override;
14+
15+
private:
16+
double radius;
17+
18+
};
19+
20+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "Rectangle.h"
2+
3+
Rectangle::Rectangle(int x1, int y1, int x3, int y3): Shape(4){
4+
5+
SetPoint(0, x1, y1);
6+
SetPoint(1, x1, y3);
7+
SetPoint(2, x3, y3);
8+
SetPoint(3, x3, y1);
9+
10+
}
11+
12+
double Rectangle::GetArea() const{
13+
14+
Shape::Point p0 = GetPointAtIndex(0);
15+
Shape::Point p1 = GetPointAtIndex(1);
16+
Shape::Point p3 = GetPointAtIndex(3);
17+
18+
return p0.GetDistance(p1) * p0.GetDistance(p3);
19+
20+
}
21+
22+
double Rectangle::GetPer() const{
23+
24+
Shape::Point p0 = GetPointAtIndex(0);
25+
Shape::Point p1 = GetPointAtIndex(1);
26+
Shape::Point p3 = GetPointAtIndex(3);
27+
28+
return 2 * (p0.GetDistance(p1) + p0.GetDistance(p3));
29+
30+
}
31+
32+
bool Rectangle::IsPointIn(int x, int y) const{
33+
34+
Shape::Point p(x, y);
35+
return p.x >= GetPointAtIndex(0).x && p.y >= GetPointAtIndex(1).x &&
36+
p.y <= GetPointAtIndex(0).y && p.y >= GetPointAtIndex(2).y;
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef __RECTANGLE_H_
2+
#define __RECTANGLE_H_
3+
4+
#include "Shape.h"
5+
6+
class Rectangle : public Shape{
7+
8+
public:
9+
Rectangle(int x1, int y1, int x3, int y3);
10+
double GetArea() const override;
11+
double GetPer() const override;
12+
bool IsPointIn(int x, int y) const override;
13+
14+
};
15+
16+
#endif

0 commit comments

Comments
 (0)