Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please code this in C++, please read the instructions and have all three parts in the instructions A, B, and C done. For the seperate

Please code this in C++, please read the instructions and have all three parts in the instructions A, B, and C done.

For the seperate files just mention like this is what you should put in this file etc, don't use an links

----------------------------------------------------------------------------------------

The purpose of this assignment is to make sure that you know how to write a program that uses unnamed namespace, separate compilation, and external linkage.

PROGRAM SPECIFICATION

We will write a program that explores how the unnamed namespace works, uses separate compilation, and also gives external linkage a try. For this assignment we will print out a predefined array of characters with a user written function, we will create two namespaces that hold the same function name, and then we create another function as a normal function that illustrates the use of the global namespace. The number of array positions used is found in a constant aptly named SIZE, and it will not be passed as a parameter. Instead, we will try to pass (and share) this SIZE constant by using external linkage and by defining it in an unnamed namespace.

Included here is your driver code:

#include

#include "myFunction.hpp"

namespace

{

extern const int SIZE = 10;

}

void myFunction(char []);

namespace myNamespace1

{

void myGreeting();

}

namespace myNamespace2

{

void myGreeting();

}

void theGreeting();

int main()

{

char myArray[SIZE] = "123456789";

// call your function using only the array - to demonstrate the SIZE is sharable (external)

myFunction(myArray);

// call your second namespace greeting function

{

using namespace myNamespace2;

myGreeting();

}

// call your first namespace greeting function

{

using namespace myNamespace1;

myGreeting();

}

// call your first namespace greeting function

theGreeting();

return 0;

}

You need to:

A) Append the code to the driver (main) program the myGreeting function defined in myNamespace1 and myNamespace2. These functions should simply display a message such as Hello from namespace 1, or something similar. Lastly in the driver program after these two namespaces, write theGreeting. This function simply display another hello, such as hey from the global namespace.

B) Create myFunction.cpp: this function simply goes through all of the characters in myArray (passed as an argument), and prints them one by one. This output should be 1 2 3 4 5 6 7 8 9. Do this using a for loop. You will use the SIZE constant variable as your upper bound in your for loop. SIZE is not passed, and has external linkage. That means that the variable is visible to the linker from other files, i.e. it is globally visible and can be shared between translation units.

C) Create myFunction.hpp: this header simply include your myFunction.cpp. This function may or may not be used to help out with the SIZE constant. Alternatively, you may wish to create another header file just with the purpose of holding the namespace with SIZE. These options are something you shall explore and decide upon yourself.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Recommend the key methods to improve service productivity.

Answered: 1 week ago