forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace.cpp
More file actions
70 lines (53 loc) · 1.89 KB
/
Copy pathnamespace.cpp
File metadata and controls
70 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
namespace square {
using namespace std; // Only applies inside namespace square
int area (int s) {
return s * s;
}
int perimeter (int s) {
return 4 * s;
}
void print(int s) {
cout << "Square with side " << s << " perimeter="
<< perimeter(s) << " area=" << area(s) << endl;
}
}
namespace circle {
constexpr double PI = 3.14159;
double area(int r) {
return PI * r * r;
}
double perimeter(int r) {
return 2 * PI * r;
}
void print(int s) {
std::cout << "Circle with radius " << s << " perimeter="
<< perimeter(s) << " area=" << area(s) << std::endl;
}
}
void circlesquare(int x) {
// Specify where we want area & perimeter to come from
using circle::area;
using square::perimeter;
std::cout << "The area of a circle with radius " << x << " is " << area(x) << std::endl;
std::cout << "The perimeter of a square with side " << x << " is " << perimeter(x) << std::endl;
}
int main () {
std::cout << "print function:" << std::endl;
square::print(10);
circle::print(10);
std::cout << std::endl;
std::cout << "area & perimeter functions:" << std::endl;
std::cout << "The area of a square with a side of 4 is " << square::area(4) << std::endl;
std::cout << "The perimeter of a square with a side of 4 is " << square::perimeter(4) << std::endl;
std::cout << "The area of a circle with radius 5 is " << circle::area(5) << std::endl;
std::cout << "The perimeter of a circle with radius 5 is " << circle::perimeter(5) << std::endl;
std::cout << std::endl;
// We have to use the SRO here to access the value of PI
std::cout << "PI = " << circle::PI << std::endl;
std::cout << std::endl;
std::cout << "circlesquare:" << std::endl;
circlesquare(7);
circlesquare(3);
return 0;
}