C++ add object to class?

I was wondering if it is possible to add a custom object to a predefined class. I am a novice c++ programmer so I do not fully
understand how classes and objects operate or if what I am trying to do even uses a class object structure.

For example if want to print text centered on a screen, I would create a custom object “printCenter” and add the functionality to print in the center of screen


 Controller1.Screen.printCenter("Hello") ;  //can you add a "printCenter" or other custom function to a class?  

I think it may be better to create my own class and not use the predefined structure but I am not fully sure how to.

Some terminology: in Object Oriented Programming, Objects are instances of Classes. What you are describing is a Method, which is a Function attached to an Object (as defined by the Object’s Class).

As for what’s going on here, Controller1 is an object of class vex::controller. It has a member (variable or object attached to another object) called Screen of class vex::controller::lcd. You want to add a method to the class vex::controller::lcd so that you can use that method with Controller1.Screen.

The problem: you cannot add methods to classes outside of the file in which the class is defined. To add a method, you would have to make a class that inherits from vex::controller::lcd and adds your method to it. But at that point the vex::controller class does not have a member of your new class, so you would have to make another class that inherits from vex::controller but changes the Screen member to be of your new lcd class.

It’s messy. I would just write a non-object function that takes your text, pads the correct number of spaces onto the beginning of your desired text to center it, then pass the result of that function to the lcd’s print method. (Or ask VEX and Robot Mesh to add it to our vex::controller::lcd classes.)

Thanks for the help, im trying to develop my c++ skills but I havent got too far yet

There are a number of ways to achieve what you want. A simple function like John suggested that then calls controller.Screen.print having set the cursor position is probably the easiest. Here’s an example for you to study that uses a sub class of brain to do something similar (except for brain, not controller). I’m not suggesting you use this, just gives you an idea of what can be done.

A subclass of brain called brain_x

namespace vex {
  class brain_x : public vex::brain {
    public:
      brain_x() : brain() {};
      ~brain_x() {};
  
      class lcd_x : public vex::brain::lcd {
        public:
          lcd_x() : lcd() {};
          ~lcd_x() {};

          void printCenter( const char *str ) {
            printCenter( (char *)str );
          }
          void printCenter( char *str ) {
            // not really centered, need stringWidth method, that's will be in next sdk
            printAt( 240-(strlen(str)*10)/2, 100, str );
          }
      };
      
      lcd_x     Screen = lcd_x();
  };
}

instance is created like this

brain_x  Brain;

then new method can be called like this

Brain.Screen.printCenter("Hello");