Programming/C++/Objects and Data Structures

From Dev Wiki
< Programming‎ | C++
Revision as of 11:41, 6 March 2023 by Brodriguez (talk | contribs) (Start of documenting classes)
Jump to navigation Jump to search

Basic Data Structures

Vectors

A vector is a C++ simple class, that effectively acts as a "dynamic array".
It is technically slower and takes more space, but it can be arbitrary sizes.


Creating Vectors

#include <vector>

# Create empty vector.
std::vector<<data_type>> <vector_name>;

# Create vector with some initial data.
std::vector<<data_type>> <vector_name> = {<data_value_1>, <data_value_2>};

# Create vector with some number of elements, but elements aren't populated yet.
std::vector<<data_type>> <vector_name>(<number_of_elements>);

Ex:

#include <vector>

# Create empty vector.
std::vector<int> my_int_vector;

# Create vector with some initial data.
std::vector<int> my_int_vector = {95, 12, 29};

# Create vector with some number of elements, but elements aren't populated yet.
# In this case, 5 elements.
std::vector<int> my_int_vector(5);

Manipulating Vectors

Get vector length:

<vector_var>.size();


Get item at vector index:

...

# Get vector item at a given index.
<vector_var>[<index>];

Ex:

...

# Get vector item at a given index.
# In this case, we get the 6th item, located at index 5 (vectors start at index 0).
my_int_vector[5];


Add item to vector:

...

# Add item to end of vector.
<vector_var>.push_back(<value>);

# Insert item at given vector index.
<vector_var>.insert(<index>, <value>);


Remove item from vector:

...

# Remove item from end of vector (no return value).
<vector_var>.pop_back();

# Remove item at given vector index.
<vector_var>.remove(<index>);


Classes and Objects

First, see C++/Headers and File Including.

Note: C++ classes have the concept of public and private. Everything not explicitly declared in the public region is considered private by default.

Declaring Classes

We declare our class itself in the header file. Any internal functions are usually left as function declarations here. Then in the corresponding .cpp file, we define the functions.

my_file.hpp
class <ClassName> {
    // Class attributes.
    <type> <var_name_1>;
    <type> <var_name_2>;

    ...

    <type> <var_name_n>;

    public:
        // Class function declarations.
        <return_type> <function_name_1>(<arg_1>, <arg_2>, ..., <arg_n>);
        <return_type> <function_name_2>(<arg_1>, <arg_2>, ..., <arg_n>);

        ...

        <return_type> <function_name_n>(<arg_1>, <arg_2>, ..., <arg_n>);
};
my_file.cpp
<ClassName>::<function_name_1(<arg_1>, <arg_2>, ..., <arg_n>) {
    # Function logic here.

    ...
}
<ClassName>::<function_name_2(<arg_1>, <arg_2>, ..., <arg_n>) {
    # Function logic here.

    ...
}

...

<ClassName>::<function_name_n(<arg_1>, <arg_2>, ..., <arg_n>) {
    # Function logic here.

    ...
}

Manipulating Objects

Reminder that an object is a live instance of a class.

To instantiate an object from a class:

<ClassName> <variable_name>;

Ex:

// This creates a variable called "my_object_instance" from the "MyClass" class.
MyClass my_object_instance;


Access an object's attributes:

// Note that the attribute has to have been part of the class definition.
<variable_name>.<attribute_name>;

Ex:

// Note that the attribute has to have been part of the class definition.
my_object_instance.my_int;


Access an object's functions:

// Note that the function has to have been part of the class definition.
<variable_name>.<function_name>;

Ex:

// Note that the function has to have been part of the class definition.
my_object_instance.my_function();