C++ Classes Flashcards

1
Q

Though functionally identical, what is the biggest difference between structs and classes?

A

Class defaults to private, while struct defaults to public

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

What is the difference in use case between structs and classes

A

Classes are best for data abstraction and inheritance, while structs are best used for data grouping and access.

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

What are the two basic constructors?

A

Default (no args) and with input

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

What is the abbreviation of Constructor?

A

CTOR

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

What are the non-basic Constructors?

A
  • Copy
  • Assignment
  • Move
  • Move Assignment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Uniform Initialization Syntax

A

The standard ways of constructing objects and variables in c++

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

What are the Uniform initialization Syntax ctors?

A

Default, Empty, Initializer List, Assignment

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

What is the following Ctor? string x;

A

default

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

What is the following Ctor? string x{};

A

Empty

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

What is the following Ctor? string x{“string”};

A

Initializer List

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

What is the following Ctor? string x = “string”;

A

Assignment

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

How do you define a Class?

A

class MyClass{
//private fields
int num1;

public:
MyClass() {num1 = 1;}
int get_num1() { return this->num1;}
};

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

How do you define a Struct?

A

struct MyStruct{
//public fields
MyStruct() {num1 = 1;}
int get_num1() { return this->num1;}

private:
int num1;
};

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

What is the “this” keyword?

A

It is a pointer to the current object

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

What is the syntax for using the “this” keyword?

A

this->

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

Given class MyClass, how do you set the output of the class (like toString in Java)

A

ostream& operator«(ostream &out, const MyClass &m) {
//return desired output. Ex:
return out &laquo_space;m.get_x() &laquo_space;“,” &laquo_space;m.get_y();
}

17
Q

What is the constructor keyword “=default”?

A

Keeps the default version of a ctor.

18
Q

What is the ctor keyword “=delete”?

A

Deletes ctor and prevents it from ever being defined. (also provideds a ‘deleted’ message)

19
Q

What is Member Initialization?

A

Pre-allocating values in the contructor.

20
Q

What is the order of member initialization?

A

In the order that the variables were declared in the class.

21
Q

When does Member pre Initialization occur?

A

Before the curly braces in a constructor statement.
Ex: Circle() : /*member initialization*/ {}

22
Q

What MUST be member initialized?

A
  • Const values
  • objects with no default ctor
  • Reference Variables
  • Any call to parent / base class
23
Q

Why must const values be member initialized?

A

The compiler will set them to a default if not, and then they cannot be changed.

24
Q

Why must objects without a default ctor be member initialized?

A

Because otherwise there is no default value they can be assigned.

25
Why must reference variables be member initialized?
Because there is no logical default for a memory reference.
26
What is Constructor Delegation?
It is essentially constructor chaining. Ex: Circle::Circle() : Circle(1) { } Circle(int radius) : Circle (1, 1, radius) { } Circle(int x, int y) : Circle (x, y, 1) { } Circle(int x, int y, int radius) : Circle (x, y, radius) { }
27
What is a Default Value in classes / structs?
When the variables are assigned values as they are declared outside of ctor.
28
What are Default Constructor Arguments?
When variables are directly assigned in the ctor. Ex: Circle(int x=1, int y=1, int radius=1) : x(x), y(y), radius(radius) { }
29
What is the limiting factor of Default Constructor Arguments?
If variable values are provided in the ctor call, it must be in order. Removes some possible call options.
30
What is a const Variable?
A variable that, once allocated, can never be changed.
31
What is a const Method?
Methods that cannot change the data members of the class.
32
What is the most common example of const methods?
Getters.
33
How do you declare given method 'MyMethod' to be const?
int MyMethod() const {...}
34
How do const arguments work?
Create an agreement that data stored in the const variable will not be changed. Important when using 'pass by reference'.