Programming/C++/Objects and Data Structures: Difference between revisions
Brodriguez (talk | contribs) (Create page) |
Brodriguez (talk | contribs) m (Brodriguez moved page C++/Objects and Data Structures to Programming/C++/Objects and Data Structures) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 72: | Line 72: | ||
# Remove item at given vector index. | # Remove item at given vector index. | ||
<vector_var>.remove(<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 {{ ic |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 [[ Programming/C++/Syntax#Function_Declarations | function declarations ]] here. Then in the corresponding {{ ic |.cpp}} file, we define the functions. | |||
{{ hc |my_file.hpp|<nowiki> | |||
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>); | |||
}; | |||
</nowiki>}} | |||
{{ hc |my_file.cpp|<nowiki> | |||
<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. | |||
... | |||
} | |||
</nowiki>}} | |||
=== 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. | |||
{{ hc |my_file.hpp|<nowiki> | |||
class <ClassName> { | |||
// Class attributes. | |||
<type> <var_name_1>; | |||
<type> <var_name_2>; | |||
... | |||
<type> <var_name_n>; | |||
public: | |||
<ClassName>(<arg_1>, <arg_2>, ..., <arg_n>); | |||
}; | |||
</nowiki>}} | |||
{{ hc |my_file.cpp|<nowiki> | |||
<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. | |||
} | |||
} | |||
</nowiki>}} | |||
Ex: | |||
{{ hc |my_file.hpp|<nowiki> | |||
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); | |||
}; | |||
</nowiki>}} | |||
{{ hc |my_file.cpp|<nowiki> | |||
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. | |||
} | |||
} | |||
</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 === | |||
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(); |
Latest revision as of 03:57, 21 April 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();