C++ Flashcards
What does C++ add to C?
- Classes
- Exception handling
- Function & operator overloading
- Default values for parameters
- Pass by reference
What is ::?
Scope resolution operator
What removes the need for the scope resolution operator?
“using namespace std”
What does cout and cerr do?
cout: goes to stdout
cerr: goes to stderr
Why must two functions that are identical in all but input types be created?
They may result in different return types
In a .hpp class, what are the two key elements that define a class?
- Private class variables
- Public member function prototypes
What are the three access specifiers in a class?
- Private (default): can only be accessed within the class
- Public: can be accessed by any part of the program
- Protected: To do with inheritance
Where are functions for a class defined?
In a .cpp file with the same name as the .hpp file
How is a class used in a C++ file?
include <headerfilename.hpp></headerfilename.hpp>
What can be done if you have a class that can take a varying number of parameters?
Use overloading:
NewClass(int, int)
NewClass(int)
This is valid as there are a differing number of arguments
What values can be defaulted in a class function?
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 has malloc been replaced in C++?
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
What are constructors and destructors of a class?
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.
What is a reference?
A reference is an alias for a variable
int num = 43;
int &ref = num;
ref = 43;
What is ‘this’?
‘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
What is a copy constructor?
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
What is a static member variable?
- They are shared by all objects of the same class
- Can be accessed even if no objects exist
What is inheritance?
When a class uses another class’s protected and public functions/attributes as well as its own
What effect do access specifiers have on inherited classes?
If either the base class or inheritance member access specifier is protected then the derivied class effect is protected. The same for private.
What are the rules for constructors and destructors?
- Constructors are executed in order of derivation
- Destructors are executed in reverse order of derivation
What is the diamond problem?
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.
What are the two solutions to the diamond problem?
- 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)
What is a friend function in relation to a class?
- 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
What is friendship NOT?
- Inherited
- Transitive
What can friend functions be used for?
Operator overloading
What is function overloading?
More than one function with the same name
What is operator overloading?
More than one ‘meaning’ of an operator
What is the aim of operator overloading and how does it achieve this?
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
Which operators cannot be overloaded?
.
sizeof
?:
::
.*
How is overloading + achieved?
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;
}
What are some considerations to have in mind when overloading operators?
- 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
What are the uses of overloaded functions?
They are so generic that they can be applied to almost any datatype, otherwise separate functions would have to be created for each datatype
What is a function template?
Instructions for how to build a family of similar looking functions. Begins with:
template<dataname></dataname>
What is a class template?
Helps to build a family of similar looking classes. Begins with:
template<typename></typename>
What are the four main components of the standard template library?
- Containers (vector, queue, stack)
- Iterators (objects that enable a program to traverse a container)
- Algorithms (binary_search)
- Functions
What is an exception?
An anomalous or exceptional condition
What do exceptions do C++?
- Enable error trapping
- Allow the transfer of program execution to another part of the code
- During execution of the program, i.e. at runtime
How can exceptions be thrown?
- Automatically
- Programmatically
What happens if an uncaught exception is thrown?
terminate called after throwing an instance of […]
Abort(coredump)
How do you find type identification information at run time?
include <typeinfo></typeinfo>
typeid(class).name()
It terms of streams, what does «_space;and»_space; do?
«: the insertion operator because it inserts characters into a stream (output)
»: the extraction operator because it extracts characters from a stream
What does ifstream, ofstream and fstream do in terms of files?
ifstream: read access
ofstream: write access
fstream: read/write access
How do you open a file?
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>
What are some stream state flags?
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