Skip to content

Commit a5f9c06

Browse files
committed
CPP notes
- blocked-based class structure - pointer vs reference
1 parent 80f53af commit a5f9c06

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

content/C/CPP Notes.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author Profile:
66
tags:
77
- cpp
88
Creation Date: 2024-12-14, 20:50
9-
Last Date: 2024-12-14T21:03:15+08:00
9+
Last Date: 2024-12-16T22:07:33+08:00
1010
References:
1111
draft:
1212
description:
@@ -29,4 +29,35 @@ description:
2929
auto variable_name = expression;
3030
```
3131

32-
- The [[Language Processors#Compiler]] is able to figure out the [[Datatype]] of `expression` during compilation automatically
32+
- The [[Language Processors#Compiler]] is able to figure out the [[Datatype]] of `expression` during compilation automatically
33+
34+
## Block-based Class Structure
35+
---
36+
```cpp
37+
class Node {
38+
private:
39+
int val; // This is private by default
40+
int freq;
41+
42+
public:
43+
Node(int val, int freq) {
44+
this->val = val;
45+
this->freq = freq;
46+
}
47+
};
48+
49+
```
50+
51+
- In C++, [[Access Modifier|access modifiers]] (`public`, `private`, `protected`) are **block-based**. They apply to all members declared after them until another access modifier appears
52+
53+
54+
## Pointer vs Reference
55+
---
56+
```cpp
57+
int* ptr = &x; // Pointer points to the address of x
58+
59+
int& ref = x; // Create a reference to x
60+
```
61+
62+
- References in C++ are **an alias** for an existing variable
63+
- For [[C Structure|structure]], accessing attributes with pointer to a structure requires `->`, but reference can access directly with `.`

0 commit comments

Comments
 (0)