0

My code outputs weird numbers. in line "return this->x + this->y + this->z; " it should output all this 3 number sum, but it outputs weird numbers. Expected result: 11

Anyone can help with this?

code:

#include <iostream>
#include <string>


class Point2D {

protected:
    double x,y;

public:

    Point2D() {
        std::cout << "Objekts izveidots bez parametriem." << std::endl;
    }

    void setx(double x) {
        this->x = x;
    }

    void sety(double y) {
        this->y = y;
    }

    double getsumma() {
        return this->x + this->y;
    }

    void printInfo() {
        std::cout << "Divu malu summa ir:" << getsumma() << std::endl;
    }

    ~Point2D() {
        std::cout << "Izdzests" << std::endl;
    }
};

class Point3D : public Point2D {
protected:
    double z;

public:

    Point3D() {
        std::cout << "Objekts izveidots bez parametriem." << std::endl;
    }

    void setz(double z) {
        this->z = z;
    }


    double getperimetrs() {
        return this->x + this->y + this->z;
    }

    void printInfo() {
        std::cout << "Trijstura perimentrs ir: " << getperimetrs() << std::endl;
    }

    ~Point3D() {
        std::cout << "Izdzests" << std::endl;
    }
};

int main() {

    Point2D z1;
    Point3D x1;

    z1.setx(5);
    z1.sety(3);
    x1.setz(5);

    z1.printInfo();
    x1.printInfo();

    system("pause");

}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Cris
  • 17
  • 8
  • 1
    Could you clarify what you mean by moving character from one class to another? – Bo R Feb 23 '19 at 18:04
  • Apart from the unclear question, it's bad practice to have public inheritance between two concrete classes. – Bo R Feb 23 '19 at 18:08
  • 2
  • @LightnessRacesinOrbit Checkout https://stackoverflow.com/questions/16724946/why-derive-from-a-concrete-class-is-a-poor-design – Bo R Feb 23 '19 at 18:20
  • 1
    @BoR Meh, just sounds like a good excuse to force everybody onto poorly-performing and hard to read polymorphic inheritance hierarchies. For nice simple types like this there's really just no need at all. – Lightness Races in Orbit Feb 23 '19 at 18:35
  • Change your constructor to `Point2D() : x{0.0/0.0}, y{0.0/0.0} { std::cout << "Objekts izveidots bez parametriem." << std::endl; }`. – Eljay Feb 23 '19 at 18:59

2 Answers2

3

In x1 you are only setting the Z value. X and Y are in an uninitialized state. Thus you don't know their values are when you add them.

Otherwise, you might be interested in this program:

#include <iostream>

class Point2D {
protected:
    double x, y;
public:
    Point2D(double x, double y) : x(x), y(y) {
        std::cout << "Objekts izveidots bez parametriem.\n";
    }

    double getsumma() const {
        return x + y;
    }

    virtual void printInfo() const {
        std::cout << "Divu malu summa ir: " << getsumma() << '\n';
    }

    virtual ~Point2D() {
        std::cout << "Izdzests\n";
    }
};

class Point3D : public Point2D {
protected:
    double z;

public:
    Point3D(double x, double y, double z) : Point2D(x, y), z(z) {
        std::cout << "Objekts izveidots bez parametriem.\n";
    }

    double getperimetrs() const {
        return getsumma() + z;
    }

    void printInfo() const override {
        std::cout << "Trijstura perimentrs ir: " << getperimetrs() << '\n';
    }

    ~Point3D() override {
        std::cout << "Izdzests\n";
    }

};

int main() {

    Point2D z1(5, 3);
    Point3D x1(5, 3, 5);

    z1.printInfo();
    x1.printInfo();
}
Bo R
  • 2,334
  • 1
  • 9
  • 17
3

This is not how inheritance works.

You have two different, distinct, unrelated, separate objects:

  • One is a Point2D called z1, whose members x and y you set.
  • The other is a Point3D called x1, whose member z you set.

Just because the type Point3D inherits from Point2D doesn't mean that there is any relationship between these two particular instances.

z1's z is not set, and x1's x and y are not set. Those members are left uninitialised, with unspecified values, hence your dodgy output.

I think you probably just meant to make one object, like this:

Point3D obj;
obj.setx(5);
obj.sety(3);
obj.setz(5);
obj.printInfo();

The output is 13 (5+3+5 is not 11).

Inheritance is how you can do setx and sety on a Point3D, because those functions are inherited from its Point2D base.

Another way of looking at it is that every Point3D has a "hidden" Point2D inside it automatically (by inheritance). You don't need to make one yourself.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055