6.x — Chapter 6 summary and quiz

Quick review

We covered a lot of material in this chapter. Good job, you’re doing great!

A compound statement or block is a group of zero or more statements that is treated by the compiler as if it were a single statement. Blocks begin with a { symbol, end with a } symbol, with the statements to be executed are placed in between. Blocks can be used anywhere a single statement is allowed. No semicolon is needed at the end of a block. Blocks are often used in conjunction with if statements to execute multiple statements.

User-defined namespaces are namespaces that are defined by you for your own declarations. Namespaces provided by C++ (such as the global namespace) or by libraries (such as namespace std) are not considered user-defined namespaces.

You can access a declaration in a namespace via the scope resolution operator (::). The scope resolution operator tells the compiler that identifier specified by the right-hand operand should be looked for in the scope of the left-hand operand. If no left-hand operand is provided, the global namespace is assumed.

Local variables are variables defined within a function (including function parameters). Local variables have block scope, meaning they are in-scope from their point of definition to the end of the block they are defined within. Local variables have automatic storage duration, meaning they are created at the point of definition and destroyed at the end of the block they are defined in.

A name declared in a nested block can shadow or name hide an identically named variable in an outer block. This should be avoided.

Global variables are variables defined outside of a function. Global variables have file scope, which means they are visible from the point of declaration until the end of the file in which they are declared. Global variables have static duration, which means they are created when the program starts, and destroyed when it ends. Avoid dynamic initialization of static variables whenever possible.

An identifier’s linkage determines whether other declarations of that name refer to the same object or not. Local variables have no linkage. Identifiers with internal linkage can be seen and used within a single file, but it is not accessible from other files. Identifiers with external linkage can be seen and used both from the file in which it is defined, and from other code files (via a forward declaration).

Avoid non-const global variables whenever possible. Const globals are generally seen as acceptable. Use inline variables for global constants if your compiler is C++17 capable.

Local variables can be given static duration via the static keyword.

Using statements (including using declarations and using directives) can be used to avoid having to quality identifiers with an explicit namespace. These should generally be avoided.

Finally, C++ supports unnamed namespaces, which implicitly treat all contents of the namespace as if it had internal linkage. C++ also supports inline namespaces, which provide some primitive versioning capabilities for namespaces.

Quiz time

Question #1

Fix the following program:

Show Solution

Question #2

Write a file named constants.h that makes the following program run. If your compiler is C++17 capable, use inline constexpr variables. Otherwise, use normal constexpr variables.


Show Solution

Question #3

Complete the following program by writing the passOrFail() function, which should return true for the first 3 calls, and false thereafter.

Show Hint

The program should produce the following output:

User #1: Pass
User #2: Pass
User #3: Pass
User #4: Fail
User #5: Fail

Show Solution

7.1 -- Control flow introduction
6.13 -- Unnamed and inline namespaces

328 comments to 6.x — Chapter 6 summary and quiz

  • Aasif Ali

    i did not understand the third question .... why will it give true for the first 3 inputs?

  • Kanaetochi

    (return (--passes >= 0) is an example of I'm not sure what to call it, but using a "directional statement" in a conditional statement.

    What that line did was it reduced the variable "passes" by one, then asked the computer if it was greater than or equal to 0.

    People often do it to save lines while programming, but it doesn't really have a noticeable effect on the output.

  • Ladyoftheroundtable

    Quick question, my function for question 3 was pretty verbose compared to the official solution, but my question was, does having it just return a bool literal count as magic numbers?

    • nascardriver

      I've never heard a `bool` being called a magic number, but you can call it that. There's no way to know what the return value of `passOrFail()` means. You could rename the function to for example "doesPass", and the meaning of the return value would be clear.
      If you don't want to rename the function, you can return an enum (covered later), or document the return value, eg.

    • Alex

      The true and false keywords generally aren't considered magic numbers.

  • Chayim

    In question 1 how does "num = -num" return it into positive? is -22 - -22 not 0?

    • nascardriver

      A negative sign (-) inverts the sign of the value. It turns positive into negative and negative into positive.

  • Newbie

    i wrote this function for quiz question #3, i want to know if this is a good way to do it cause i don't get what you did in the solution (return (--passes >= 0);, don't know what's going on)

    • frog

      as I understand it,

      decrements (--) the value of the passes variable first, and then compares (>=) it to the literal 0 which then evaluates into a Boolean value 0(false) or 1(true)

  • Chayim

    In question 1, the code as it is unresolved, why does it compile to negative - when entering a positive number like 55 it compiles to -55 ?

    • Anon

      You can read the if statement as a single line statement:

      As a result,

      will always run and make a positive number become negative no matter what the condition is, because it is not part of the "if statement".

Leave a Comment

Put all code inside code tags: [code]your code here[/code]