-3

I am a newbie in C++. I have a project to create a library for my robot. Basically, the main class is ROBOT, then the second levels are: LIGHT, MOTOR. Then, for the LIGHT level, there are the methods: Set_ON_OFF, Set_color, Set_brightness. For the MOTOR level, there are the methods: Set_right_left, Set_power, Set_move_back. So the multi-level class ROBOT is organized like:

ROBOT

  1. LIGHT
    • Set_ON_OFF
    • Set_color
    • Set_brightness
  2. MOTOR
    • Set_right_left
    • Set_power
    • Set_move_back

In the main program, I want to call the methods like that:

ROBOT obj_robot;

obj_robot.LIGHT.Set_ON_OFF = 1;
obj_robot.LIGHT.Set_color= 135;
obj_robot.LIGHT.Set_brightness= 75;

obj_robot.MOTOR.Set_right_left= 0;
obj_robot.MOTOR.Set_power= 85;
obj_robot.MOTOR.Set_move_back= 1;

How can I implement such class organization?

Thank you for your help.

Cedric

2 Answers2

0

Methods in C++ are called like this:

obj_robot.MOTOR.Set_power(85);

You should really read a tutorial on C++. e.g.:

https://developers.google.com/edu/c++/getting-started https://cal-linux.com/tutorials/getting_started_with_c++.html

Alex Garcia
  • 773
  • 7
  • 21
0

You can create your organization like this (there are a lot of different solutions ofc) :

class Light{
public:
    void setOnOff(int on) { m_on = on; }
    void setColor(int color) { m_color = color; }
    void setBrightness(int brightness) {m_brightness = brightness;}
private:
    int m_on;
    int m_color;
    int m_brightness;
};


class Robot {
public:
//  Motor& getMotor() {return m_motor;}
    Light& getLight() {return m_light;}
private:
//  Motor m_motor;
    Light m_light;
};

int main(int argc, char *argv[]) {
    Robot robot;
    robot.getLight().setOnOff(1);
    robot.getLight().setColor(135);
    robot.getLight().setBrightness(75);
};

But as @AlexGarcia pointed out, you should really read a few tutorial and create simple c++ program before trying to do thing like this.

user30701
  • 308
  • 1
  • 6