|
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 |
18 | 19 |
|
19 | 20 | #include <iostream> |
20 | 21 |
|
21 | 22 | class Square { |
22 | 23 | private: |
23 | | - int side; |
| 24 | + int side; |
24 | 25 |
|
25 | 26 | public: |
26 | | - inline void setSide(int a) { side = a; } |
| 27 | + inline void setSide(int a) { side = a; } |
27 | 28 |
|
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) |
32 | 33 | }; |
33 | 34 |
|
34 | 35 | class Rectangle { |
35 | 36 | private: |
36 | | - int width, height; |
| 37 | + int width, height; |
37 | 38 |
|
38 | 39 | 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); |
41 | 42 | }; |
42 | 43 |
|
43 | 44 | 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; |
49 | 49 | } |
50 | 50 |
|
51 | 51 | int main(void) { |
52 | | - Square sqr; |
53 | | - Rectangle rect; |
| 52 | + Square sqr; |
| 53 | + Rectangle rect; |
54 | 54 |
|
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 |
57 | 57 |
|
58 | | - std::cout << rect.area() << "\n"; |
| 58 | + std::cout << rect.area() << "\n"; |
59 | 59 |
|
60 | | - return 0; |
| 60 | + return 0; |
61 | 61 | } |
0 commit comments