Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing an OOP exercise and I have a class called Room with a vector, which stores the objects from classes Windows and Doors. I try to access the values stored in the objects but the output I get is this:

window added, vector size = 1
window added, vector size = 2
door added, vector size = 1
door added, vector size = 2
square window height = 8.99945e-039
Backdoor height = 2.13405e+033


The values printed are not the same I specified in the main function. What am I doing wrong here?

What I have tried:

using namespace std;

#include <iostream>
#include <vector>

class Window{
    private:
        float height;
        float width;
    public:
        void setH(float h) {height = h;}
        void setW(float w) {width = w;}
        float getH() {return height;}
        float getW() {return width;}
};

class Door{
    private:
        float height;
        float width;
    public:
        void setH(float h) {height = h;}
        void setW(float w) {width = w;}
        float getH() {return height;}
        float getW() {return width;}
};

class Room{
    private:
        float height;
        float width;
        float length;

    public:
        void setH(float h) {height = h;}
        void setW(float w) {width = w;}
        void setL(float l) {length = l;}
        float getH() {return height;}
        float getW() {return width;}
        float getL() {return length;}
        void addWindow(Window langas1);
        void addDoor(Door durys1);
        float getWallArea();
        vector<Window> windowsVec;
        vector<Door> doorsVec;
};

void Room::addWindow(Window window1){
    windowsVec.push_back(window1);
    cout << "window added, vector size = " << windowsVec.size() << endl;
}
void Room::addDoor(Door door1){
    doorsVec.push_back(door1);
    cout << "door added, vector size = " << doorsVec.size() << endl;
}

int main(){

    Room LivingRoom;
    Window Square;
    Window Rectangular;
    Door Frontdoor;
    Door Backdoor;

    LivingRoom.addWindow(Square);
    LivingRoom.addWindow(Rectangular);
    LivingRoom.addDoor(Frontdoor);
    LivingRoom.addDoor(Backdoor);

    LivingRoom.setH(300);
    LivingRoom.setL(8000);
    LivingRoom.setW(6000);

    Square.setH(50);
    Square.setW(50);
    Rectangular.setH(60);
    Rectangular.setH(40);
    Frontdoor.setH(200);
    Frontdoor.setW(90);
    Backdoor.setH(180);
    Backdoor.setW(70);

    cout << "square window height = " << LivingRoom.windowsVec.at(0).getH() << endl;
    cout << "frontdoor height = " << LivingRoom.doorsVec.at(1).getH() << endl;

    return 0;
}
Posted
Updated 16-Feb-21 6:48am
v2
Comments
Richard MacCutchan 16-Feb-21 12:51pm    
You did not set any dimensions for the doors and windows added to the LivingRoom.
Rick York 16-Feb-21 13:37pm    
Did you notice that the Door and Window classes are identical except for their names? This where a base class becomes very useful. In this case, it could contain all of those members and methods and then you can make specialized classes for the Door and Window that derive from it. In this case, they would only be used for the purpose of identifying the type of the object because they have no differentiating properties.

1 solution

When you push_back a door into the vector, it copies that door's values into a door that exists in the vector's container. Later, you set the door's size. But that door is a local variable, not the one inside the vector, and so the one in the vector still has the random values that it had when it was added to the vector.

Try setting the door's attributes before adding it to the vector.
 
Share this answer
 
v2
Comments
[no name] 16-Feb-21 12:53pm    
5. So in c++ it is recommendable to initialize members in (all) constructors and implement at least a copy constructor, right?
Greg Utas 16-Feb-21 13:39pm    
It is definitely advisable to initialize all members in a constructor. A member that is a class will be initialized automatically, but other members must be explicitly initialized.

A copy constructor is appropriate unless the class wants to prohibit copying its objects.

In some cases, the default version of a constructor or copy constructor is adequate, but as of C++11 this can be explicitly noted by declaring it using "Class() = default;". And if you want to prohibit copying, declare the copy constructor using "Class(const Class& that) = delete;".
k5054 16-Feb-21 13:00pm    
There is also a copy of the Window object created in the call to Room::AddWindow(). So by the object that vector::push_back() adds to the vector is a copy of the copy.
Member 14955513 16-Feb-21 13:10pm    
Thank you, now it works. Is there a way to make it work with setting the values after adding it to the vector? I can imagine it should get quite confusing in a bigger scale programs.
Rick York 16-Feb-21 13:33pm    
Sure. You just need to know where in the vector the object is so you access the correct one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900