0

I have a class 3D point velocity which inherits from the 3D point and 3D velocity (not very well thought ). Just testing C++ concepts. The issue is that when I print the values from the sum, it prints out the garbage value. Not sure why I have added the logs in the functions. Values look good, but still, the sum is printing invalid values. Code:

//
// Created by xyz on 8/28/2022.
//

#include <iostream>

class my3Point {
public:
    int _x, _y, _z;
    my3Point() : _x(0), _y(0), _z(0) {}
    my3Point(int a, int b, int c) : _x(a), _y(b), _z(c) {
        // std::cout<<"x: "<<a<<", y: "<<b<<", z: "<<c<<std::endl;
        // std::cout<<"x: "<<_x<<", y: "<<_y<<", z: "<<_z<<std::endl;
    }
    my3Point(my3Point &rhs){
        _x = rhs._x; _y = rhs._y; _z = rhs._z;
    }
    my3Point& operator+(my3Point&rhs){
        std::cout<<"Inside the addition of my 3D point"<<std::endl;
        _x += rhs._x;
        _y += rhs._y;
        _z += rhs._z;
        std::cout<<"x: "<<_x<<", y: "<<_y<<", z: "<<_z<<std::endl;
    }
};

class my3Velocity {
public:
    int _vx, _vy, _vz;
    my3Velocity() : _vx(0), _vy(0), _vz(0) {}
    my3Velocity(int a, int b, int c) : _vx(a), _vy(b), _vz(c) {
        //std::cout<<"x: "<<_vx<<", y: "<<_vy<<", z: "<<_vz<<std::endl;
    }
    my3Velocity(my3Velocity &rhs){
        _vx = rhs._vx; _vy = rhs._vy; _vz = rhs._vz;
    }
    my3Velocity& operator+(my3Velocity&rhs){
        std::cout<<"Inside the addition of my 3D Velocity"<<std::endl;
        _vx += rhs._vx;
        _vy += rhs._vy;
        _vz += rhs._vz;
        std::cout<<"x: "<<_vx<<", y: "<<_vy<<", z: "<<_vz<<std::endl;
    }
};

class my3VelocityPoint: public my3Point, public my3Velocity {
public:
    int acc; //accerlation
    my3VelocityPoint() : my3Point(), my3Velocity(), acc(0) {}
    my3VelocityPoint(int a1, int a2, int a3, int b1, int b2, int b3, int c) : my3Point(a1, a2, a3), my3Velocity(b1, b2, b3), acc(c) {}
    my3VelocityPoint& operator+(my3VelocityPoint&rhs){
        my3Point::operator+(rhs);
        my3Velocity::operator+(rhs);
        acc += rhs.acc;
        std::cout<<"Inside the addition of my 3D velocity point"<<std::endl;
    }
    my3VelocityPoint& operator=(my3VelocityPoint &rhs){
        if (this == &rhs){
            return *this;
        }
        _x = rhs._x; _y = rhs._y; _z = rhs._z;
        _vx = rhs._vx; _vy = rhs._vy; _vz = rhs._vz;
        acc = rhs.acc;
        std::cout<<"Inside operator = "<<std::endl;
    }
    void print();
};

void my3VelocityPoint::print() {
    std::cout<<"Position: \n";
    std::cout<<"x: "<<_x<<", y: "<<_y<<", z: "<<_z<<std::endl;
    std::cout<<"Velocity: \n";
    std::cout<<"x: "<<_vx<<", y: "<<_vy<<", z: "<<_vz<<std::endl;
    std::cout<<"Accerlation: \n";
    std::cout<<"x: "<<acc<<std::endl;
}


int main()
{
    /*
    my3Point myPoint;
    my3Velocity myVelocity;
    my3VelocityPoint myVelPoint;
    std::cout<<"SIZES: "<<std::endl;
    std::cout<<"3D Point: "<< sizeof(my3Point)<<std::endl;
    std::cout<<"3D Velocity: "<< sizeof(my3Velocity)<<std::endl;
    std::cout<<"3D Velocity Point: "<< sizeof(my3VelocityPoint)<<std::endl;

    std::cout<<"ADDRESSES: "<<std::endl;
    std::cout<<"3D Point: "<< &myPoint<<std::endl;
    std::cout<<"3D Velocity: "<< &myVelocity<<std::endl;
    std::cout<<"3D Velocity Point: "<< &myVelPoint<<std::endl;
    std::cout<<"Vel x: "<<&(myVelPoint._x)<<", y "<<&(myVelPoint._y)<<", z: "<<&(myVelPoint._z)<<std::endl;
    std::cout<<"Vel vx: "<<&(myVelPoint._vx)<<", vy "<<&(myVelPoint._vy)<<", vz: "<<&(myVelPoint._vz)<<std::endl;
     */

    my3VelocityPoint myVelPoint1(0,0,0, 1, 1, 1, 5), myVelPoint2(3,0, 0, 3, 4, 5, 10);
    myVelPoint1.print(); myVelPoint2.print();
    my3VelocityPoint sum;
    // sum.print();
    sum = myVelPoint1 + myVelPoint2;
    sum.print();
    /*
    void* myAddr = &myPoint;
    std::cout<<"Address of class: "<<myAddr<<std::endl;
    std::cout<<"Address of class: "<<&(myPoint._y)<<std::endl;
     */

    return 0;
}
  • 1
    These `operator+`s are completely broken. When you have any two objects, `a=b+c;` is not expected to modify either b or c; yet, `b.operator+`, if `b` is your `operator+`, it will end up modifying `this`, or `b`. Surprise! Additionally, despite declared as returning a reference, these `operator+s` return absolutely nothing, whatsoever. This results in undefined behavior. See the linked question for a comprehensive overview of operator overloading in C++. – Sam Varshavchik Aug 29 '22 at 03:01
  • You are so right!!!! I think I might be low on my sugar level! – harrySherlock Aug 29 '22 at 03:37
  • It's slightly puzzling that you decided to use the initializer list for all constructors except the copy constructors. The copy constructors are also completely unnecessary, as they only reproduce what the default copy would do, and so is your `my3VelocityPoint::operator=`. – molbdnilo Aug 29 '22 at 08:19

0 Answers0