Introduction to Classes Flashcards

1
Q

What is the focus in procedural programming?

A

To create functions that implement our program logic. We pass data objects to these functions, those functions perform operations on the data and potentially return a result

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

What is the focus in object-oriented programming?

A

To create program defined data types that contain properties and well defined behaviours

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

class invariant

A

a condition that must be true for the lifetime of an object for the object to remain in a valid state

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

invalid state

A

an object that has a violated class invariant. Unexpected or undefined behaviour may result from further use of that object

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

class

A

program defined compound type with data, and functions which work on that data

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

member functions

A

functions that belong to a class type

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

implicit object

A

the object that a member function is called on

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

non member functions

A

name for functions that are not member functions

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

what should you do if your class has no data members

A

use a namespace instead

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

const member functions

A

member function that guarantees it will not modify the object or call any non-const member functions (as they may modify the object)

a member function that does not (and will not ever) modify the state of the object should be made const, so that it can be called on both non-const and const objects

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

access level

A

each member of a class has an access level that determines who can access that member.

access levels are defined on a per class basis not a per object basis

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

access controls

A

access level system informally called this

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

public members

A

members of a class type that do not have any restrictions on how they can be accessed. Public members can be accessed by anyone (as long as they are in scope). This includes other members of the same class or code outside of the class

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

the public

A

code that exists outside the members of a given class type

includes:
non-member functions
members of other class types

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

What is the default access level for all members of a struct?

A

public

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

private members

A

members of a class type that can only be accessed by other members of the same class

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

What is the default access level for all members of a class?

A

private

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

is a class with private members an aggregate?

A

no and therefore it cannot use aggregate initialisation

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

how should you name private members?

A

starting with a m_ prefix. his helps distinguih them from the names of local variables, function parameters and member functions

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

access specifier

A

allows you to explicitly set the access level of our members

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

should structs use access specifiers?

A

they should avoid them, therefore all their members default to public

22
Q

access function

A

a trivial public member function whose job is to retrieve or change the value of a private member variable. Access function come in two flavours: getters and setters

23
Q

Getters

A

sometimes called accessors, are public member functions that return the value of a private member variable

24
Q

setters

A

sometimes called mutators, are public member fucntions that set the value of a private member variable.

25
Q

interface

A

this determines how a user of the class type will interact with objects of the class type. Because only public members can be accessed from outside of the class type, the public members of a class type form its interface.

26
Q

public interface

A

an interface composed of only public members

27
Q

implementation of a class type

A

the code that actually makes the class behave as intended. This includes both the member variables that store data, and the bodies of the member functions that contain the program logic and manipulate the member variables

28
Q

data hiding / information hiding / data abstraction

A

technique used to enforcethe separation of the interface and implementation by hiding the implementation of a program defined data type from users

29
Q

encapsulation

A

sometimes used to refer to data hiding or to refer to the bundling of data and functions together (without regard for access controls) so its use can be ambiguous

30
Q

What order should you define the members in your class and why?

A

Prefer to declare your public members first and your private members last. This spotlights the public interface and de-emphasises implementation details

31
Q

constructor

A

a special member function that is used to initialise class type objects. A matching constructor must be found in order to create a non-aggregate class type object

32
Q

member initialiser list

A

allows you to initialise your member variables from within a constructor. Member variables in a member initialiser should be listed in the order that they are defined in the class. Prefer using the member initialiser list to initialise your members over assigning values in the body of the constructor

33
Q

default constructor

A

a constructor that takes no parameters (or has all default parameters). The default constructor is used if no initialisation values are provided.

34
Q

implicit default constructor

A

If a non-aggregate class type object has no user-declared constructors, the compiler will generate a default constructor (so that the class can be value or default initialised)

35
Q

constructor chaining

A

when a constructor delegates construction to another constructor

36
Q

delegating constructors

A

a constructor that delegates using constructor chaining

37
Q

can constructors delegate and initialise

A

no only one or the other

38
Q

temporary / unamed / anonymous object

A

an object that has no name and exists only for the duration of a single expression

39
Q

copy constructor

A

a constructor you use to initialise an object with an existing object of the same type

40
Q

implicit copy constructor

A

if you do not provide a copy constructor for your class c++ will create a public one for you that does memberwise initialisation

41
Q

as-if rule

A

the compiler can modify a program however it likes in order to produce more optimised code, so long as those modifications do not affect a program’s observable behaviour

One exception to the rule is copy elision

42
Q

copy elision

A

a compiler optimization technique that allows the compiler to remove unnecessary copying of objects

43
Q

elided

A

we say the constructor has been elided when the compiler optimised away a call to the copy constructor

44
Q

user defined conversion

A

a function written to convert a value to or from a program defined type

45
Q

converting constructor

A

constructor that can be used to perform an implicit conversion

46
Q

are all constructors converting constructors?

A

they are in their default state

47
Q

explicit

A

we use this keyword to tell the compiler a constructor should not be used as a converting constructor

48
Q

what can explicit constructors not be used for?

A

copy initialisation
copy list initialisation
implcit conversions

49
Q

what constructors should you mark as explicit

A

any constructor that takes a single argument

50
Q

what constructors should you leave as implicit?

A

copy or move constructors (as these do not perform conversions)

cases where the conversion between types is both semantically equivalent and performant

51
Q

are constexpr member functions const?

A

not implicitly