Introduction to C++ I Flashcards

1
Q

What is C++?

A

Object-oriented language built on top of C.

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

What are some functionalities of C++?

A
  • Operator overloading
  • We have classes and objects
  • Namespaces for organisation
  • Pass-by-reference
  • In C++ usage of objects implicitly triggers default or programmer defined functions for managing lifecycle.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the types/visibility of member variables we can have in a C++ class?

A
  • private
  • public
  • protected
  • friend
  • static
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where are constructors of classes typically declared?

A

In the public scope (can be protected or private).

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

What does public declaration of a class allow?

A
  • Initialisation
  • Copying (duplication of resource)
  • Moving (transferring ownership of resource)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does a destructor deceleration specify?

A

Destructor declaration specifies how to clean up a class type.

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

When are destructors callled?

A

Destructors are automatically called when a class is deleted (e.g. out of scope or explicitly free the memory allocated for the class).

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

What are member functions?

A

Functions of the class that do stuff. Specify name, parameters, return type.

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

What are const functions?

A

Functions that do not change the state of the class.

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

Can member function be declared and defined in the header class?

A

Yes, we can provide implementation as well in the header file.

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

What is the default visibility for a class member?

A

Private

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

What visibility can C++ class members be?

A
  • private
  • public
  • protected
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where are private members accessible from?

A

Inside the class or through member functions.

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

Where are public members accessible?

A

Outside the class (they form the class interface) and should either be accessors or mutators.

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

Do we use header guards in C++?

A

Yes, they are needed in the header files.

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

What is the encourage method of storing declaration and implementation?

A

Store the declaration in the header and the definition/implementation in an implementation file (.cpp).

15
Q

What are initialisation lists?

A

Used to set a member variable to function parameter value.

16
Q

What is the this pointer?

A

A pointer to the current instance of the class object, only visible withing a member function of a class instance. Only needed if you have to refer to class member.

17
Q

What is stored in the dereferencing of *this?

A

*this is the value at the address of the class instance.

18
Q

What is overloading?

A

Multiple functions with the same name, but different parameters and qualifiers (e.g. const)

19
Q

What are structs in C++?

A

They are classes where visibility is default public.

20
Q

How are structs in C++ different to C?

A

Structs in C++ have the option to add functions to them but you don’t have to.

21
Q

What happens when a class instance is out of scope?

A

The destructor is automatically called.

22
Q

If we use the new keyword what must we use later in the program?

A

delete, when we have finished using a variable.

23
If we use the new[] keyword what must we use later in the program?
delete[]
24
What is are the comparison of C and C++ dynamic allocation?
- C allocates raw bytes of memory with malloc. - C++ allocates memory for a number of elements - C scaling is easy to forget (sizeof(int)!) - In C++ byte scaling is implicit
25
Why do we need namespaces?
- Large codebases can have many hundreds of types and functions. - Everyone wants to name their types and functions concisely and succinctly (in a brief manner) - Allows us to deal with many similar but unrelated things sharing a common namespace.
26
How do we avoid using a namespace repeatedly in a file?
'using' keyword
27
How do we represent true or false in C++?
Using bool (which is not available in C).
28
What can we do with bool and int in C++?
Convert implicitly to each other.
29
What is wchar_t?
Wide character. ASCII is limited to the values 0-127 (7 bits). Not enough for some languages.
30
What is nullptr in C++?
Keyword used instead of NULL in C++.
31
What are namespaces?
Namespaces are a way to logically group and organize code elements such as classes, functions, and variables to prevent naming conflicts and improve code readability and maintainability.
32
What is encapsulation?
The process of bundling information into objects such as classes. Helps hide the internal state of an object and prevents unintended modifications.
33
What is a const variable?
A variable that cannot be changed once it has been initialized.
34
What is undefined behaviour?
Situations where the behavior of a program is not defined by the language specification and instead relies on the compiler, leading to unpredictable outcomes that can include crashes, unexpected results, or seemingly correct behavior.