Week 01 / 14 Object-Oriented Programming
Review - tooling and memory
How the course works, how to work in Visual Studio, which kinds of errors you will meet, where variables live in memory, and how parameters are passed.
- Know the course structure and how you are assessed
- Create, build and run a C++ project in Visual Studio
- Tell compile, link, runtime and logical errors apart
- Explain the difference between the stack, the heap and static memory
- Use the debugger and the Memory window
- Choose between passing by value, by reference and by address
Course structure and assessment
The course runs for 14 weeks, with one lecture and one lab each week. In the labs we write code, break it, and fix it.
| Week | Lecture |
|---|---|
| 1 | Review: tooling, errors, memory, debugger, parameters |
| 2 | Review: pointers, const, arrays, dynamic allocation |
| 3 | Introduction to classes |
| 4 | Constructors, destructor, accessors |
| 5 | Const and static members, operator =, the βhas aβ relationship |
| 6 | Overloading I: functions, operators, friend |
| 7 | Overloading II: common operators |
| 8 | Inheritance, exceptions |
| 9 | Streams and files |
| 10 | Overriding I: namespaces, multiple inheritance, virtual functions |
| 11 | Overriding II: pure virtual functions, abstract classes, interfaces |
| 12 | Templates |
| 13 | STL |
| 14 | C++11 and final review |
There are two live tests in the labs, in weeks 7 and 11. Both are individual, on a computer, and AI-free.
The Visual Studio environment
- File β New β Project β Console App (C++).
- Write your code in
main.cpp. - Build β Build Solution (Ctrl+Shift+B) compiles and links.
- Debug β Start Without Debugging (Ctrl+F5) runs the program.
Errors show up in the Error List window. The full compiler messages are in the Output window. Always read the first error, because many of the ones after it are only consequences of it.
Kinds of errors
Compile error. The code breaks the rules of the language:
int x = 10
std::cout << x; // missing ';' on the previous line
Link error. The function is declared, but never defined:
int sum(int a, int b); // declaration only
int main() { return sum(1, 2); } // LNK2019: unresolved external symbol
Runtime error. The program compiles, but stops while running:
int* p = nullptr;
*p = 5; // access through an invalid address
Logical error. The program runs and prints something, but not what you wanted. These are the hardest to find.
We want the sum of the array elements. What does the program print?
#include <iostream>
using namespace std;
int main() {
int v[3] = {1, 2, 3};
int s = 0;
for (int i = 1; i <= 2; i++) s += v[i];
cout << s;
} Kinds of memory
- Stack: local variables and parameters live here. They are created when a function starts and destroyed automatically when it returns.
- Heap: memory you request explicitly with
newand release explicitly withdelete. It lives until you release it. - Static memory: global and
staticvariables. They live for the whole program.
int a = 3; int* p; - both are local variables on the stack.
Step 1/3
Debugger and the Memory window
- Breakpoint (F9): execution stops on that line.
- Step Over (F10): runs the current line without entering functions.
- Step Into (F11): enters the called function.
- Watch / Locals / Autos: variable values at the point where execution stopped.
- Debug β Windows β Memory β Memory 1: type an address (e.g.
&aorp) and see the bytes stored there.
Before you press F10, say out loud which value you expect to see. Then check.
Passing parameters
| Mode | Signature | Can the function change the callerβs variable? |
|---|---|---|
| By value | void f(int x) | No, it works on a copy |
| By reference | void f(int& x) | Yes, x is another name for the variable |
| By address | void f(int* x) | Yes, through *x |
#include <iostream>
using namespace std;
void doubleIt(int x) { x = x * 2; }
int main() {
int a = 5;
doubleIt(a);
cout << a;
} Run and change the program below. What happens if sumByValue takes int& s instead?
#include <iostream>
using namespace std;
void sumByValue(int a, int b, int s) { s = a + b; }
void sumByReference(int a, int b, int& s) { s = a + b; }
void sumByAddress(int* a, int* b, int* s) { *s = *a + *b; }
int main() {
int x = 3, y = 4, s = 0;
sumByValue(x, y, s);
cout << "value: " << s << endl;
sumByReference(x, y, s);
cout << "reference: " << s << endl;
s = 0;
sumByAddress(&x, &y, &s);
cout << "address: " << s << endl;
}Input (stdin)
Check yourself
Score: 0/6