簡體   English   中英

C++ 構造函數和析構函數 - 類三角形

[英]C++ Constructors and Destructors - Class Triangle

  1. 創建三角形
  2. Triangle必須具有 as 屬性:
    • 3 邊,3 角,面積;
    • 3 個坐標(Point2D),對應於它在笛卡爾平面上的位置(在某些情況下等於 0)。
  3. Triangle必須具有不同的構造函數,必須在其中計算面積。 它還必須具有訪問方法和其他可能需要的方法。
  4. 使用動態內存分配實現所有三角形成員變量。 在構造函數中分配,在析構函數中釋放(新建和刪除)。
  5. 在 main.cpp 上,實例化對象並存儲在數組或向量中。

這是我到目前為止所擁有的:

三角.hpp:

#ifndef TRIANGLE_HPP
#define TRIANGLE_HPP

#include <iostream>

using namespace std;

class Point2d
{
public:
    int m_x, m_y;
    Point2d() : m_x(0), m_y(0){};
    Point2d(int x, int y) : m_x(x), m_y(y) {};
};

class Triangle
{
private:
    float *m_l1, *m_l2, *m_l3;
    float *m_angle1, *m_angle2, *m_angle3;
    float *m_area;

    void Initialize();

public:
    Triangle();

    Triangle(float l1, float l2, float l3);
    Triangle(float a1, float a2, float a3, float l1);
    Triangle(Point2d pt1, Point2d pt2, Point2d pt3);

    ~Triangle();

    //is_triangle();
};

#endif // TRIANGLE_HPP

三角形.cpp:

#include "triangle.hpp"
#include <cmath>

//Triangle::Triangle() : m_l1(NULL), m_l2(NULL), m_l3(NULL) {}

void Triangle::Initialize()
{
    m_l1 = new float(0);
    m_l2 = new float(0);
    m_l3 = new float(0);
    m_angle1 = new float(0);
    m_angle2 = new float(0);
    m_angle3 = new float(0);
    m_area = new float(0);
}

Triangle::Triangle()
{
    Initialize();
}

//Triangle::Triangle(float l1, float l2, float l3) : m_l1(l1), m_l2(l2), m_l3(l3){}

Triangle::Triangle(float l1, float l2, float l3)
{
    Initialize();
    *m_l1 = l1;
    *m_l2 = l2;
    *m_l3 = l3;

    //need to calculate all three angles and area
}

Triangle::Triangle(float a1, float a2, float a3, float l1)
{
    Initialize();
    *m_l1 = l1;
    *m_angle1 = a1;
    *m_angle2 = a2;
    *m_angle3 = a3;

    //calculate other 2 sides and area
}

Triangle::Triangle(Point2d pt1, Point2d pt2, Point2d pt3)
{
    Initialize();
    *m_l1 = sqrt( (pt2.m_x - pt3.m_x)*(pt2.m_x - pt3.m_x) + (pt2.m_y - pt3.m_y)*(pt2.m_y - pt3.m_y) );
    *m_l2 = sqrt( (pt1.m_x - pt3.m_x)*(pt1.m_x - pt3.m_x) + (pt1.m_y - pt3.m_y)*(pt1.m_y - pt3.m_y) );
    *m_l3 = sqrt( (pt1.m_x - pt2.m_x)*(pt1.m_x - pt2.m_x) + (pt1.m_y - pt2.m_y)*(pt1.m_y - pt2.m_y) );



}

Triangle::~Triangle()
{
    delete m_l1;
    delete m_l2;
    delete m_l3;
    delete m_angle1;
    delete m_angle2;
    delete m_angle3;
    delete m_area;
}

我對如何實現計算和其他所有內容有點迷茫,主要還沒有。 有人可以幫幫我嗎?

您沒有正確初始化類中的指針,如果分配失敗,這將導致析構函數出現問題。 您將delete隨機指針。

您需要使用成員初始化器列表將所有指針設置為 nullptr 或使用new分配內存: Triangle() : m_l1(nullptr), m_l2(new float), ... { } 您對Initialize的使用雖然看起來更整潔,但卻是錯誤的。

或者,您可以使用內聯成員初始化:

class Triangle
{
private:
    float *m_l1{new float()};
...
};

使用()它甚至會初始化為 0。

如果您還減少了單獨分配每個變量的一些瘋狂,那么整個班級的情況如下:

#ifndef TRIANGLE_HPP
#define TRIANGLE_HPP 1

#include <iostream>

// use aggregate initialization `Point2d p{1,2};` so no constructors required
struct Point2d {
    int x{0}, y{0};
};

class Triangle {
private:
    float *l{new float[3]()};
    float *angle{new float[3]()};
    float *area{new float()};

public:
    Triangle();

    Triangle(float l1, float l2, float l3);
    Triangle(float a1, float a2, float a3, float l1);
    Triangle(const Point2d & pt1, const Point2d & pt2, const Point2d & pt3);

    ~Triangle();

    //is_triangle();
};

#endif // TRIANGLE_HPP

#include "triangle.hpp"
#include <cmath>


Triangle::Triangle() { }

Triangle::Triangle(float l1, float l2, float l3)
{
    l[0] = l1;
    l[1] = l2;
    l[2] = l3;
    //need to calculate all three angles and area
}

Triangle::Triangle(float a1, float a2, float a3, float l1)
{
    l[0] = l1;
    angle[0] = a1;
    angle[1] = a2;
    angle[2] = a3;

    //calculate other 2 sides and area
}

Triangle::Triangle(const Point2d & pt1, const Point2d & pt2, const Point2d & pt3)
: Triangle(sqrt( (pt2.x - pt3.x) * (pt2.x - pt3.x) + (pt2.y - pt3.y) * (pt2.y - pt3.y) ),
           sqrt( (pt1.x - pt3.x) * (pt1.x - pt3.x) + (pt1.y - pt3.y) * (pt1.y - pt3.y) ),
           sqrt( (pt1.x - pt2.x) * (pt1.x - pt2.x) + (pt1.y - pt2.y) * (pt1.y - pt2.y) ))
{ }

Triangle::~Triangle()
{
    delete[] l;
    delete[] angle;
    delete area;
}

至於計算缺失的長度、角度或面積是你的作業,最好在https://math.stackexchange.com/上詢問

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM