C++ oop?

Hello!
As someone who is working hard on making tutorials, I am also trying to learn things myself. Some questions I have are relative to have learned from other coding platforms, but I would love it if I can get some assistance if I can do similar things with C++.

How would I be able to do OOP in C++, where I can create classes and methods?

Pseudocode (Java blended with C++):

PID liftPID = new PID(0.05, 0.02, 0.03);//kP, kI, kD
int desiredValue = 100;

while(1==1){

double motorPower = liftPID.calculate(LiftMotor.position(degrees), desiredValue); 

wait(20, msec);
}

https://learncpp.com

8 Likes

This is a rather broad question. I suggest that you go through a C++ tutorial/book in order to get clear and complete knowledge, rather than asking here on the forum where you are going to get fragments of information.

7 Likes

Haven’t you been using VEXcode for a few years now? Cpp is an object-oriented language, so I’m very confident you’ve used it before

1 Like

I was originally told C++ was not necessarily an Object-Oriented language, so I didn’t bother asking generally. As an addition, I was told it wasn’t by many I asked. That being said, I was unaware of OOP’s existence in C++, so I was coding without C++ OOP knowledge.

My question was in regards to seeing if I can do something similar to OOP because I was unsure if it was possible. It’s my apologies for not searching to see if it were actually possible. But, my main question (or follow up question) I would have is how exactly would I do something similar to OOP across folders in VEXCode? Would it be the same as in C++ as well or would it be slightly different?

As others have said, I think you need a more solid understanding of how C++ works before you can reasonably work with a higher-level question like this. Some good resources have been posted already; also, here is a playlist of videos where I go over a variety of C++ concepts starting from basics and working to more advanced things (with the assumption that you already know C).

To directly address your question, yes C++ has OOP, but it is not the same as in Java. The biggest difference comes from the fact that Java is garbage-collected, while C++ is not; this is what allows one to just create objects in Java and forget about them. In C++, some additional care needs to be taken to ensure that things work as you expect.

Also, C++ is a notoriously complicated programming language with lots of syntax and obscure features, so it’s easy to get lost in that instead of trying to solve the problem at hand. While OOP is great, it’s not necessary for everything, and it’s also not the best answer for everything. While I would definitely create a PID class for use in my own code, don’t feel as though you need to do the same in yours; “normal” style code works fine. That being said, it’s a great introductory example to learn how OOP in C++ works.

Best of luck.

22 Likes

http://cplusplus.com/
i recommend this.

1 Like

There are a lot of C++ resources, I’m sure op is aware of.

Yes C++ has classes and objects, no GC, etc. It is object oriented, the reason you are probably somewhat confused is because VEX has made their API more C like. Nothing wrong with that btw.

I would argue that most low level robotics code doesn’t need OOP?

There are exceptions, for example in PROS I once made a PID class/object that self-contained all the state for a PID loop including the tuning. That way I could spin up multiple at once without issue.

The actual API itself is built with OOP in mind. Motor objects, sensor objects, controller objects and brain objects. However on users creating their own objects, something like a PID object is one of the few places where it undeniably makes sense.

At least if you are writing a single robot program. If you are writing a library to be used by a hundred robot programs, a chassis object for example makes a lot of sense. That way functions can take a chassis object and not have to worry about how that chassis is implemented, or how many wheels it has.

10 Likes