Programming/C++/Objects and Data Structures: Difference between revisions
Brodriguez (talk | contribs) (Add notes about constructors) |
Brodriguez (talk | contribs) (Add deconstructor sections) |
||
Line 183: | Line 183: | ||
</nowiki>}} | </nowiki>}} | ||
=== Deconstructors === | |||
Due to having to manage memory, C++ classes have deconstructors. | |||
Deconstructors are called automatically any time: | |||
* An object moves out of scope. | |||
* An object is explicitly deleted. | |||
* The program ends. | |||
Deconstructor syntax is exactly the same as the [[ Programming/C++/Objects and Data Structures#Constructors |constructor syntax]].<br> | |||
The only difference is that deconstructors are preceded by a {{ ic |~}} operator. | |||
Ex: | |||
{{ hc | my_file.hpp |<nowiki> | |||
class Car { | |||
// Class attributes. | |||
int number_of_wheels; | |||
std::string color; | |||
public: | |||
// Define constructor and deconstructor. | |||
Car(int num_of_weels, std::string color); | |||
~Car(); | |||
} | |||
</nowiki>}} | |||
{{ hc | my_file.cpp |<nowiki> | |||
/** | |||
* Class constructor. | |||
*/ | |||
Car::Car(int num_of_weels, std::string color) { | |||
// Constructor logic here. | |||
} | |||
/** | |||
* Class deconstructor. | |||
*/ | |||
Car::~Car() { | |||
// Deconstructor logic here. | |||
} | |||
</nowiki>}} | |||
=== Manipulating Objects === | === Manipulating Objects === | ||
Line 190: | Line 231: | ||
// Without constructor. | // Without constructor. | ||
<ClassName> <variable_name>; | <ClassName> <variable_name>; | ||
// With constructor. | // With constructor. | ||
<ClassName> <variable_name>(<constructor_arg_1>, <constructor_arg_2>, ..., <constructor_arg_n>); | <ClassName> <variable_name>(<constructor_arg_1>, <constructor_arg_2>, ..., <constructor_arg_n>); | ||
Line 198: | Line 239: | ||
// Without constructor. | // Without constructor. | ||
MyClass my_object_instance; | MyClass my_object_instance; | ||
// With constructor. | // With constructor. | ||
MyClass my_object_instance("this", "is", "a", "test"); | MyClass my_object_instance("this", "is", "a", "test"); | ||
Access an object's attributes: | Access an object's attributes: |
Revision as of 06:27, 7 March 2023
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.
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. ... }
Constructors
C++ also provides class constructors.
These take the form of functions that have the exact same name as the class, and no return type.
my_file.hpp
class <ClassName> { // Class attributes. <type> <var_name_1>; <type> <var_name_2>; ... <type> <var_name_n>; public: <ClassName>(<arg_1>, <arg_2>, ..., <arg_n>); };
my_file.cpp
<ClassName>(<arg_1>, <arg_2>, ..., <arg_n>) { // Initialize internal attributes based on passed in values. // Note that not all attributes have to be set here. But we have the option to set if desired. :<var_name_1>(<arg_1>), <var_name_2>(<arg_2>), ..., <var_name_n>(<arg_n>) { // Constructor logic here. } }
Ex:
my_file.hpp
class MyCoolClass { // Class attributes. std::string my_attribute_1; int my_attribute_2; ... bool my_attribute_n; public: MyCoolClass(std::string input_1, int input_2); };
my_file.cpp
MyCoolClass::MyCoolClass(std::string input_1, int input_2) { // Initialize internal attributes based on passed in values. // Note that not all attributes have to be set here. But we have the option to set if desired. :my_attribute_1(input_1), my_attribute_2(input_2) { // Constructor logic here. } }
Deconstructors
Due to having to manage memory, C++ classes have deconstructors.
Deconstructors are called automatically any time:
- An object moves out of scope.
- An object is explicitly deleted.
- The program ends.
Deconstructor syntax is exactly the same as the constructor syntax.
The only difference is that deconstructors are preceded by a ~
operator.
Ex:
my_file.hpp
class Car { // Class attributes. int number_of_wheels; std::string color; public: // Define constructor and deconstructor. Car(int num_of_weels, std::string color); ~Car(); }
my_file.cpp
/** * Class constructor. */ Car::Car(int num_of_weels, std::string color) { // Constructor logic here. } /** * Class deconstructor. */ Car::~Car() { // Deconstructor logic here. }
Manipulating Objects
Reminder that an object is a live instance of a class.
To instantiate an object from a class:
// Without constructor. <ClassName> <variable_name>; // With constructor. <ClassName> <variable_name>(<constructor_arg_1>, <constructor_arg_2>, ..., <constructor_arg_n>);
Ex:
// This creates a variable called "my_object_instance" from the "MyClass" class. // Without constructor. MyClass my_object_instance; // With constructor. MyClass my_object_instance("this", "is", "a", "test");
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();