OOP Search

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.

Objectives
  • 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.

WeekLecture
1Review: tooling, errors, memory, debugger, parameters
2Review: pointers, const, arrays, dynamic allocation
3Introduction to classes
4Constructors, destructor, accessors
5Const and static members, operator =, the β€œhas a” relationship
6Overloading I: functions, operators, friend
7Overloading II: common operators
8Inheritance, exceptions
9Streams and files
10Overriding I: namespaces, multiple inheritance, virtual functions
11Overriding II: pure virtual functions, abstract classes, interfaces
12Templates
13STL
14C++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

  1. File β†’ New β†’ Project β†’ Console App (C++).
  2. Write your code in main.cpp.
  3. Build β†’ Build Solution (Ctrl+Shift+B) compiles and links.
  4. 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
a: 3
p: ?
HEAP

int a = 3; int* p; - both are local variables on the stack.

Step 1/3

Debugger and the Memory window

Before you press F10, say out loud which value you expect to see. Then check.

Passing parameters

ModeSignatureCan the function change the caller’s variable?
By valuevoid f(int x)No, it works on a copy
By referencevoid f(int& x)Yes, x is another name for the variable
By addressvoid 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)

Open in Godbolt

Check yourself

1. You declared int sum(int, int); and call it in main, but never wrote its body. What kind of error do you get?

2. Where does the int created by int* p = new int(7); live, and for how long?

3. What is r after int a = 1; int& r = a;?

4. Which of these are runtime errors (the program compiles, but stops)?

5. What does the program print?
#include <iostream>
using namespace std;
void f(int x) { x++; }
void g(int& x) { x++; }
int main() {
    int a = 1;
    f(a); g(a); g(a);
    cout << a;
}

6. What does the program print?
#include <iostream>
using namespace std;
void change(int* p) { *p = 10; p = nullptr; }
int main() {
    int a = 1;
    int* q = &a;
    change(q);
    cout << a << " " << (q != nullptr);
}

Score: 0/6

Code in repository: L01

Romanian version