Skip to content

Commit 4a682bd

Browse files
committed
Add 03-advanced structs-and-classes module
1 parent 60f70b3 commit 4a682bd

8 files changed

Lines changed: 254 additions & 8 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Current C++ core modules:
3939
- `02-core/input-validation`
4040
- `02-core/algorithms-basics`
4141

42+
Current C++ advanced modules:
43+
44+
- `03-advanced/structs-and-classes`
45+
4246
## Guided Learning Path
4347

4448
1. Complete setup: `languages/cpp/00-setup/README.md`
@@ -47,7 +51,7 @@ Current C++ core modules:
4751
- read `README.md`
4852
- run `example/main.cpp`
4953
- solve `exercises/01.cpp` and `exercises/02.cpp`
50-
- after foundations, continue with `languages/cpp/02-core`
54+
- after foundations, continue with `languages/cpp/02-core`, then `languages/cpp/03-advanced`
5155
4. Mark progress in `languages/cpp/CHECKLIST.md`
5256
5. Repeat until all modules are complete
5357

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# 03 Advanced
22

3-
This level is reserved for advanced C++ topics after core proficiency.
3+
This level introduces object modeling and design patterns after core proficiency.
44

5-
## Intended Topics
5+
## Module Order
66

7-
- Object-oriented design and class modeling
8-
- Resource management and RAII
9-
- Generic programming foundations
7+
1. [structs-and-classes](./structs-and-classes/README.md)
108

11-
## Status
9+
## How To Study This Level
1210

13-
This level folder is ready for future modules.
11+
For each module:
12+
13+
1. Read `README.md`.
14+
2. Run `example/main.cpp`.
15+
3. Complete `exercises/01.cpp` and `exercises/02.cpp`.
16+
4. Mark progress in `../CHECKLIST.md`.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Structs and Classes
2+
3+
This module introduces object modeling with `struct` and `class` in C++.
4+
5+
## Why It Matters
6+
7+
As programs grow, related data and behavior should be grouped together.
8+
`struct` and `class` help you model real entities clearly.
9+
10+
## Quick Run
11+
12+
```bash
13+
g++ -std=c++17 -Wall -Wextra -pedantic example/main.cpp -o structs_and_classes_example
14+
./structs_and_classes_example
15+
```
16+
17+
On Windows (MSYS2 shell), run:
18+
19+
```bash
20+
./structs_and_classes_example.exe
21+
```
22+
23+
## Topics Covered
24+
25+
### `struct` vs `class`
26+
27+
- `struct` members are `public` by default.
28+
- `class` members are `private` by default.
29+
- Both can have fields, methods, and constructors.
30+
31+
### Constructors
32+
33+
Constructors initialize objects when they are created.
34+
35+
```cpp
36+
class Account {
37+
public:
38+
Account(const std::string& ownerName, double initialBalance);
39+
};
40+
```
41+
42+
### Encapsulation
43+
44+
Keep object state private and expose safe methods (`deposit`, `withdraw`, getters).
45+
46+
## Common Pitfalls
47+
48+
- Exposing all fields publicly in classes that should protect state.
49+
- Forgetting `const` on methods that do not modify object state.
50+
- Not validating values in constructors or mutating methods.
51+
52+
## Exercise Focus
53+
54+
- `exercises/01.cpp`: model a rectangle with a `struct` and member methods.
55+
- `exercises/02.cpp`: create a small class with private state and controlled updates.
56+
57+
## Checkpoint
58+
59+
- [ ] I can explain default access in `struct` and `class`.
60+
- [ ] I can write and use constructors.
61+
- [ ] I can protect state with private fields and public methods.
62+
- [ ] I completed both exercises.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
5+
struct Student {
6+
std::string name;
7+
int age;
8+
double grade;
9+
10+
void print() const {
11+
std::cout << "Student{name=\"" << name << "\", age=" << age
12+
<< ", grade=" << grade << "}\n";
13+
}
14+
};
15+
16+
class BankAccount {
17+
public:
18+
BankAccount(const std::string& ownerName, double initialBalance)
19+
: owner(ownerName), balance(initialBalance) {
20+
if (balance < 0.0) {
21+
balance = 0.0;
22+
}
23+
}
24+
25+
bool deposit(double amount) {
26+
if (amount <= 0.0) {
27+
return false;
28+
}
29+
balance += amount;
30+
return true;
31+
}
32+
33+
bool withdraw(double amount) {
34+
if (amount <= 0.0 || amount > balance) {
35+
return false;
36+
}
37+
balance -= amount;
38+
return true;
39+
}
40+
41+
const std::string& getOwner() const {
42+
return owner;
43+
}
44+
45+
double getBalance() const {
46+
return balance;
47+
}
48+
49+
private:
50+
std::string owner;
51+
double balance;
52+
};
53+
54+
int main() {
55+
std::vector<Student> students{
56+
{"Alex Johnson", 19, 8.7},
57+
{"Maya Patel", 20, 9.1}
58+
};
59+
60+
std::cout << "Students (struct example):\n";
61+
for (const Student& student : students) {
62+
student.print();
63+
}
64+
65+
BankAccount account("Alex Johnson", 100.0);
66+
account.deposit(40.0);
67+
account.withdraw(25.0);
68+
69+
std::cout << "\nBank account (class example):\n";
70+
std::cout << "Owner: " << account.getOwner() << '\n';
71+
std::cout << "Balance: " << account.getBalance() << '\n';
72+
73+
return 0;
74+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
3+
struct Rectangle {
4+
double width;
5+
double height;
6+
7+
double area() const {
8+
return width * height;
9+
}
10+
11+
double perimeter() const {
12+
return 2.0 * (width + height);
13+
}
14+
};
15+
16+
int main() {
17+
Rectangle rectangle{0.0, 0.0};
18+
19+
std::cout << "Enter width and height: ";
20+
std::cin >> rectangle.width >> rectangle.height;
21+
22+
if (rectangle.width <= 0.0 || rectangle.height <= 0.0) {
23+
std::cout << "Width and height must be positive.\n";
24+
return 0;
25+
}
26+
27+
std::cout << "Area: " << rectangle.area() << '\n';
28+
std::cout << "Perimeter: " << rectangle.perimeter() << '\n';
29+
return 0;
30+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <limits>
3+
#include <string>
4+
5+
class Counter {
6+
public:
7+
Counter() : value(0) {}
8+
9+
void increment() {
10+
++value;
11+
}
12+
13+
void decrement() {
14+
--value;
15+
}
16+
17+
void reset() {
18+
value = 0;
19+
}
20+
21+
int getValue() const {
22+
return value;
23+
}
24+
25+
private:
26+
int value;
27+
};
28+
29+
int main() {
30+
Counter counter;
31+
std::string command;
32+
33+
std::cout << "Commands: inc, dec, reset, stop\n";
34+
while (true) {
35+
std::cout << "Enter command: ";
36+
if (!(std::cin >> command)) {
37+
std::cin.clear();
38+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
39+
continue;
40+
}
41+
42+
if (command == "inc") {
43+
counter.increment();
44+
} else if (command == "dec") {
45+
counter.decrement();
46+
} else if (command == "reset") {
47+
counter.reset();
48+
} else if (command == "stop") {
49+
break;
50+
} else {
51+
std::cout << "Unknown command.\n";
52+
continue;
53+
}
54+
55+
std::cout << "Current value: " << counter.getValue() << '\n';
56+
}
57+
58+
std::cout << "Final value: " << counter.getValue() << '\n';
59+
return 0;
60+
}

languages/cpp/CHECKLIST.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@
2424
- [ ] input-validation exercises completed.
2525
- [ ] algorithms-basics: implement linear search, counting, and min/max patterns.
2626
- [ ] algorithms-basics exercises completed.
27+
28+
## Advanced
29+
30+
- [ ] structs-and-classes: model entities with constructors and encapsulation.
31+
- [ ] structs-and-classes exercises completed.

languages/cpp/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Current concept modules inside `02-core`:
3131
- [input-validation](./02-core/input-validation/README.md)
3232
- [algorithms-basics](./02-core/algorithms-basics/README.md)
3333

34+
Current concept modules inside `03-advanced`:
35+
36+
- [structs-and-classes](./03-advanced/structs-and-classes/README.md)
37+
3438
Use the checklist: [CHECKLIST.md](./CHECKLIST.md)
3539

3640
## Concept Folder Structure
@@ -66,6 +70,10 @@ After finishing all concepts in `01-foundations`, continue with `02-core` in thi
6670
1. `input-validation`
6771
2. `algorithms-basics`
6872

73+
Then continue with `03-advanced`:
74+
75+
1. `structs-and-classes`
76+
6977
## Build Command
7078

7179
```bash

0 commit comments

Comments
 (0)