lec 13: c++ programming - a few other things

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/39

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:55 PM on 4/11/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

40 Terms

1
New cards

What is the this pointer in C++?

A pointer that refers to the current/invoking object inside a member function.

2
New cards

Why is the this pointer useful in constructors like Rectangle(int width, int height)?

It distinguishes object data members from parameters with the same names.

3
New cards

What does this->width = width; mean?

Assign the parameter width to the current object's data member width.

4
New cards

Why is width = width; a problem in that constructor example?

It assigns the parameter to itself instead of updating the object's member.

5
New cards
6
New cards

What is a static member variable?

A class member that belongs to the class itself rather than to each individual object.

7
New cards

Are static member variables shared across objects of the class?

Yes.

8
New cards

Can a static member be accessed without creating an object first?

Yes.

9
New cards

Why must a static member variable be defined separately outside the class?

Because declaring it in the class only tells the class it exists; storage must be created separately.

10
New cards

What is the example definition of a static member variable?

int Person::counter = 0;

11
New cards

What does the Person counter example track?

How many Person objects have been created.

12
New cards
13
New cards

What is a static member function?

A class function that belongs to the class rather than any particular object.

14
New cards

Do static member functions have a this pointer?

No.

15
New cards

How do you call a static member function from outside the class?

Using the class name and scope operator, like Person::getPersonCount().

16
New cards

Why can static members be risky?

They behave a lot like shared global state and can cause thread-safety issues.

17
New cards
18
New cards

What are default parameters?

Parameters with default values that can be omitted in a function call.

19
New cards

Which parameters can be omitted when using default parameters?

Trailing parameters.

20
New cards

Where is the default value specified?

In the function declaration.

21
New cards

Can member functions use default parameters?

Yes.

22
New cards

What happens if you call printNumber() when the function is void printNumber(int i = 0)?

The argument 0 is used.

23
New cards

What rule applies after a parameter has a default value?

All following parameters must also have default values.

24
New cards

Why can default parameters cause overloading problems?

Because they can make calls ambiguous.

25
New cards
26
New cards

What is an exception in C++?

A standardized way to represent and handle exceptional situations.

27
New cards

What does a try block contain?

Risky code that may throw an exception.

28
New cards

What does throw do?

Generates an exception.

29
New cards

What does a catch block do?

Handles an exception.

30
New cards

How does C++ choose which catch block to use?

By matching the thrown type.

31
New cards

What does catch(…) do?

Catches any exception not caught by earlier handlers.

32
New cards

After an exception is handled, where does execution continue?

After the try-catch block.

33
New cards

What usually happens to uncaught exceptions?

The program terminates.

34
New cards
35
New cards

What is std::exception?

The standard base exception class in C++.

36
New cards

What header provides std::exception according to the slides?

37
New cards

Name some standard exception subclasses from the slides.

std::bad_alloc, std::runtime_error, std::invalid_argument

38
New cards

Why is catch(const exception& e) good practice?

It avoids modifying the exception and avoids unnecessary copying.

39
New cards

What does e.what() provide?

A message describing the exception.

40
New cards

What is a custom exception?

An exception class you define yourself, usually by inheriting from std::exception and overriding what().