Unit_3: C++ Basics Flashcards

1
Q

What is forward declaration? Why need it? How to use it?

A
  1. a way to tell the compiler that a type exists without providing its full definition. So use when just need pointer/reference, don’t need to know class size or access class members
  2. => reduce compilation time (fewer include), allow pointer/reference declare w/o full class definition
  3. eg: class B; =>just tell compiler B exists, so we can use pointer/reference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When to #include?

A
  1. extending that class (.h) -> know members of superclass
  2. implementing that class (.cpp): know about its own members
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s memory leak? Types?

A

Memory leak happen when we don’t probably reclaim memory -> unusable for the rest of program
1. declared variables have scope
=> must free pointer’s memory before it goes out of scope.
So in linkedlist, delete only free the immediate node (newNode) but nodes linked to it are not. => need destructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is #pragma once

A
  • used at top of header files only -> this file should be include only once
  • only stop you from including something more than once in ONE file compilation -> can’t protect you from circular include (eg include ListItem and Node, and Node was including ListItem too)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happends if you put executable code in header file?

A

get 2 copies of executable code in compilation -> link editor errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s self reference?

A

When a class needs to refer to itself (eg access its own methods/fields; pass a reference to itself) => this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly