private and public in namespace cpp

Solutions on MaxInterview for private and public in namespace cpp by the best coders in the world

showing results for - "private and public in namespace cpp"
Jason
27 Aug 2016
1// thing.cpp
2
3namespace thing
4{
5    namespace   // anonymous namespace
6    {
7        int x = 1;
8        int y = 2;
9
10        int sum(int a, int b)
11        {
12            return a + b;
13        }
14    }
15
16    int getX() 
17    {
18        return x;
19    }
20
21    int getSum()
22    {
23        return sum(x, y);
24    }
25};
26
Giacomo
14 Mar 2019
1#include <cstdio>
2#include "thing.hpp"
3
4int main(int argc, char **argv)
5{
6    printf("%d\n", thing::getX());     // OK
7    printf("%d\n", thing::getSum());   // OK
8    printf("%d\n", thing::sum(1, 2));  // error: ‘sum‘ is not a member of ‘thing’    
9    printf("%d\n", thing::y);          // error: ‘y‘ is not a member of ‘thing’    
10}
11