CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #82

Same Name, Different Namespace, Different File

mediumModular Programming

📄 Code

Read carefully — what does this print?
1// ===== mathx.h =====
2#ifndef MATHX_H
3#define MATHX_H
4 
5namespace seneca {
6 int twice(int n);
7 const int LIMIT = 3;
8}
9 
10#endif
11 
12// ===== mathx.cpp =====
13#include "mathx.h"
14 
15namespace seneca {
16 int twice(int n) {
17 return n * 2;
18 }
19}
20 
21// ===== main.cpp =====
22#include <iostream>
23#include "mathx.h"
24using namespace std;
25 
26int twice(int n) {
27 return n * 3;
28}
29 
30int main() {
31 int total = 0;
32 for (int i = 1; i <= seneca::LIMIT; i++) {
33 total += seneca::twice(i);
34 }
35 cout << total << " " << twice(seneca::LIMIT) << endl;
36 return 0;
37}

🎯 Your Answer