C++ Flashcards

You may prefer our related Brainscape-certified 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
Q

calculate 5^2

A

include

cout &laquo_space;pow(5,2);

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

difference between while loop and do while ?

A

do{THIS}while(CONDITION);

the semi-colon and running at least one time

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

define a switch statement on the variable age

A

int age = 20;
switch(age){
case i:
do; break;
default:
do;
}

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

logical and, or, not ?

A

&&
||
!

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

what library contains rand() ?

A

include

30
Q

how to get [0,6] using rand()

A

rand%6+1

31
Q

How do we seed rand() ?

A

srand(seed);

better:
#include
srand(time(0));

32
Q

global variable v. local variable

A

one defined outside functions, one defined inside a function.

NB : functions prefer the nearest variable

33
Q

how do i use the global variable of same name from inside a function

A

using unary resolution operator ::tuna

34
Q

define function overloading

A

same name, different signature

35
Q

make a 5 int array using an initializer list

A

int bucky[5] = {2,3,4,5,6}

36
Q

how to define array as an argument to f

A

void f(int array[])

37
Q

how to define a multidimensional array

A

int x[2][3]= {{2,3,4},{3,5,6}}

38
Q

get the memory address of i

A

&i

39
Q

define pointer

A

a variable containing a memory address as its value

40
Q

point to i

A

int* ptr = &i;

41
Q

difference between passing by value, by reference and by pointer

A

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
Q

sizeof(x) returns ..

A

the size of x in bytes

43
Q

return len of array x

A

sizeof(x)/sizeof(x[0])

44
Q

what happens when a ptr is incremented

A

it points to the next element in memory (not to the next byte)

45
Q

what’s the purpose of :
#ifndef CLASS_H
#define CLASS_H

A

only include once

46
Q

when do you have to write A::A {}, A::somefunction{}, basically defining the scope you’re working in at every function definition

A

in the .cpp file, but not in the .h file (scope already obvious)

47
Q

call method() using ptr, a pointer to Class

A

ptr->method();

48
Q

implement a deconstructor

A

~Class();

no parameters, no return type, and no overloading

49
Q

CONST class instance requires ..

A

CONST methods

50
Q

How do we need to initialize constant attributes in a class constructor

A

member init syntax :

Class(int a, int b) : x(a), y(b) {}

51
Q

How to make a function your class’s friend

A

in class, class lets you sign as a friend

friend void f();

52
Q

what’s this ?

A

this is a pointer to the current class instance

53
Q

overload + to use it on myClass objects

A

MyClass operator+(MyClass c) :
MyClass a;
a.num=this->num+c.num
return(a);

54
Q

define accessibility in public, protected, and private inheritance :

A

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
Q

sizeof(char)

A

1 byte = 8 bits

56
Q

define a string s in C style

A

const char * s = “Hello”;

57
Q

a string in c++ is …

A

an array of chars

58
Q

difference between single and double quote strings

A

single: char, double: char ptr

59
Q

include string library

A

include

60
Q

how does the compiler know const char * c has ended in :

cout &laquo_space;c;

A

the nulle termination character \0

61
Q

declare a c++ string and find its size

A

std::string name = “Cherno”;
name.size();

62
Q

What does static mean outside a class ?
static int s_ = 5;

A

This variable is only going to be linked internally inside this translation unit

63
Q

How do you access a variable in another .cpp from main.cpp ? what if we mark it as static now ?

A

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
Q

What does static mean inside a class ?

A

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
Q

What are enums and how are they used ?

A

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
Q

A const variable must be initialized …

A

At definition

67
Q

State two possible uses for the keyword “mutable”

A
  1. declaring a class variable as mutable allows const functions to modify it
  2. auto f = = mutable {x++;}
68
Q

A string’s end is marked using … ?

A

a null termination character ‘\0’

69
Q

What’s the use of member initialization list ?

A

Resource acquisition is initialization

We construct and give value at same time instead of calling constructor twice

70
Q

How do you instantiate an object of class Entity ? Give different possibilities

A
  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
Q

memcpy params ?

A

memcpy(dest, src, size)