C++ Flashcards
What are the 5 properties of OOP languages such as C++? Define each…
Abstraction -> Group associated features into on place for use.
Polymorphism -> Objects of the same type can take different forms.
Encapsulation -> All implementations are encapsulated for security, accessibility,
Inheritance -> Allows for diverse implementations of a single interface and documentation.
Extensibility -> Enables classes to extend functionality of other classes.
What header is needed for basic IO commands? What are the commands used for character input and output?
include <iostream></iostream>
std::cout -> Writes character stream to console.
std::cin -> Reads character stream from keyboard.
What do constructors and destructors return?
Nothing.
Explain when and how to use the ‘this’ keyword…
this is a pointer to the immediate scope of the class. For example, if used in a constructor, it will point to the variable outside of the constructor scope.
How do we differentiate the public, protected and private members of a class?
Public:
Protected:
Private:
In regards to dynamic memory allocation to pointers in the class, what happens upon instantiation of an object?
The required memory is automatically allocated to the pointers.
In regards to memory allocation, what happens when the destructor is called?
Allocated memory is freed.
How do we declare the destructor?
~MyClass();
When is the destructor called?
When delete(Object) is called on the object.
What is the purpose of a Copy Constructor?
It’s a way to assign members of an object instances based on another instance of the same object.
How do we declare a Copy Constructor?
MyClass ( const MyClass & )
How can we access functions outside the scope of their declaration? Use a class name Person and function named getAge() as an example…
Person::getAge();
Is function overloading possible?
How do we set a default argument?
Yes.
setAge( int x = 28 ) { }
Which keyword dynamically assigns memory to a pointer type? Which keyword frees this memory?
new
delete
What’s the difference between new and malloc?
New allocates memory and calls the constructor, whereas malloc only allocates memory.
New also calculates the memory for you, rather than having to explicitly declare it.