C++ Notes
C++ Notes
Loops
Range based for loops can be used for vectors and arrays. Instead of int n, we can say const int& n.
const because we are only printing the values and “&” so we pass by reference- memory efficient.
Break
If break is used with a nested loop, then only the inner loop is terminated.
Continue
Continue in a nested loop will only skip that iteration of the inner loop and doesn’t affect outside loop.
Switch
Same thing can be done with if, else if but this is slightly cleaner.
Go to
This is generally avoided and any program can be written without goto
Functions
We can pass default values which are used if no arguments are passed.
Once we provide a default value for one parameter, the following parameters must all have default
values.
Storage
Using the word static before declaring a variable means that although it only exists inside the function it
is defined, it’s lifetime starts when the function is called and only ends when the program ends, so its
value persists even after the function has ended.
If “var” wasn’t static, the output would be 1, 1 instead of 1, 2.
Return by reference
This return (address to) the variable. We cannot return local variables/constants. In the main, we assign
the value 5 to this global variable returned by the function.
Arrays
When passing 2d arrays as arguments, number of columns need to be specified but the number of rows
don’t.
Strings
Getline takes the input stream as the first parameter and the second parameter as the location where the
line from the user will be stored.
Structures
Pointers can also point to structures and not just the primitive data types.
Classes
To access overridden functions, we use a double colon “::”. Base is the base class and derived2 is an
instance of the derived class (called derived) from Base class.
We can also call the overridden function using pointers to the type of the base class:
If mammal and winged animal both have a member function called someFunction() and bat doesn’t
have its own someFunction then compiler shows an error because it doesn’t know which function to
call. So when we call the function, we do the following:
Friend Functions
Can access private and protected data of the class.
Friend class
When we declare a class as a friend within another class, then all member function of the friend become
friend functions.
Here class B is a friend of class A so class B has access to members of class A but not the other way
round.
Virtual functions
Virtual functions are those member functions of the class that we have to override in the derived class.
If we do this then even if we have a pointer of type base class, we will execute the function in the
derived class unlike before when pointers could be used to access the overridden function of the base
class.
With C++ 11, we can use the keyword override next to the functions to make it clear.
Function templates
When we need to perform the same type of operation on 2 different data types then we would use
function overloading and create the same function again with a different data type. A better approach is
function template.
T is a template argument that accepts different data types like int, float etc and class is a keyword. When
an argument of a data type is passed to someFunction, then a new version of the function is created for
the given data type.
Class templates
T is the template argument again and a placeholder for the data type.