Phần 1
1. Goal
Before learning modern C++, you must understand the basic relationship between objects, variables, and memory.
After this lesson, you should understand:
1. What a variable is.
2. What an object is.
3. How variables relate to memory.
4. Why initialization matters.
5. Common mistakes with object lifetime and copying.Phần 2
2. What Problem Does This Solve?
C++ programs work with data stored in memory.
A variable gives a name to a value or object so the program can access it.
Main idea:
A variable is a name.
An object is the actual entity stored in memory.
Memory is where objects live.Example concepts:
price
quantity
side
order id
timestampThese are all represented by objects in memory and accessed through variables.
Phần 3
3. Minimum Syntax
Declare and initialize a variable:
type name = value;Examples:
int price = 100;
double size = 10.5;
char side = 'B';Declare first, assign later:
int quantity;
quantity = 50;Main idea:
Always prefer initialization when creating a variable.Phần 4
4. Object vs Variable
A variable is the name used in source code.
An object is the actual typed storage in memory.
Key idea:
variable name -> refers to -> object in memoryFor simple local variables, the distinction is often hidden.
But understanding it becomes important when learning:
references
pointers
object lifetime
copying
constructors
destructors
move semanticsPhần 5
5. Common Mistakes
5.1. Using an uninitialized variable
An uninitialized local variable may contain an unpredictable value.
This is dangerous because the program may appear to work sometimes and fail later.
Prefer:
Initialize variables immediately.5.2. Confusing copy with sharing
When one simple variable is assigned to another, the value is copied.
Changing the new variable does not change the old one.
Main idea:
copy means independent value
sharing requires references or pointers5.3. Using an object after its lifetime ends
A local object only lives inside its block.
After leaving the block, the object no longer exists.
This becomes very important when learning pointers and references.
Phần 6
6. Notes for Trading Code
Trading systems often store data such as:
bid price
ask price
quantity
spread
order id
timestampThese values must be initialized correctly.
A wrong or uninitialized value can cause incorrect calculations, invalid orders, or risky trading decisions.
Main idea:
In trading code, bad memory assumptions can become real financial bugs.Phần 7
7. End-of-Day Checklist
You should be able to answer:
1. What is a variable?
2. What is an object?
3. Where does an object live?
4. Why is initialization important?
5. What happens when a value is copied?
6. When does a local object die?Phần 8
8. Conclusion
Object, variable, and memory are the foundation of C++.
The key idea:
A variable is a name.
An object is the real thing stored in memory.
Initialization makes the object start in a valid state.Mã mẫu hoàn chỉnh
cpp98_foundation/object variable memory/main.cpp
#include <iostream>
int main() {
int bidPrice = 100;
int askPrice = 101;
int quantity = 50;
int spread = askPrice - bidPrice;
int notional = askPrice * quantity;
std::cout << "Bid: " << bidPrice << "\n";
std::cout << "Ask: " << askPrice << "\n";
std::cout << "Spread: " << spread << "\n";
std::cout << "Notional: " << notional << "\n";
return 0;
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Đã tự kiểm tra 0/6 mục