[C++] From Zero to Hero

binh12A3
3 min readOct 24, 2021

--

😎😎

1. namespace

For a detail explanation about namespace, you can find here.

  • We could preface the namespace before the API call “std::
  • We could use “using namespace std” to shorten the code (not recommended)
  • We could explicitly specify which objects we want to use by “using std::xxx” (recommended)

2. endl

  • endl = “\n” + flush.
  • We shouldn’t use endl when the application read/load files, since it’ll flush the cache and caused performance issue.

3. cin>> and cout<<

  • In C, we use printf(), scanf() and #include <stdio.h>
  • In C++, we use cin>>, cout<< and #include <iostream>

4. Format data

  • Format number
  • Padding

5. What is the Difference Between a Pointer and a Reference C++

  • Pointer and Reference are very similar. But the reference is recommended to be used more than pointer, since the pointer could be NULL or floating.
  • A reference need to be initialized when declare, and can’t have NULL value.
  • A reference is kind of like giving a variable a different name. For example, in below example, we can call var or ref, both are the same. So we have 2 ways to reference to the location of memory.
  • The pointer has a free to move around and point to different variables while the reference is assigned one time and it becomes to the reference to that location of memory, we can’t tell ref points to another variable. (i.e : ref is sticked to the address 0058F984)
  • The reference variable isn’t allocated any memory.
  • The reference variable is used in functional parameters (i.e pass by reference)to improve performance.
  • To reference to a constant, we need to use const reference too
const int x = 5;
const int &ref = x;
  • A const ref could reference to an un-constat variable
int x = 5;
const int &ref = x;

6. Random function

In C++11, we could use new library “#include <random>”

7. auto keyword

  • Before C+11, auto is used to explicitly determine that iCount is a local variable.
auto int iCount = 0; //before C++11
Equivalent with
int iCount = 0;
  • From C++11, auto keyword is used as dynamic type (i.e: same as var keyword in C#). So we need to initialize the value when we define a variable if we want to use auto keyword.
  • And we even can use auto as a return type of function.
  • But we can’t use auto in function parameters.
int add(auto a, auto b) //ERROR
{
return a + b;
}

8. extern “C”

  • Normally, we’ll use the extern keyword to say to the compiler that this object (variable, function,…) is defined in another place.
  • We need to use extern “C” to communicate between C++ and C.
  • We want to use/import a function in C, or export a function in C++, then C could use it.

9. Fileguards in C or C++ Header Files

  • Then if we compile, we’ll get the error “‘constants’: ‘class’ type redefinition” because there are multiple places include “constants.h”.
  • We can resolve it by including Fileguards in “constants.h”. And it’s a good practice to include Fileguards in all Header Files.

10. What is the difference between break and continue keywords in C/C++

11. C++ keywords

--

--

binh12A3
binh12A3

Written by binh12A3

share to be shared 😎 Made in 🇻🇳

No responses yet