C++ Crash Course from Udacity

The outline is Basics, Compilation and Execution, Arithmetic Operations, Control Flow, Pointers, Arrays, Functions, Classes, Overloading, Templates, Classes and Inheritance, PolyMorphism, Vectors and Iterators, Interview Questions, C++ Checkpoint

In Arithmetic Operation, note C++ implicit assignments, In C++, as in many languages, there are postfix and prefix operators. The form for each is: Incrementing – prefix: ++a postfix: a++; Decrementing – prefix: –a – postfix: a
Initial values: post = 0 pre= 0 After one postfix and prefix: post = 0 pre= 1 After two postfix and prefix: post = 1 pre= 2.

In control flow, note Python doesn’t have switch, but C++ does, there is also a do…while loops in C++, which in Python you can set while=True then loop.

switch(expression) { case constant-expression : statements; break; (this is optional); case constant-expression : statements; break; (this is optional); ... default : //optional statements; }
do { statements; }while(condition );
for( ; 😉 { std::cout<<"This for loop will run forever\n"; } //infinite loop using for
while(1) { std::cout<<"This while loop will run forever\n";} //infinite loop using a while loop

Function Overloading allow to use the same function for different functions. for example, below overloaded a function called findSmaller. This function will have three variations; integer arguments and return variable, float arguments and return variable, and character arguments and return variable. Now I can write a program using findSmaller with three different input variable types.

/*Goal: look at a program, and see if we can make it more versatile*/

#include<iostream>

int findSmallerInt(int input1, int input2);
float findSmallerFloat(float input1, float input2);
char findSmallerChar(char input1, char input2);

int main()
{
    int a = 5; 
    int b = 4;
    float f1 = 5.43;
    float f2 = 6.32;
    char c1 = 'c';
    char c2 = 'z';
    std::cout<<findSmallerInt(a,b)<<" is the smaller of "<<a<<" and "<<b<<"\n";
    std::cout<<findSmallerFloat(f1,f2)<<" is the smaller of "<<f1<<" and "<<f2<<"\n";
    std::cout<<findSmallerChar(c1,c2)<<" is the smaller of "<<c1<<" and "<<c2<<"\n";
    
    return 0;
}

int findSmallerInt(int input1, int input2)
{
    if(input1<input2)
        return input1;
    return input2;
}
float findSmallerFloat(float input1, float input2)
{
    if(input1<input2)
        return input1;
    return input2;
}

char findSmallerChar(char input1, char input2)
{
    if(input1<input2)
        return input1;
    return input2;
}

//Now look at how it is used in class
//main.hpp
#include<iostream>
class Compare
{
public:
    int findSmaller(int input1, int input2);
    float findSmaller(float input1, float input2);
    char findSmaller(char input1, char input2);
};

int Compare::findSmaller(int input1, int input2)
{
    if(input1<input2)
        return input1;
    return input2;
}
float Compare::findSmaller(float input1, float input2)
{
    if(input1<input2)
        return input1;
    return input2;
}

char Compare::findSmaller(char input1, char input2)
{
    if(input1<input2)
        return input1;
    return input2;
}
//main.cpp
/*Goal: look at a program, and see if we can make it more versatile*/
#include "main.hpp"
int main()
{
    Compare c;
    int a = 5; 
    int b = 4;
    float f1 = 5.43;
    float f2 = 6.32;
    char c1 = 'c';
    char c2 = 'z';
    std::cout<<c.findSmaller(a,b)<<" is the smaller of "<<a<<" and "<<b<<"\n";
    std::cout<<c.findSmaller(f1,f2)<<" is the smaller of "<<f1<<" and "<<f2<<"\n";
    std::cout<<c.findSmaller(c1,c2)<<" is the smaller of "<<c1<<" and "<<c2<<"\n";
    
    return 0;
}
//overload operator, example below overload plus function to add a class box
class Box {
    public:
        int height;
        int width;
        int depth;
        
        // Constructor
        Box(int h, int w, int d): height(h), width(w), depth(d) {}

        // Overloaded '+' operator
        Box operator+(const Box& b) {
            return Box(this->height + b.height, this->width + b.width, this->depth + b.depth);
        }
};

Pointers, which are the addresses of variables, can be accessed in C++. for example int a = 54; where is a, the location of a can be found using a pointer:
std::cout << &a<< “\n”; a = 54 address of a is at &a = 0x7ffd5fd90fa4.

“dereference” the pointer, you’re following the pointer to the location it points to and getting the variable at that location.

#include <iostream>

int main() {
    int treasure = 100;    // This is your treasure.
    int* mapX = &treasure; // This is your map with the X. It stores the location of the treasure.

    int foundTreasure = *mapX; // Here you dereference the pointer (follow the X to the treasure).

    std::cout << foundTreasure;
    std::cout << mapX;
    return 0;
}
//the output is 100  0x7ffee962f178

C++ allows us to create generic functions using templates. templates are a powerful feature of C++ that help you write more flexible and reusable code.

template <typename T>
T add(T a, T b) {
    return a + b;
}
int x = add<int>(3, 4);         // T is int, so this adds two integers
float y = add<float>(3.1, 4.2); // T is float, so this adds two floats

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.