C++ Flashcards

1
Q

What does C++ add to C?

A
  • Classes
  • Exception handling
  • Function & operator overloading
  • Default values for parameters
  • Pass by reference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is ::?

A

Scope resolution operator

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

What removes the need for the scope resolution operator?

A

“using namespace std”

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

What does cout and cerr do?

A

cout: goes to stdout
cerr: goes to stderr

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

Why must two functions that are identical in all but input types be created?

A

They may result in different return types

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

In a .hpp class, what are the two key elements that define a class?

A
  • Private class variables
  • Public member function prototypes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the three access specifiers in a class?

A
  • Private (default): can only be accessed within the class
  • Public: can be accessed by any part of the program
  • Protected: To do with inheritance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Where are functions for a class defined?

A

In a .cpp file with the same name as the .hpp file

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

How is a class used in a C++ file?

A

include <headerfilename.hpp></headerfilename.hpp>

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

What can be done if you have a class that can take a varying number of parameters?

A

Use overloading:
NewClass(int, int)
NewClass(int)
This is valid as there are a differing number of arguments

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

What values can be defaulted in a class function?

A

Only the trailing parameters
void NewClass::someFunction(int i, int j=0) (VALID)
void NewClass::someFunction(int i=1, int j=0) (VALID)
void NewClass::someFunction(int i=1, int j) (INVALID)

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

How has malloc been replaced in C++?

A

Instead of:
“int *p = (int )malloc(sizeof(int)25);
free(p);
It is now:
int *p = new int; // a single integer, uninitialised
int *p = new int(43); // a single integer, initialised to value 43
int *p = new int[25]; // an array of 25 integers
delete p; // free the single value p
delete[] p; // free the array p

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

What are constructors and destructors of a class?

A

They can be implicitly called when a object of the class type has been created or destroyed (i.e. when return 0 is stated). The constructor malloc memory to pointers and the destructor frees memory allocated to pointers.

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

What is a reference?

A

A reference is an alias for a variable
int num = 43;
int &ref = num;
ref = 43;

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

What is ‘this’?

A

‘this’ is a pointer to a current object and used to avoid ambiguities if you want to pass the object to a function or tor return a reference to the calling object

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

What is a copy constructor?

A

A member function that initialises an object using another object of the same class
MyClass (const MyClass &)
MyClass c, d;
MyClass e = c; // calls the copy constructor
d = e; // calls an assignment operator not the copy constructor

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

What is a static member variable?

A
  • They are shared by all objects of the same class
  • Can be accessed even if no objects exist
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is inheritance?

A

When a class uses another class’s protected and public functions/attributes as well as its own

19
Q

What effect do access specifiers have on inherited classes?

A

If either the base class or inheritance member access specifier is protected then the derivied class effect is protected. The same for private.

20
Q

What are the rules for constructors and destructors?

A
  • Constructors are executed in order of derivation
  • Destructors are executed in reverse order of derivation
21
Q

What is the diamond problem?

A

It is do with multiple inheritance. If there are two derived classes from the same class, and a class that derives from both of them, what happens if this class tries to access a variable from the original class.

22
Q

What are the two solutions to the diamond problem?

A
  • Apply the scope resolution operator
  • Prevent two copies of the base class being included in the bottom class (this can be done using a virtual class)
23
Q

What is a friend function in relation to a class?

A
  • A non-member function (not a member of the class)
  • Has access to the private and protected elements of the class
  • It is preceded by ‘friend’ in the public modifier
24
Q

What is friendship NOT?

A
  • Inherited
  • Transitive
25
Q

What can friend functions be used for?

A

Operator overloading

26
Q

What is function overloading?

A

More than one function with the same name

27
Q

What is operator overloading?

A

More than one ‘meaning’ of an operator

28
Q

What is the aim of operator overloading and how does it achieve this?

A

Make like easier for the users of a class, not the developer of the class. Easier means:
- Easier to understand the code
- Fewer code defects

29
Q

Which operators cannot be overloaded?

A

.
sizeof
?:
::
.*

30
Q

How is overloading + achieved?

A

class MyInteger
{
int i;
public:
MyInteger (int i) { this->i = i; }

  int   operator+(MyInteger); };

// This function represents <MyInteger> + <MyInteger>
int MyInteger::operator+ (MyInteger m)
{
return this->i + m.i;
}
int main ()
{
MyInteger a(4);
MyInteger b(3);
MyInteger c;</MyInteger></MyInteger>

c = a + b;

return 0;
}

31
Q

What are some considerations to have in mind when overloading operators?

A
  • At least one of the operands of an overloaded operator must be of a user-defined type
  • Overloaded operators are just function calls, so cannot preserve associativity or precedence rules of the operator, according to its built-in semantics
  • Just because you can does not mean you should
32
Q

What are the uses of overloaded functions?

A

They are so generic that they can be applied to almost any datatype, otherwise separate functions would have to be created for each datatype

33
Q

What is a function template?

A

Instructions for how to build a family of similar looking functions. Begins with:
template<dataname></dataname>

34
Q

What is a class template?

A

Helps to build a family of similar looking classes. Begins with:
template<typename></typename>

35
Q

What are the four main components of the standard template library?

A
  • Containers (vector, queue, stack)
  • Iterators (objects that enable a program to traverse a container)
  • Algorithms (binary_search)
  • Functions
36
Q

What is an exception?

A

An anomalous or exceptional condition

37
Q

What do exceptions do C++?

A
  • Enable error trapping
  • Allow the transfer of program execution to another part of the code
  • During execution of the program, i.e. at runtime
38
Q

How can exceptions be thrown?

A
  • Automatically
  • Programmatically
39
Q

What happens if an uncaught exception is thrown?

A

terminate called after throwing an instance of […]
Abort(coredump)

40
Q

How do you find type identification information at run time?

A

include <typeinfo></typeinfo>

typeid(class).name()

41
Q

It terms of streams, what does &laquo_space;and&raquo_space; do?

A

«: the insertion operator because it inserts characters into a stream (output)
»: the extraction operator because it extracts characters from a stream

42
Q

What does ifstream, ofstream and fstream do in terms of files?

A

ifstream: read access
ofstream: write access
fstream: read/write access

43
Q

How do you open a file?

A

f.open(<filename>, <mode>)
#include <fstream>
ifstream inFile;
ofstream outFile;
inFile.open("myFile.txt"); // ifstream: default mode is ios::in
outFile.open("myFile2.txt"); // ofstream: default mode is ios::out
if ( !inFile.is_open() || !outFile.is_open() )
// do some error processing
inFile.close();
outFile.close();</fstream></mode></filename>

44
Q

What are some stream state flags?

A

bad(): returns true if a reading or writing operation fails
fail(): returns true in the same cases as bad(), but also when a format error happens
eof(): returns true if a file open for reading has reached the end
good(): returns where any of the above return true
clear(): resets the state flags