32. Exercises – Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards

© Slobodan Dmitrović 2020
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6047-0_32

32. Exercises

Slobodan Dmitrović1 
(1)
Belgrade, Serbia
 

32.1 Header and Source Files

Write a program that declares an arbitrary function in a header file. The header file is called myheader.h Define this function inside the main program source file called source.cpp. The main function is also located inside a source.cpp file. Include the header into our source file and invoke the function.

myheader.h:
void myfunction();  //function declaration
source.cpp:
#include "myheader.h" //include the header
#include <iostream>
int main()
{
    myfunction();
}
// function definition
void myfunction()
{
    std::cout << "Hello World from multiple files.";
}

32.2 Multiple Source Files

Write a program that declares an arbitrary function in a header file. The header file is called mylibrary.h Define a function inside the source file called mylibrary.cpp. The main function is inside a second source file called source.cpp file. Include the header in both source files and invoke the function.

mylibrary.h:
void myfunction();  //function declaration
mylibrary.cpp:
#include "mylibrary.h"
#include <iostream>
// function definition
void myfunction()
{
    std::cout << "Hello World from multiple files.";
}
source.cpp:
#include "mylibrary.h"
int main()
{
    myfunction();
}

Explanation:

This program has three files:
  • A header file called mylibrary.h where we put our function declaration.

  • A source file called mylibrary.cpp where we put our function definition. We include the header file mylibrary.h into mylibrary.cpp source file.

  • A source file called source.cpp where the main program is. We also include the mylibrary.h header file into this source file.

Since our header file is included in multiple source files, we should put header guard macros into it. The mylibrary.h file now looks like:
#ifndef MY_LIBRARY_H
#define MY_LIBRARY_H
void myfunction();
#endif // !MY_LIBRARY_H
To compile a program that has multiple source files, with g++ we use:
g++ source.cpp mylibrary.cpp

Visual Studio IDE automatically handles the multiple file compilation.

32.3 Namespaces

Write a program that declares a function inside a namespace and defines the function outside the namespace. Invoke the function in the main program. Namespace and function names are arbitrary.
#include <iostream>
namespace MyNameSpace
{
    void myfunction();
}
void MyNameSpace::myfunction()
{
    std::cout << "Hello World from a function inside a namespace.";
}
int main()
{
    MyNameSpace::myfunction();
}

32.4 Nested Namespaces

Write a program that defines a namespace called A, and another namespace called B, nested inside the namespace A. Declare a function inside a namespace B and define the function outside both namespaces. Invoke the function in the main program. Then, introduce the entire namespace B to the current scope and invoke the function.
#include <iostream>
namespace A
{
    namespace B
    {
        void myfunction();
    }
}
void A::B::myfunction()
{
    std::cout << "Hello World from a function inside a nested namespace." << '\n';
}
int main()
{
    A::B::myfunction();
    using namespace A::B;
    myfunction();
}