Part 3 - Control Structures, Operators, Classes and Objects Flashcards

1
Q

What 3 types of control structures exist?

A

Selection: if, else, switch

Repetition: while, do, for

Exception: try/catch

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

What is the difference between break and continue?

A

break will take you completely out of the loop

continue will start the next iteration of the loop

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

What does declaring a function inline do?

A

Causes a copy of the code to be inserted into the code generated by the compiler instead of a function call

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

If a function is a member of a class it must be applied to _________________ unless it is defined as static

A

An object of that class

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

How can one give the value 5 a leading 0 when outputting?

A

cout << setfill(‘0’) << setw(2) << val << endl;

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

What does setw do?

A

Sets the field width of the next item to be output

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

What does setfill do?

A

Sets the fill character to be used for all subsequent outputs

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

What 3 visibility specifiers exist in C++?

A

public

private

protected

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

What is private visibility?

A

Private members can only be accesses in functions of the class and its friend functions

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

What is protected visibility?

A

Protected members can only be accessed in functions and friend functions of the class or subclasses of the class

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

What is public visibility?

A

Public members can be accessed anywhere in the program as long as the class is in scope

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

What is the default visibility level?

A

Private

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

If no constructor is provided for a class the compiler will generate a __________

A

No argument constructor

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

What does a default no argument constructor do?

A

Calls the no argument constructors of any members of the class that are class objects

Will NOT initialise any primitive objects

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

Where should default parameters for constructors be specified?

A

In the constructor definition in the header file

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

How would you create a new object of type Time on the heap?

A

Time *tPtr1 = new Time;

17
Q

If a class object has been declared constant, e.g.

const Time noon(12);

The only functions that can be applied to the object are _________________

A

Constant member functions

18
Q

Where must initialisation of non static class members be performed?

A

In the constructor(s) of the class

19
Q

What is an initialiser list?

Give an exame using the Time class

A

A way to initialise members outside the constructor

Time::Time() : hour(0), sec(0), min(0) {

//…

}

20
Q

How should members of a class, that are themselves objects of a type of class, be initialised?

Demonstrate with this class:

class Lecture {

private: Time startTime;
public: Lecture(….., int startHour);

};

A

Initialiser lists

Lecture::Lecture(….., startHour) : startTime(startHour) { … }

21
Q

If a class has as a member an object of another class and has a constructor in which that member is not initialised, what happens?

A

The no argument constructor for the class is invoked.

22
Q

If any object member belongs to a class that has no no-argument constructor, what cannot be used? Hence, what must we provide?

A

Default no-arg constructor

A constructor that initialises the object in its initialiser list

23
Q

When must a non constant static class member be initialised?

A

Outside of any declaration, i.e. perhaps at the top of the cpp file

24
Q

For the class:

class Employee {

private:

int payRollNum;

static int count;

public:

Employee();

};

Demonstrate how count would be initialised

A

// Employee.cpp

Employee::count = 0;

Employee::Employee() {…}

25
Q

What does this refer to in a function?

A

A pointer to the instance of the object

26
Q

How can a reference to the current object be retrieved?

A

*this