小编Jos*_*pez的帖子

我必须实例化这个类吗?或者这是一个错误的责任分离?

我正在练习课程,我创建了一个程序来创建一个矢量并计算它的大小.我将代码分开,以便一个类是一个矢量类,另一个类处理数学.我的问题是,这是不正确的?我必须实例化我的数学课才能够使用它,它看起来很笨,即使它有效.

主要:

int main()
{
    // Instantiate math library... ?
    Math math; // <-- Instantiating math library here
    Vector3D *test = new Vector3D("Test vector 1", 0, 0, 1);
    printVector(test);

    // Calling math library to calculate magnitude
    std::cout << "Test vector 1 magnitude: " << math.magnitude(test) << std::endl;


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

矢量类:

class Vector3D {
    private:
        std::string     name;
        double          x;
        double          y;
        double          z;

    public:
        Vector3D();
        Vector3D(std::string, double, double, double);

        // Setters
        void setName(std::string);
        void setX(double);
        void setY(double);
        void setZ(double);

        // …
Run Code Online (Sandbox Code Playgroud)

c++ oop

3
推荐指数
2
解决办法
125
查看次数

标签 统计

c++ ×1

oop ×1