Skip to content

Commit eb33d7b

Browse files
committed
Sync with video
1 parent 793df54 commit eb33d7b

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

cpp/cpp5/namespace.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,57 @@ namespace square {
1212
}
1313

1414
void print(int s) {
15-
cout << "Square with side " << s << " perimeter=" << perimeter(s) << " area=" << area(s) << endl;
16-
}
15+
cout << "Square with side " << s << " perimeter="
16+
<< perimeter(s) << " area=" << area(s) << endl;
17+
}
1718
}
1819

1920
namespace circle {
21+
constexpr double PI = 3.14159;
2022

21-
const int PI = 3.14159;
22-
23-
double area (int r) {
23+
double area(int r) {
2424
return PI * r * r;
2525
}
2626

27-
double perimeter (int r) {
28-
return 2 * r * PI;
27+
double perimeter(int r) {
28+
return 2 * PI * r;
29+
}
30+
31+
void print(int s) {
32+
std::cout << "Circle with radius " << s << " perimeter="
33+
<< perimeter(s) << " area=" << area(s) << std::endl;
2934
}
30-
31-
void print(int r) {
32-
std::cout << "Circle with radius " << r << " perimeter=" << perimeter(r) << " area=" << area(r) << std::endl;
33-
}
3435
}
3536

36-
void func(int x) {
37+
void circlesquare(int x) {
3738
using circle::area;
3839
using square::perimeter;
39-
40-
std::cout << "Shape x=" << x << " perimeter=" << perimeter(x) << " area=" << area(x) << std::endl;
40+
41+
std::cout << "The area of a circle with radius " << x << " is " << area(x) << std::endl;
42+
std::cout << "The perimeter of a square with side " << x << " is " << perimeter(x) << std::endl;
4143
}
4244

4345
int main () {
4446

45-
std::cout << "Namespace demonstration:" << std::endl;
47+
std::cout << "print function:" << std::endl;
4648
square::print(10);
4749
circle::print(10);
4850

4951
std::cout << std::endl;
50-
func(4);
51-
func(5);
52+
std::cout << "area & perimeter functions:" << std::endl;
53+
std::cout << "The area of a square with a side of 4 is " << square::area(4) << std::endl;
54+
std::cout << "The perimeter of a square with a side of 4 is " << square::perimeter(4) << std::endl;
55+
56+
std::cout << "The area of a circle with radius 5 is " << circle::area(5) << std::endl;
57+
std::cout << "The perimeter of a circle with radius 5 is " << circle::perimeter(5) << std::endl;
58+
std::cout << std::endl;
59+
60+
std::cout << "PI = " << circle::PI << std::endl;
61+
std::cout << std::endl;
62+
63+
std::cout << "circlesquare:" << std::endl;
64+
circlesquare(7);
65+
circlesquare(3);
5266

5367
return 0;
5468
}

0 commit comments

Comments
 (0)