C++ Flashcards

1
Q

What are the access modifiers in C++ ? Define each one of them

A

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)

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

define encapsulation

A

bundling data and methods that work on that data into one unit (the class)

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

what are the two types of polymorphism occuring under C++ ?

A
  • compile-time polymorphism
  • runtime polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

explain compile-time and runtime polymorphism

A

compile-time polymorphism is achieved by function or operator overloading

runtime polymorphism is achieved at runtime by overriding functions (virtual)

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

define early binding and give a few other names for it
contrast it with late binding and explain the difference

A

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.

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

difference between class and struct ?

A

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

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

what is a singleton ?

A

a class we plan to have only one instance of

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

what is an interface ?

A

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;)

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

what is the purpose of an abstract class ?

A

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.

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

what is a simple difference between compile-time and runtime

A

errors can be detected without running the code at compile-time, and only through running at runtime

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

How to cast away the constness of an int

A

const_cast(i) = 6;

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

How to cast into constness

A

static_cast(j) = 7;

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

what is #include called ?

A

a preprocessor directive == copy paste the iostream file at the beginning of our file

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

what’s the purpose of the main function ?

A

tells the computer where to start

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

output “hello world” to the console

A

cout &laquo_space;“hello world” &laquo_space;endl;

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

what’s the purpose of the main’s return 0; ?

A

Says everything before it ran well

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

how to print a new line insi;e the quotation marks ?

A

\n

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

take in user input into variable a

A

cin&raquo_space; a;

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

how do you define a function’s prototype and why

A

a function’s prototype lets the compiler know the function exists before it reads its definition.

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

how can c++ tell a function is a constructor?

A

it has the same name as the class name (and no return type), gets called automatically when an instance is created

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

why and how can we split our classes into separate files ?

A

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

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

what is :: ?

A

binary scope resolution operator&raquo_space; determines the namespace currently in use

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

difference between ++x and x++ ?

A

if ++ before variable, runs the line of code then adds one to x, else adds then runs.

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

for loop syntax

A

for(int x=1; condition; increment) { DO }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
calculate 5^2
#include cout << pow(5,2);
26
difference between while loop and do while ?
do{THIS}while(CONDITION); the semi-colon and running at least one time
27
define a switch statement on the variable age
int age = 20; switch(age){ case i: do; break; default: do; }
28
logical and, or, not ?
&& || !
29
what library contains rand() ?
#include
30
how to get [0,6] using rand()
rand%6+1
31
How do we seed rand() ?
srand(seed); better: #include srand(time(0));
32
global variable v. local variable
one defined outside functions, one defined inside a function. NB : functions prefer the nearest variable
33
how do i use the global variable of same name from inside a function
using unary resolution operator ::tuna
34
define function overloading
same name, different signature
35
make a 5 int array using an initializer list
int bucky[5] = {2,3,4,5,6}
36
how to define array as an argument to f
void f(int array[])
37
how to define a multidimensional array
int x[2][3]= {{2,3,4},{3,5,6}}
38
get the memory address of i
&i
39
define pointer
a variable containing a memory address as its value
40
point to i
int* ptr = &i;
41
difference between passing by value, by reference and by pointer
by value, you pass a copy of the object by reference, you pass the actual (constant, non null) memory address of the object by pointer, you pass a pointer to the object you want (which can point elsewhere later, and be a nullptr)
42
sizeof(x) returns ..
the size of x in bytes
43
return len of array x
sizeof(x)/sizeof(x[0])
44
what happens when a ptr is incremented
it points to the next element in memory (not to the next byte)
45
what's the purpose of : #ifndef CLASS_H #define CLASS_H
only include once
46
when do you have to write A::A {}, A::somefunction{}, basically defining the scope you're working in at every function definition
in the .cpp file, but not in the .h file (scope already obvious)
47
call method() using ptr, a pointer to Class
ptr->method();
48
implement a deconstructor
~Class(); no parameters, no return type, and no overloading
49
CONST class instance requires ..
CONST methods
50
How do we need to initialize constant attributes in a class constructor
member init syntax : Class(int a, int b) : x(a), y(b) {}
51
How to make a function your class's friend
in class, class lets you sign as a friend friend void f();
52
what's this ?
this is a pointer to the current class instance
53
overload + to use it on myClass objects
MyClass operator+(MyClass c) : MyClass a; a.num=this->num+c.num return(a);
54
define accessibility in public, protected, and private inheritance :
public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class. protected inheritance makes the public and protected members of the base class protected in the derived class. private inheritance makes the public and protected members of the base class private in the derived class. NOTABENE: private members of the base class are inaccessible to the derived class.
55
sizeof(char)
1 byte = 8 bits
56
define a string s in C style
const char * s = "Hello";
57
a string in c++ is ...
an array of chars
58
difference between single and double quote strings
single: char, double: char ptr
59
include string library
#include
60
how does the compiler know const char * c has ended in : cout << c;
the nulle termination character \0
61
declare a c++ string and find its size
std::string name = "Cherno"; name.size();
62
What does static mean outside a class ? static int s_ = 5;
This variable is only going to be linked internally inside this translation unit
63
How do you access a variable in another .cpp from main.cpp ? what if we mark it as static now ?
extern int s_; If s_ is marked as static then the linker will not be able to resolve it. It is like it's private to the translation unit it's declared in.
64
What does static mean inside a class ?
static var means there will only be one instance of the variable across all the instances of the class. Have to be defined outside the class. static method can be called without a class instance, and it can't use any non-static class members since if no instance, that would make no sense
65
What are enums and how are they used ?
They are user-defined types that can take only one of few values (integer-like), increment from 0 by default unless defaulted enum Example : unsigned char {A=0,B,C}; Example value = B; if (value==1) { // DO }
66
A const variable must be initialized ...
At definition
67
State two possible uses for the keyword "mutable"
1. declaring a class variable as mutable allows const functions to modify it 2. auto f = [=]() mutable {x++;}
68
A string's end is marked using ... ?
a null termination character '\0'
69
What's the use of member initialization list ?
Resource acquisition is initialization We construct and give value at same time instead of calling constructor twice
70
How do you instantiate an object of class Entity ? Give different possibilities
1. Entity entity = Entity("cherno"); // stack, scoped, small (1MB) 2. Entity* e = new Entity(); // heap, slower, must free later is my object big ? do i want to explicitly control the lifetime of my object ?
71
memcpy params ?
memcpy(dest, src, size)