Skip to main content
Engineering LibreTexts

4.1: We Begin to Code...

  • Page ID
    29029
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    As we step into this module we will start to see examples of C++ code. I wanted to give just a brief example so we can also start to write some code.

    #include <iostream>
    using namespace std; 
    
    int main()
    { 
       int newVal = 99;
       // cout is just a way we can get output to the screen
       // we will talk more about this later.
       cout << "This is output" << newVal << endl;
       
       return 0;
    }

     For your first few program you can use this code to get started. We have already discussed the preprocessor line (the #include line). There is a brief discussion of the namespace entry just below. You may not understand all of the coding syntax, but you should be able to understand the namespace concept

    C++ requires every program to have a main() function, it returns an integer value - often times you will see it returns 0.

    The term "cout", is used to output data to the screen. the << is separates the different values to output: anything between "" is a string value; we can specify variable names; and we can send an "end of line" with the endl.

    Namespace in C++

    Definition and Creation:

    Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.

    • Namespace is a feature added in C++ and not present in C.
    • A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it.
    • Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.

    A namespace definition begins with the keyword namespace followed by the namespace name as follows:

    namespace namespace_name 
    {
       int x, y; // code declarations where 
                 // x and y are declared in 
                 // namespace_name's scope
    }
    
    • Namespace declarations appear only at global scope. (we will talk about scope later)
    • Namespace declarations can be nested within another namespace.
    • Namespace declarations don’t have access specifiers. (Public or private)
    • No need to give semicolon after the closing brace of definition of namespace.
    • We can split the definition of namespace over several units.
    // Creating namespaces 
    #include <iostream> 
    using namespace std; 
    
    namespace ns1 
    { 
        int value() { return 5; } 
    } 
    namespace ns2 
    { 
        const double x = 100; 
        double value() { return 2*x; } 
    } 
    
    int main() 
    { 
        // Access value function within ns1 
        cout << ns1::value() << endl; 
    
        // Access value function within ns2 
        cout << ns2::value() << endl; 
    
        // Access variable x directly 
        cout << ns2::x << endl;     
    
        return 0; 
    } 
    

    The statement "using namespace std" tells C++ that we are using the standard C++ namespace, so that we can use statements like cout without having to specifically code std::cout or std::endl. 

    We also define two other namespaces for our own variables, ns1 and ns2.  When we use these variables/functions, we are required to prefix the variables and functions with the namespace name. In the example above we have ns1 we have an function that returns an integer named value(), in ns2 we also have a function named value(), which returns a double. These are duplicate names, BUT, are defined in different namespaces and therefore operate as separate functions.

    Using namespaces is very helpful in large projects where we have lots of pieces and several different teams working on different pieces of the project. We can create variables and functions and not have to worry about duplicate names, just need to specify which namespace we are using in each situation. 

    Adapted from:
    "Namespace in C++ | Set 1 (Introduction)" by Abhinav Tiwari, Geeks for Geeks is licensed under CC BY-SA 4.0


    This page titled 4.1: We Begin to Code... is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?