All the basics related to C++
In VEX, there are two languages to program in: C++ and Python. For most robotics applications and data algorithms you want to go with the quicker and more efficient languages to process code - in almost all cases for VEX, that will C++.
So by default this will be the default language for programming at East.
All resources will be derived from this Site: https://www.w3schools.com/cpp/default.asp. Refer to this website when more explicit questions arise.
C++ Basics
Comments
Throughout this entire tutorial comments located around the code will be very prevalent. They are a way to keep your code organized without directly interacting with the code. Say that a couple months have gone by and you forget what a variable does, a comment is there to help you remember! Or, say a function is acting stupid, rather than deleting all of if it and losing it, you can just comment it out.
There are a few ways to do them, but the two most common ways are starting your code with //
and moving to the next line, or a multi-line with an opening /*
& closing */
operator.
// This is a comment, it will not be read by the program
/*
This is a multi-line comment
It can go across multiple lines!
Just like this
*/
Variables
Variables Types
In C++, there are different types of variables (defined with different keywords), for example:
- int - stores integers (whole numbers), without decimals, such as 123 or -123
- float - stores integers (whole numbers), without decimals, such as 123 or -123
- double - also stores floating point numbers, with decimals, such as 19.99 or -19.99 (With double the amount of precision)
- char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
- string - stores text, such as "Hello World". String values are surrounded by double quotes
- bool - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
type variableName = value;
Example(s)
int myNum = 5; // Integer (whole number without decimals)
float my FloatNum = 5.99; // Floating point number (with decimals)
double myDoubleNum = 5.9999999999; // Double Floating point number (with more decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean
In most instances after each line of code, c++ requires the terminator ";". If this is forgotten often you will get compiler issues.
Multiple Variables
Depending on how you prefer to organize your code, sometimes it can be easier to initialize multiple variables all at once. This can be done with "," as the seperator for the variables
Example(s)
int x, y, z;
x = y = z = 50;
// x, y, and z will now output 50
The other Method in which this can be done is by line by line
int tempVar1 = 1;
int tempVar2 = 2;
Macros
Sometimes you will want to define an explicit value to a "Keyword" rather than a variable itself. It can help to keep your code more organized. This is done through the #define
initalization. The most common one that I use is for PI
. Macros can be finicky, so use them with caution.
Example(s)
#define PI 3.14159
#define RAD2DEG 180/PI
#define DEG2RAD PI/180
int tempVar = PI*5; // tempVar = 15.70796
Macro's usually will need to be located at the top of your file in order to run properly.
Arrays
without size []
, with size [size]
int intArray[] = {0, 1, 2};
float floatArray[] = {0.0, 1.0, 2.0};
double doubleArray[] = {0.00, 1.00, 2.00};
int intArray[2] = {0, 1};
float floatArray[3] = {0.0, 1.0, 2.0};
double doubleArray[4] = {0.00, 1.00, 2.00, 4.00};
can also make them 2d with back-to-back square brackets [][]
int intArray[2][3] = {{0, 1, 2},
{0, 1, 2}};
Structures
code goes here
Enums
code goes here
References
code goes here
Pointers
code goes here
Variable Modifications
These go before the Variable initalization, they modify the behavior of the variable.
Constants
An identifier that causes a variable to never change once initally set.
const float tempVar = 1;
Static
An identifier that causes a variable to remain as a callable after created in a function (see Functions section for more info).
static float tempVar = 1;
Printing
Printing is a way to view your variables results in the terminal. The easiest way to do this is the printf
or cout
function, but there are alos a few others depending on what you have available in your library.
printf("print this out") // output: print this out
cout << 1; // output: 1
In order to use printf with decimals/integers, the sytax must include a string with an inserted key-character as seen below:
int tempInt = 1;
float tempFloat = 1;
printf("Var: %i", tempInt); // output = Var: 1
printf("Var: %f", tempFloat); // output = Var: 1.000000
printf("Var: %.3f", tempFloat); // output = Var: 1.000 ~ '.3' Reduces to 3 decimal places
printf("Var: %.2f", tempFloat); // output = Var: 1.00
Conditionals and Operators
Operators
Operators are ways to check/modify variables against eachother, here are all the types of operators:
Assignment
- (
=
)
Arithmatic
- (
+
) ~ Addition - (
-
) ~ Subtraction - (
*
) ~ Multiplication - (
/
) ~ Division - (
%
) ~ Modulus - Outputs the remainder after division
Relational/Comparison
- (
==
) ~ Equal to - (
!=
) ~ Not equal to - (
<
) ~ Less than - (
>
) ~ Greater than - (
<=
) ~ Less than or equal to - (
>=
) ~ Greater than or equal to
Logical
- (
!
) ~ NOT - (
&&
) ~ AND - (
||
) ~ OR
Comma The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.
a = (b=3, b+2); // set b = 3 first, then a = b+2
printf("%i", b) // output: 3
printf("%i", a) // output: 5
Operator Example Usage:
int tempVar = 1;
int otherVar = 2;
int bigVar = 5;
bool holdVar = true;
bool otherholdVar = false;
bigVar % otherVar; // output: 1
tempVar > otherVar; // output: false (0)
tempVar >= otherVar; // output: false (0)
tempVar < otherVar; // output: true (1)
tempVar <= otherVar; // output: true (0)
!otherholdVar; // output: true
holdVar && holdVar; // output: false
holdVar || holdVar; // output: true
If, else if, and else
While loops
For loops
Break/Continue:
Switch Statements
Functions
Function Types
Function Scope (Global vs Local)
A variable created inside a function belongs to the local scope of that function, and can only be used inside that function:
A local variable cannot be used outside the function it belongs to.
If you try to access it outside the function, an error occurs:
void testcode( int varIN ){
int newVar = varIN;
printf("Output: %i", newVar); // Output: <Number>
}
int main(){
printf("Output: %i", newVar); // Won't Compile! Doesn't exist inside of main; ERROR!
}
If you would like to access a variable everywhere, you must create something called a global variable. This is how you would create such a variable:
int globalVar; // The global variable, OUTSIDE of the function/main
void testcode( int varIN ){
globalVar = varIN;
printf("Output: %i", newVar); // Output: <Number>
}
int main(){
printf("Output: %i", newVar); // Output: <Number>
}
Function Overloading
if you like the use of a function and would like to use it by calling it multiple different ways, function overloading exists!
void testcode( void ){
printf("This function is empty!");
}
void testcode( bool tempIn ){
printf("This function is for bools!");
}
void testcode( int tempIn ){
printf("This function is for integers!");
}
int main(){
testcode(); // Output: This function is empty!
testcode(true); // Output: This function is for bools!
testcode(1); // Output: This function is for integers!
}
Classes
Object Oriented Programming (OOP)
Constructors
Methods
Access Specifiers
References for Advanced C++
While this covers the general concepts for getting introduced into c++, and kick-started into vex programming there are plenty more advanced concepts which are not covered in this Library. For more information on advanced C++ concepts visit: