Skip to content

Commit 03834d2

Browse files
committed
docs: clear notes and add examples
1 parent 334fda7 commit 03834d2

7 files changed

Lines changed: 552 additions & 266 deletions

File tree

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,3 @@
1-
2-
3-
4-
5-
6-
7-
|
8-
9-
---
10-
[Keywords](../keywords.html "cpp/keyword")
11-
12-
| [alignas](alignas.html "cpp/keyword/alignas")(C++11)
13-
---
14-
[alignof](alignof.html "cpp/keyword/alignof")(C++11)
15-
[and](and.html "cpp/keyword/and")
16-
[and_eq](and_eq.html "cpp/keyword/and eq")
17-
[asm](asm.html "cpp/keyword/asm")
18-
[auto](auto.html "cpp/keyword/auto")(*)
19-
[bitand](bitand.html "cpp/keyword/bitand")
20-
[bitor](bitor.html "cpp/keyword/bitor")
21-
[bool](bool.html "cpp/keyword/bool")
22-
[break](break.html "cpp/keyword/break")
23-
[case](case.html "cpp/keyword/case")
24-
[catch](catch.html "cpp/keyword/catch")
25-
[char](char.html "cpp/keyword/char")
26-
[char8_t](char8_t.html "cpp/keyword/char8 t")(C++20)
27-
[char16_t](char16_t.html "cpp/keyword/char16 t")(C++11)
28-
[char32_t](char32_t.html "cpp/keyword/char32 t")(C++11)
29-
[class](class.html "cpp/keyword/class")
30-
[compl](compl.html "cpp/keyword/compl")
31-
[concept](concept.html "cpp/keyword/concept")(C++20)
32-
[const](const.html "cpp/keyword/const")
33-
[consteval](consteval.html "cpp/keyword/consteval")(C++20)
34-
[constexpr](constexpr.html "cpp/keyword/constexpr")(C++11)
35-
[constinit](constinit.html "cpp/keyword/constinit")(C++20)
36-
[const_cast](const_cast.html "cpp/keyword/const cast")
37-
[continue](continue.html "cpp/keyword/continue")
38-
[contract_assert](contract_assert.html "cpp/keyword/contract assert")(C++26)` `
39-
[co_await](co_await.html "cpp/keyword/co await")(C++20)
40-
[co_return](co_return.html "cpp/keyword/co return")(C++20)
41-
[co_yield](co_yield.html "cpp/keyword/co yield")(C++20)
42-
[decltype](../keywords/decltype.html "cpp/keyword/decltype")(C++11)
43-
[default](default.html "cpp/keyword/default")
44-
45-
| [delete](delete.html "cpp/keyword/delete")
46-
---
47-
[do](do.html "cpp/keyword/do")
48-
[double](double.html "cpp/keyword/double")
49-
[dynamic_cast](dynamic_cast.html "cpp/keyword/dynamic cast")
50-
[else](else.html "cpp/keyword/else")
51-
[enum](enum.html "cpp/keyword/enum")
52-
[explicit](explicit.html "cpp/keyword/explicit")
53-
[export](export.html "cpp/keyword/export")
54-
[extern](extern.html "cpp/keyword/extern")
55-
[false](false.html "cpp/keyword/false")
56-
[float](float.html "cpp/keyword/float")
57-
[for](for.html "cpp/keyword/for")
58-
**friend**
59-
[goto](goto.html "cpp/keyword/goto")
60-
[if](../keywords/if.html "cpp/keyword/if")
61-
[inline](inline.html "cpp/keyword/inline")
62-
[int](int.html "cpp/keyword/int")
63-
[long](long.html "cpp/keyword/long")
64-
[mutable](mutable.html "cpp/keyword/mutable")
65-
[namespace](namespace.html "cpp/keyword/namespace")
66-
[new](new.html "cpp/keyword/new")
67-
[noexcept](noexcept.html "cpp/keyword/noexcept")(C++11)` `
68-
[not](not.html "cpp/keyword/not")
69-
[not_eq](not_eq.html "cpp/keyword/not eq")
70-
[nullptr](nullptr.html "cpp/keyword/nullptr")(C++11)
71-
[operator](operator.html "cpp/keyword/operator")
72-
[or](or.html "cpp/keyword/or")
73-
[or_eq](or_eq.html "cpp/keyword/or eq")
74-
[private](private.html "cpp/keyword/private")
75-
[protected](protected.html "cpp/keyword/protected")
76-
[public](public.html "cpp/keyword/public")
77-
78-
| [register](register.html "cpp/keyword/register")
79-
---
80-
[reinterpret_cast](reinterpret_cast.html "cpp/keyword/reinterpret cast")
81-
[requires](requires.html "cpp/keyword/requires")(C++20)
82-
[return](return.html "cpp/keyword/return")
83-
[short](short.html "cpp/keyword/short")
84-
[signed](signed.html "cpp/keyword/signed")
85-
[sizeof](sizeof.html "cpp/keyword/sizeof")(*)
86-
[static](../keywords/static.html "cpp/keyword/static")
87-
[static_assert](static_assert.html "cpp/keyword/static assert")(C++11)
88-
[static_cast](static_cast.html "cpp/keyword/static cast")
89-
[struct](struct.html "cpp/keyword/struct")
90-
[switch](switch.html "cpp/keyword/switch")
91-
[template](template.html "cpp/keyword/template")
92-
[this](this.html "cpp/keyword/this")
93-
[thread_local](thread_local.html "cpp/keyword/thread local")(C++11)
94-
[throw](throw.html "cpp/keyword/throw")
95-
[true](true.html "cpp/keyword/true")
96-
[try](try.html "cpp/keyword/try")
97-
[typedef](typedef.html "cpp/keyword/typedef")
98-
[typeid](typeid.html "cpp/keyword/typeid")
99-
[typename](typename.html "cpp/keyword/typename")
100-
[union](union.html "cpp/keyword/union")
101-
[unsigned](unsigned.html "cpp/keyword/unsigned")
102-
[using](using.html "cpp/keyword/using")
103-
[virtual](virtual.html "cpp/keyword/virtual")
104-
[void](void.html "cpp/keyword/void")
105-
[volatile](volatile.html "cpp/keyword/volatile")
106-
[wchar_t](wchar_t.html "cpp/keyword/wchar t")
107-
[while](while.html "cpp/keyword/while")
108-
[xor](xor.html "cpp/keyword/xor")
109-
[xor_eq](xor_eq.html "cpp/keyword/xor eq")
110-
111-
112-
113-
114-
115-
1161
### Usage
1172

118-
* [`friend` specifier](../language/friend.html "cpp/language/friend")
119-
120-
3+
* [`friend` specifier](../language/friend.md)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# [friend declaration](declaration.cpp)
2+
3+
# [syntax](syntax.cpp)
4+
5+
# TODO:
6+
7+
# [example](example.cpp)
8+
* non-member friends
9+
* use cases
10+
* stream insertion and extraction operators
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
// ============================================
5+
// Friend: grants access to private & protected members
6+
// Uses: | class body
7+
// ============================================
8+
9+
// 1. friend function
10+
class BankAccount
11+
{
12+
private:
13+
std::string owner;
14+
double balance;
15+
16+
protected:
17+
std::string account_number;
18+
19+
public:
20+
BankAccount(const std::string& owner_, double balance_)
21+
: owner(owner_), balance(balance_), account_number("ACC-12345") {}
22+
23+
// 1.1 friend function declaration
24+
// → grants access to private & protected members
25+
friend void print_account_details(const BankAccount& acc);
26+
27+
// 1.2 friend function definition AVAILABLE ALSO | class body
28+
friend void add_interest(BankAccount& acc, double rate)
29+
{
30+
acc.balance += acc.balance * rate; // balance private member
31+
std::cout << "Interest added. New balance: $" << acc.balance << std::endl;
32+
}
33+
};
34+
35+
// friend function definition -- from -- PREVIOUSLY being declared | class body
36+
void print_account_details(const BankAccount& acc)
37+
{
38+
std::cout << "Account Details:" << std::endl;
39+
std::cout << " Owner: " << acc.owner << std::endl; // private member
40+
std::cout << " Balance: $" << acc.balance << std::endl; // private member
41+
std::cout << " Account #: " << acc.account_number << std::endl; // protected member
42+
}
43+
44+
// 2. friend class accessing private & protected members
45+
// Forward declaration for friend class example
46+
class Mechanic;
47+
48+
class Car
49+
{
50+
private:
51+
std::string model;
52+
int engine_temp;
53+
bool engine_running;
54+
55+
protected:
56+
std::string vin;
57+
58+
public:
59+
Car(const std::string& model_)
60+
: model(model_), engine_temp(90), engine_running(false), vin("VIN123456") {}
61+
62+
// 2.1 friend class declaration | class body
63+
// → grants Mechanic access -- to -- Car's private & protected members
64+
friend class Mechanic;
65+
66+
void display() const
67+
{
68+
std::cout << "Car: " << model << std::endl;
69+
}
70+
};
71+
72+
// 2.1.1 friend class can access Car's private & protected members
73+
class Mechanic
74+
{
75+
std::string name;
76+
77+
public:
78+
Mechanic(const std::string& name_) : name(name_) {}
79+
80+
// access Car's private members
81+
void diagnose(const Car& car) const
82+
{
83+
std::cout << "\nMechanic " << name << " diagnosing:" << std::endl;
84+
std::cout << " Model: " << car.model << " (private)" << std::endl;
85+
std::cout << " Engine temp: " << car.engine_temp << "°C (private)" << std::endl;
86+
std::cout << " Running: " << (car.engine_running ? "Yes" : "No") << " (private)" << std::endl;
87+
std::cout << " VIN: " << car.vin << " (protected)" << std::endl;
88+
}
89+
90+
// can modify Car's private members
91+
void start_engine(Car& car) const
92+
{
93+
std::cout << "\nMechanic " << name << " starting engine..." << std::endl;
94+
car.engine_running = true; // Access private member
95+
car.engine_temp = 120; // Modify private member
96+
std::cout << " Engine started!" << std::endl;
97+
}
98+
99+
void stop_engine(Car& car) const
100+
{
101+
car.engine_running = false;
102+
car.engine_temp = 90;
103+
}
104+
};
105+
106+
int main()
107+
{
108+
std::cout << "=== Friend: Access Grant Examples ===" << std::endl;
109+
std::cout << std::endl;
110+
111+
// Example 1: Friend function
112+
std::cout << "1. Friend Function Example:" << std::endl;
113+
BankAccount acc("Alice", 1000.0);
114+
print_account_details(acc); // Can access private & protected members
115+
add_interest(acc, 0.05);
116+
std::cout << std::endl;
117+
118+
// Example 2: Friend class
119+
std::cout << "2. Friend Class Example:" << std::endl;
120+
Car car("Toyota Corolla");
121+
car.display();
122+
123+
Mechanic mechanic("Bob");
124+
mechanic.diagnose(car); // Can access ALL private & protected members
125+
mechanic.start_engine(car); // Can modify private members
126+
mechanic.diagnose(car);
127+
std::cout << std::endl;
128+
129+
std::cout << "=== Summary ===" << std::endl;
130+
std::cout << "✓ Friend function: grants access to private & protected members" << std::endl;
131+
std::cout << "✓ Friend class: grants ALL members access to private & protected" << std::endl;
132+
std::cout << "✓ Friend declarations appear | class body" << std::endl;
133+
134+
return 0;
135+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <sstream>
3+
4+
class MyClass
5+
{
6+
int i; // friends have access to non-public, non-static
7+
static inline int id{6}; // and static (possibly inline) members
8+
9+
friend std::ostream& operator<<(std::ostream& out, const MyClass&);
10+
friend std::istream& operator>>(std::istream& in, MyClass&);
11+
friend void change_id(int);
12+
public:
13+
MyClass(int i = 0) : i(i) {}
14+
};
15+
16+
std::ostream& operator<<(std::ostream& out, const MyClass& mc)
17+
{
18+
return out << "MyClass::id = " << MyClass::id << "; i = " << mc.i;
19+
}
20+
21+
std::istream& operator>>(std::istream& in, MyClass& mc)
22+
{
23+
return in >> mc.i;
24+
}
25+
26+
void change_id(int id) { MyClass::id = id; }
27+
28+
int main()
29+
{
30+
MyClass mc(7);
31+
std::cout << mc << '\n';
32+
// mc.i = 333*2; // error: i is a private member
33+
std::istringstream("100") >> mc;
34+
std::cout << mc << '\n';
35+
// MyClass::id = 222*3; // error: id is a private member
36+
change_id(9);
37+
std::cout << mc << '\n';
38+
}

0 commit comments

Comments
 (0)