Introduction to C++ I Flashcards
What is C++?
Object-oriented language built on top of C.
What are some functionalities of C++?
- Operator overloading
- We have classes and objects
- Namespaces for organisation
- Pass-by-reference
- In C++ usage of objects implicitly triggers default or programmer defined functions for managing lifecycle.
What are the types/visibility of member variables we can have in a C++ class?
- private
- public
- protected
- friend
- static
Where are constructors of classes typically declared?
In the public scope (can be protected or private).
What does public declaration of a class allow?
- Initialisation
- Copying (duplication of resource)
- Moving (transferring ownership of resource)
What does a destructor deceleration specify?
Destructor declaration specifies how to clean up a class type.
When are destructors callled?
Destructors are automatically called when a class is deleted (e.g. out of scope or explicitly free the memory allocated for the class).
What are member functions?
Functions of the class that do stuff. Specify name, parameters, return type.
What are const functions?
Functions that do not change the state of the class.
Can member function be declared and defined in the header class?
Yes, we can provide implementation as well in the header file.
What is the default visibility for a class member?
Private
What visibility can C++ class members be?
- private
- public
- protected
Where are private members accessible from?
Inside the class or through member functions.
Where are public members accessible?
Outside the class (they form the class interface) and should either be accessors or mutators.
Do we use header guards in C++?
Yes, they are needed in the header files.
What is the encourage method of storing declaration and implementation?
Store the declaration in the header and the definition/implementation in an implementation file (.cpp).
What are initialisation lists?
Used to set a member variable to function parameter value.
What is the this pointer?
A pointer to the current instance of the class object, only visible withing a member function of a class instance. Only needed if you have to refer to class member.
What is stored in the dereferencing of *this?
*this is the value at the address of the class instance.
What is overloading?
Multiple functions with the same name, but different parameters and qualifiers (e.g. const)
What are structs in C++?
They are classes where visibility is default public.
How are structs in C++ different to C?
Structs in C++ have the option to add functions to them but you don’t have to.
What happens when a class instance is out of scope?
The destructor is automatically called.
If we use the new keyword what must we use later in the program?
delete, when we have finished using a variable.
If we use the new[] keyword what must we use later in the program?
delete[]
What is are the comparison of C and C++ dynamic allocation?
- C allocates raw bytes of memory with malloc.
- C++ allocates memory for a number of elements
- C scaling is easy to forget (sizeof(int)!)
- In C++ byte scaling is implicit
Why do we need namespaces?
- Large codebases can have many hundreds of types and functions.
- Everyone wants to name their types and functions concisely and succinctly (in a brief manner)
- Allows us to deal with many similar but unrelated things sharing a common namespace.
How do we avoid using a namespace repeatedly in a file?
‘using’ keyword
How do we represent true or false in C++?
Using bool (which is not available in C).
What can we do with bool and int in C++?
Convert implicitly to each other.
What is wchar_t?
Wide character. ASCII is limited to the values 0-127 (7 bits). Not enough for some languages.
What is nullptr in C++?
Keyword used instead of NULL in C++.
What are namespaces?
Namespaces are a way to logically group and organize code elements such as classes, functions, and variables to prevent naming conflicts and improve code readability and maintainability.
What is encapsulation?
The process of bundling information into objects such as classes. Helps hide the internal state of an object and prevents unintended modifications.
What is a const variable?
A variable that cannot be changed once it has been initialized.
What is undefined behaviour?
Situations where the behavior of a program is not defined by the language specification and instead relies on the compiler, leading to unpredictable outcomes that can include crashes, unexpected results, or seemingly correct behavior.