0

I made two constructors in Vector class.

public:
    Vector() {
        x = 0;
        y = 0;
        z = 0;
    }
    Vector(int _x, int _y, int _z) {
        x = _x;
        y = _y;
        z = _z;
    }

and I created an Add function inside the Vector class. This function will add two vectors.

    Vector Add(Vector v) {
        Vector output;
        output.x = x + v.x;
        output.y = y + v.y;
        output.z = z + v.z;
        return output;
    }

In the main function, I would like to add vectorA(1, 2, 3) and VectorB(4, 5, 6) to get the value of vectorO(5, 7, 9).

int main()
{
    Vector vectorA(1, 2, 3);
    Vector vectorB(4, 5, 6);
    Vector vectorO = vectorA.Add(vectorB);

    printf("(%d, %d, %d)\n", vectorO.x, vectorO.y, vectorO.z); // (5, 7, 9)


    return 0;
}

However, only abnormal values are printed, such as (0, 1075052544, 0). There is no error message, is there a solution?

I don't think it's the constructor's problem, should I fix the main function?

Edit point

I cut off the code to make it readable. However, the code lacked information, so I put the entire code in it.

#include<stdio.h>

class Vector
{
public: // private?
    double x, y, z;

public:
    Vector() {
        x = 0;
        y = 0;
        z = 0;
    }
    Vector(int _x, int _y, int _z) {
        x = _x;
        y = _y;
        z = _z;
    }
    double Magnitude(void);

    Vector Add(Vector v) {
        Vector output;
        output.x = x + v.x;
        output.y = y + v.y;
        output.z = z + v.z;
        return output;
    }

    Vector Subract(Vector v);
    double DotProduct(Vector v);
    Vector CrossProduct(Vector v);
};

int main()
{
    Vector vectorA(1, 2, 3);
    Vector vectorB(4, 5, 6);
    Vector vectorO = vectorA.Add(vectorB);

    printf("(%d, %d, %d)\n", vectorO.x, vectorO.y, vectorO.z); // (5, 7, 9)


    return 0;
}
Keastmin
  • 85
  • 6

0 Answers0