Post
avtar img holder

Yonseo

2019-08-26 01:26:55
  • Last Edit: 0000-00-00 00:00:00
  • Intro to C++

    avtar img holder

    A Deitel Book

    A lot of the stuff in c++ hasn't changed. There is plenty of great books and material to study c++.

    I will be following this book C++ How to program by Deitel Fourth Edition and providing my own examples.

    A Programmers Perspective

    The word programmer gets thrown around a lot but we fail to mention the other side of coding that is part of the discipline, and that is problem solving. Our brains are constantly thinking about our next solution in a fraction of a second. I like to think we are 50% programmers and 50% problem solvers. Much of the code we write is similar to how an author writes a book. There are moments where we spit fire and others where we have to think about our next move so we don't create plot holes. We take big problems and break it down to smaller ones and then solve one at a time. This is known as rubber duck debugging. Be open to the idea of learning something new everyday and not knowing everything.

    Intro to C++

    What are headers?. I use headers by including the directive #include. C++ depends on headers when using several built in functions such as the math header .

    iostream is used to support the input/output file system.

    #include <iostream>
    using namespace std;

    int main()
    {
        string x;
        cout << "What is your name?";
        cin >> x;
        cout << "Your name is " << x;
        return 0;
    }

    The next line tells the compiler to use the std namespace.

    Next I have main(). This is the main program that will be executed first regardless of its location within the file. The main() function expects a return of zero to show there are no errors. If a true or 1 were to return an error has occurred or there is a bug in code.

    Variables

    When using variables I need to determine the type. There are several types including int, string, char, double, void, and many more. This will return a type of value. In my code I have main() as the type of int which means it will return an integer value. When it reaches the return it will end the program with a value of 0.

    Global variables can be accessed by many functions and are declared outside the main() function. Local variables are declared inside a function and can only be accessed by that function. If you create a local variable with the same name as a global variable, the local variable will store its own value in memory. 

    #include <iostream>
    using namespace std;

    int donuts = 12; //global variable
    int pastry();

    int main()
    {
        cout << "The local variable is " << pastry() << "\n";
        cout << "The global variable is " << donuts;
        return 0;
    }

    int pastry()
    {
        int donuts = 6; //local variable
        return donuts;
    }

    Input/Output

    cin (console input) is used for console input and cout(console output) is used for console output. Simply cout will be used to print a string or a result. cin will be used for user input which is typed on the keyboard into the console. I can store this data into cin or cout by pointing at it with two arrows << or >>

    Arithmetic

    Similar to the order of operations PEMDAS (parenthesis, exponents, multiplication, addition, subtraction) the order of precedence in C++ is multiplication, modulus, division, addition, subtraction, result. The difference is in the modulus which comes after multiplication and before division. Parenthesis are not required because multiplication has higher precedence than addition. The calculation is evaluated from left to right.

    Algorithm
    Actions to execute and the order of actions. One could argue that pouring milk in a bowl and then pouring cereal is the same as pouring cereal fist and then pouring the milk because it returns the same result. The order of operations is important when solving a problem. This is what makes an algorithm.

    Conditional Operator ? :
    A conditional operator has two results one for each condition true or false which is closely related to the if/else structure.

    #include <iostream>
    using namespace std;

    int main()
    {
        int donuts = 12;
        donuts == 12 ? cout << "A dozen donuts." : cout << "Not a dozen donuts.";
        return 0;
    }

    Performance Tips

    A nested if/else structure can perform faster than a selection of if structures because of early exit when a condition is satisfied. Test for conditions that are likely to be true at the beginning of the structure. 

    Common Programming Errors

    Placing a semicolon after the condition in an if statement. Forgetting one or both braces in a block of code. Not providing an action that causes the while loop to become false may lead to an infinite loop.


    Compiling Code

    To build an executable file using mingw. Replace home.exe with the name of your choice and home.cpp with the name of your c++ file.

    g++ -o home.exe home.cpp

    Input/Output

    • ofstream: Stream class to write on files
    • ifstream: Stream class to read from files
    • fstream: Stream class to both read and write from/to files.

    Exception Handling

    Exit with an Error

    exit(1);

    Latest Posts

    avtar img holder
    First time using Unreal Game Engine
    Category
    avtar img holder
    React JS Study Notes
    Category
    avtar img holder
    Creating a Component in React JS
    Category
    avtar img holder
    Javascript for Bubble Tea
    Category
    avtar img holder
    Github Commands
    Category
    avtar img holder
    FuelPHP Login & Authentication
    Category
    avtar img holder
    Intro to C++
    Category

    COPYRIGHT © 2019Taiyaki.io All rights ReservedHand-crafted & Made with