jezvgg vor 11 Monaten
Ursprung
Commit
338241e1dd
5 geänderte Dateien mit 81 neuen und 0 gelöschten Zeilen
  1. 8 0
      Figure.h
  2. 22 0
      Triangle.cpp
  3. 30 0
      Triangle.h
  4. 9 0
      makefile
  5. 12 0
      triangle_main.cpp

+ 8 - 0
Figure.h

@@ -1,3 +1,5 @@
+#include <math.h>
+
 struct Point
 {
     double x, y;
@@ -6,6 +8,12 @@ struct Point
 
 class Figure
 {
+    protected:
+    double l2(double x1, double y1, double x2, double y2)
+    {
+        return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); 
+    }
+
     public:
     virtual double calc_area() = 0;
     virtual double calc_perimiter() = 0;

+ 22 - 0
Triangle.cpp

@@ -0,0 +1,22 @@
+#include "Triangle.h"
+#include <iostream>
+
+Triangle::Triangle(struct Point f, struct Point s, struct Point t)
+{
+    init(f.x, f.y, s.x, s.y, t.x, t.y);
+}
+
+double Triangle::calc_area()
+{
+    return 0.5 * abs((x2 - x1)*(y3-y1)-(x3-x1)*(y2-y1));
+}
+
+double Triangle::calc_perimiter()
+{
+    return l2(x1, y1, x2, y2) + l2(x3, y3, x2, y2) + l2(x1, y1, x3, y3);
+}
+
+void Triangle::name()
+{
+    std::cout << "Triangle" << std::endl;
+}

+ 30 - 0
Triangle.h

@@ -0,0 +1,30 @@
+#include "Figure.h"
+
+class Triangle: public Figure
+{
+    private:
+    double x1, y1, x2, y2, x3, y3;
+
+    template <typename T>
+    void init(T X1, T Y1, T X2, T Y2, T X3, T Y3)
+    {
+        x1 = double(X1);
+        y1 = double(Y1);
+        x2 = double(X2);
+        y2 = double(Y2);
+        x3 = double(X3);
+        y3 = double(Y3);
+    }
+
+    public:
+    template <typename T>
+    Triangle(T X1, T Y1, T X2, T Y2, T X3, T Y3)
+    {
+        init(X1, Y1, X2, Y2, X3, Y3);
+    }
+    Triangle(struct Point f, struct Point s, struct Point t);
+
+    double calc_area();
+    double calc_perimiter();
+    void name();
+};

+ 9 - 0
makefile

@@ -7,5 +7,14 @@ circle_main.o: circle_main.cpp
 Circle.o: circle_main.cpp
 	g++ -c Circle.cpp
 
+triangle: Triangle.o triangle_main.o
+	g++ Triangle.o triangle_main.o
+
+triangle_main.o: triangle_main.cpp
+	g++ -c triangle_main.cpp
+
+Triangle.o: Triangle.cpp
+	g++ -c Triangle.cpp
+
 clean:
 	rm -rf *.o all

+ 12 - 0
triangle_main.cpp

@@ -0,0 +1,12 @@
+#include "Triangle.h"
+#include <iostream>
+
+using namespace std;
+
+int main(){
+    Triangle t(0, 0, 1, 1, 1, 0);
+    cout << t.calc_area() << endl;
+    cout << t.calc_perimiter() << endl;
+    t.name();
+    return 0;
+}