Skip to content

Commit 537131e

Browse files
author
XuhuaHuang
committed
Update examples on friend class
1 parent 0aea357 commit 537131e

3 files changed

Lines changed: 72 additions & 37 deletions

File tree

FriendClass/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
# Adding multiple executable to the project
99
# Right-click on each item to set as start-up project
1010
add_executable(square_rectangle "square_rectangle.cpp")
11+
add_executable(modern_square_rectangle "modern_square_rectangle.cpp")
1112
add_executable(time_date "time_date.cpp")
1213

14+
set_target_properties(modern_square_rectangle PROPERTIES CXX_STANDARD 23)
15+
1316
add_subdirectory("TimeDate")

FriendClass/modern_square_rectangle.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <iostream>
1313

14+
class square_t; // forward declaration
15+
1416
class rectangle_t {
1517
private:
1618
int width{0};
@@ -46,6 +48,24 @@ class rectangle_t {
4648
rectangle_t double_in_size() const noexcept {
4749
return rectangle_t(width * 2, height * 2);
4850
}
51+
52+
void convert(const square_t& sq) noexcept;
53+
};
54+
55+
class square_t {
56+
friend class rectangle_t; // allow rectangle_t to access private data
57+
58+
private:
59+
int side{0};
60+
61+
public:
62+
explicit constexpr inline square_t(int s)
63+
: side(s) {}
64+
65+
[[nodiscard]]
66+
int get_side() const noexcept {
67+
return side;
68+
}
4969
};
5070

5171
// Optional: keep a free function wrapper if needed
@@ -54,6 +74,11 @@ rectangle_t double_in_size(const rectangle_t& rect) noexcept {
5474
return rect.double_in_size();
5575
}
5676

77+
void rectangle_t::convert(const square_t& sq) noexcept {
78+
width = sq.side;
79+
height = sq.side;
80+
}
81+
5782
int main() {
5883
rectangle_t r1;
5984
rectangle_t r2(2, 3);
@@ -62,5 +87,12 @@ int main() {
6287

6388
std::cout << r1.area() << std::endl;
6489

90+
rectangle_t rect;
91+
square_t sqr(4);
92+
93+
rect.convert(sqr);
94+
95+
std::cout << __LINE__ << ": " << rect.area() << std::endl;
96+
6597
return 0;
6698
}

FriendClass/square_rectangle.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
/*****************************************************************/ /**
2-
* \file square_rectangle.cpp
3-
* \brief class "Square" made friend of class
4-
*"rectangle"
5-
*
6-
* worth noting:
7-
* 1) granting the other class to access the
8-
*protected and private data members of the class
9-
*made friend with. 2) class friendship is not
10-
*bidirectional. Need to specify. 3) class
11-
*friendship is not transmissive: The friend of a
12-
*friend is not considered to be a friend unless
13-
*explicitly specified.
14-
*
15-
* \author Xuhua
16-
* \date November 2020
17-
*********************************************************************/
1+
// clang-format off
2+
/*****************************************************************//**
3+
* \file square_rectangle.cpp
4+
* \brief class "Square" made friend of class "rectangle"
5+
*
6+
* worth noting:
7+
* 1) granting the other class to access the
8+
* protected and private data members of the class
9+
* made friend with. 2) class friendship is not
10+
* bidirectional. Need to specify. 3) class
11+
* friendship is not transmissive: The friend of a
12+
* friend is not considered to be a friend unless
13+
* explicitly specified.
14+
*
15+
* \author Xuhua
16+
* \date November 2020
17+
*********************************************************************/
18+
// clang-format on
1819

1920
#include <iostream>
2021

2122
class Square {
2223
private:
23-
int side;
24+
int side;
2425

2526
public:
26-
inline void setSide(int a) { side = a; }
27+
inline void setSide(int a) { side = a; }
2728

28-
// Square class allows Rectangle class to access its private data members.
29-
friend class Rectangle;
30-
// conflict the concept of class encapsulation
31-
// could be avoided by using public get functions (getter)
29+
// Square class allows Rectangle class to access its private data members.
30+
friend class Rectangle;
31+
// conflict the concept of class encapsulation
32+
// could be avoided by using public get functions (getter)
3233
};
3334

3435
class Rectangle {
3536
private:
36-
int width, height;
37+
int width, height;
3738

3839
public:
39-
constexpr inline int area() { return (width * height); }
40-
void convert(const Square& rhs);
40+
constexpr inline int area() const { return (width * height); }
41+
void convert(const Square& rhs);
4142
};
4243

4344
void Rectangle::convert(const Square& a) {
44-
width = a.side;
45-
height = a.side;
46-
// Rectangle class using Square's private data members (side)
47-
48-
return;
45+
width = a.side;
46+
height = a.side;
47+
// Rectangle class using Square's private data members (side)
48+
return;
4949
}
5050

5151
int main(void) {
52-
Square sqr;
53-
Rectangle rect;
52+
Square sqr;
53+
Rectangle rect;
5454

55-
sqr.setSide(4);
56-
rect.convert(sqr); // converting a square to rectangle
55+
sqr.setSide(4);
56+
rect.convert(sqr); // converting a square to rectangle
5757

58-
std::cout << rect.area() << "\n";
58+
std::cout << rect.area() << "\n";
5959

60-
return 0;
60+
return 0;
6161
}

0 commit comments

Comments
 (0)