Programming/C++/Syntax: Difference between revisions
Brodriguez (talk | contribs) (Add notes about function templating) |
Brodriguez (talk | contribs) m (Brodriguez moved page C++/Syntax to Programming/C++/Syntax) |
Latest revision as of 03:57, 21 April 2023
.cpp
.h
.hpp
Comments
Inline Comments
// This is an inline comment.
Block Comments
/** *This is a block comment. *Comment line 2. *Another block comment line. */
Basic Input and Output
Basic Output
A very basic output example is:
#include <iostream> std::cout << "Hello World!\n";
We can also chain together strings this way:
#include <iostream> std::cout << "Hello " << "World!" << "\n";
If we exclude the \n
, then output will remain on a single line:
#include <iostream> std::cout << "Hello "; std::cout << "World!"; std::cout << "\n";
The above code snippets are all functionally equivalent.
Basic Input
Similar to basic output:
#include <iostream> std::cin >> <variable_here>
For example:
#include <iostream> // This will read in a string as a variable, then immediately display it. string my_string; std::cin >> my_string; std::cout << my_string << "\n";
Variables
Variables are strongly typed in C++. That means you must declare the type as well as the name.
Variable Definition
bool a_bool = true; bool b_bool = false; string my_var_1 = "This is "; string my_var_2 = "a string.";
Variable Usage
#include <iostream> std::cout << "Printing variable values.\n"; std::cout << a_bool << "\n"; std::cout << b_bool << "\n"; std::cout << my_var_1 << my_var_2 << "\n";
Booleans
C++ uses the following bools"
bool true_bool = true; bool false_bool = false;
Null Values
C++ uses the standard null
.
int null_value = null;
Import Statements
If Statements
Basic If
if (x == y) { # Logic if true. }
Full If
if (x == y) { # Logic if true. } else if (! x && (y || z)) { # Logic for "else if" true. } else { # Logic for false. }
Switch/Case Statements
switch (<conditional_statement) { case <result_1>: # Logic if matches <result_1>. break; case <result_2>: # Logic if matches <result_2>. break; ... default: # Default/fallback if no matched results. break; }
Note that the break statements are optional, but recommended. Removing a break statement will make the code execute into the next result section, instead of exiting the switch block.
Loops
For Loop
for (<counter_var> = <start_index>; <end_condition>; <counter_var_modifier>) { # Loop logic here. }
Ex:
for (int index = 0; index < 10; index++ { # Loop logic here. }
While Loop
while (<condition>) { # Loop logic here. }
Functions
Function Definitions
<return_type> <function_name>(<arg1>, <arg2>, ..., <argn>) { # Function logic here. return <return_value>; }
Ex:
# This is a function that returns nothing. void my_function() { # Function logic here. } # This is a function that returns an int. int my_function() { # Function logic here. ... int my_return_int = 5; ... return my_return_int; } # This is a function that returns nothing, but takes three parameters. void my_function(int my_int, std::string my_string, bool some_other_arg) { # Function logic here. }
Function Declarations
In the C language, a function must be physically defined above of wherever it's called.
This can be accomplished either by defining the function (see above), or via function declaration.
Function declaration is a single statement, generally at the top of the file (right under the Include Headers). These statements indicate to the compiler "this function is going to exist later on, and here is how it will be set up". Then the function itself can be defined at any arbitrary point later on in the file. It's very convenient for organization.
The syntax is as follows:
<return_type> <function_name>(<arg_1>, <arg_2>, ..., <arg_n>);
Ex:
void my_function(int my_int, std::string my_string, bool some_other_arg);
It should look very similar to the first line of any function declaration. Because it's effectively the same thing, minus the body of the function.
Function Overloading
If two functions are very similar, then we can do function overloading.
For example, what if we want a function that can take either an int
or a double
as a parameter, and then otherwise handle the same?
All you do is define the same function twice. As long as each either has different parameter typings, or a different number of parameters, then this is allowed.
Ex:
# In this case, we can call the same function name and pass in two different possible variable types. void my_number_processor(int num) { # Do stuff here. } void my_number_processor(double num) { # Do stuff here. }
Function Templating
In a similar vein of logic to Function Overloading, we have Function Templates. In fact, templates are arguably even more useful.
Rather than having to declare the entire function twice (or more, if we overload it more than two times), templates allow us to declare the function exactly once, and still get the same effect.
To declare a template, we have to put the entire function in a header file.
my_file.hpp
template <typename T> <return_type> <function_name>(T <arg>) { # Do stuff here. # Note that our passed "<arg>" variable can now be any type. ... }
Ex:
my_file.hpp
template <typename T> void my_function(T item) { # Do stuff here. # Note that our passed "item" variable can now be any type. ... }