0

I created a class Elevator with the public, private and protected variables. Also, I wrote some methods. Now a question how can I simplify these lines of code with objects announcement.

int main()
{
    Elevator ElevatorObject("NAME", 2, 3, 4, 5);
    ElevatorObject.get_name();
    ElevatorObject.get_loadCapacity();
    ElevatorObject.get_MotorPower();
    ElevatorObject.get_height();
    ElevatorObject.get_witdth();

    Elevator ElevatorObject1("NONAME", 5, 4, 3, 2);
    ElevatorObject1.get_name();
    ElevatorObject1.get_loadCapacity();
    ElevatorObject1.get_MotorPower();
    ElevatorObject1.get_height();
    ElevatorObject1.get_witdth();

    system("pause");
    return 0;
}

I am thinking to put all needed names of objects into a string array and then realize it in a loop. But then there will be the same values in object parameters.

Like this.

string ObjectName[3] = { "ElevatorObject1", "ElevatorObject2", "ElevatorObject3" };

int main()
{
    string ObjectName[3] = { "ElevatorObject1", "ElevatorObject2", "ElevatorObject3" };

    for (int i = 0; i < 3; i++) 
    {
        Elevator ObjectName[i]("NEMA", 2, 3, 4, 5);
        ObjectName[i].get_name();
        ObjectName[i].get_loadCapacity();
        ObjectName[i].get_MotorPower();
        ObjectName[i].get_height();
        ObjectName[i].get_witdth();
    }

    system("pause");
    return 0;
}

There is an error "C2131 the expression must have a constant value". Maybe I made a mistake somewhere...

2
  • Elevator ObjectName[i]("NEMA", 2, 3, 4, 5); this means you are trying to create a new array each loop of size i Commented Oct 23, 2018 at 15:23
  • ElevatorObject.get_name(); is strange. do you mean print_name(), input_name() ? Commented Oct 23, 2018 at 15:34

1 Answer 1

1

This line makes no sense:

Elevator ObjectName[i]("NEMA", 2, 3, 4, 5);

You're calling as if you mean something like this:

Elevator "ElevatorObject1"("NEMA", 2, 3, 4, 5);

Which is obviously not going to work. Instead you probably mean to supply the name:

Elevator elevator(ObjectName[i], 2, 3, 4, 5);

The name of the variable created should be something else entirely.

You probably mean to create a std::vector or std::array you can push_back into each time you create a new Elevator object.

Sign up to request clarification or add additional context in comments.

1 Comment

@JohnnyMopp Fixed. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.