C++ Flashcards
What are the access modifiers in C++ ? Define each one of them
public : accessible from any part of the program (open bedroom)
private : can only be accessed from within the class or friend classes and functions (bedroom only for parents and their friends)
protected : just like private but can also be accessed by child classes (bedroom is open to parents, friends, and children)
define encapsulation
bundling data and methods that work on that data into one unit (the class)
what are the two types of polymorphism occuring under C++ ?
- compile-time polymorphism
- runtime polymorphism
explain compile-time and runtime polymorphism
compile-time polymorphism is achieved by function or operator overloading
runtime polymorphism is achieved at runtime by overriding functions (virtual)
define early binding and give a few other names for it
contrast it with late binding and explain the difference
In early binding, the compiler matches the function call with the correct function definition at compile time. It is also known as Static Binding or Compile-time Binding. By default, the compiler goes to the function definition which has been called during compile time.
In the case of late binding, the compiler matches the function call with the correct function definition at runtime. It is also known as Dynamic Binding or Runtime Binding. This can be achieved by declaring a virtual function.
difference between class and struct ?
the only difference between a structure and a class is that structure members have public access by default and class members have private access by default
what is a singleton ?
a class we plan to have only one instance of
what is an interface ?
an interface in c++ is an abstract class. A class is made abstract by declaring at least one of its functions as PURE VIRTUAL (i.e virtual void doube f() = 0;)
what is the purpose of an abstract class ?
The purpose of an abstract class (often referred to as an ABC) is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.
Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.
what is a simple difference between compile-time and runtime
errors can be detected without running the code at compile-time, and only through running at runtime
How to cast away the constness of an int
const_cast(i) = 6;
How to cast into constness
static_cast(j) = 7;
what is #include called ?
a preprocessor directive == copy paste the iostream file at the beginning of our file
what’s the purpose of the main function ?
tells the computer where to start
output “hello world” to the console
cout «_space;“hello world” «_space;endl;
what’s the purpose of the main’s return 0; ?
Says everything before it ran well
how to print a new line insi;e the quotation marks ?
\n
take in user input into variable a
cin»_space; a;
how do you define a function’s prototype and why
a function’s prototype lets the compiler know the function exists before it reads its definition.
how can c++ tell a function is a constructor?
it has the same name as the class name (and no return type), gets called automatically when an instance is created
why and how can we split our classes into separate files ?
easier to manage, potential for team work, modularity etc. We also compile the cpp into binary and only need to provide the prototype to users.
for each class create .h (prototypes) and .cpp (bodies), both files should have same #include
in main.cpp add #include
what is :: ?
binary scope resolution operator»_space; determines the namespace currently in use
difference between ++x and x++ ?
if ++ before variable, runs the line of code then adds one to x, else adds then runs.
for loop syntax
for(int x=1; condition; increment) { DO }
calculate 5^2
include
cout «_space;pow(5,2);
difference between while loop and do while ?
do{THIS}while(CONDITION);
the semi-colon and running at least one time
define a switch statement on the variable age
int age = 20;
switch(age){
case i:
do; break;
default:
do;
}
logical and, or, not ?
&&
||
!