C++ Flashcards

1
Q

How an can we define a method outside the class?

A

Declare it inside the class, and define it later on using the class name ‘::’ method name, at attached.

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

Describe public, private and protected

what is the access specifier by default?

A
  • ​Public - members are accessible outside the class*
  • Protected - members can only be accessed by inherited classes*
  • Private(default) - members are not accessible outside the class*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How inheritance is defined in C++?

A
  • Note:*
  • The public before the parent class means we keep the accesses of the parent class.*
  • When protected is declared, all public memebers of the parent becomes protected. The rest does not change.*
  • When private is declared, all members become private.*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe the alternative usage of the for loop?

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

What is stack unwinding? why is it problematic when using goto?

A
  • In C++, at the end of their scope, the stack calls the destuctor of objects allocated on the stack*
  • It is problematic because goto performs an unconditional jump,* ignores nesting levels, and does not cause any automatic stack unwinding. Thus we should use it within the same block of statements, especially in the presence of local variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe pass by reference

A
  • Mainly used when we want to create a function that changes the given parameters, but not only.*
  • If we want to use the parameters for other computation and pass by value demands expensive copying, we can also use pass by reference. Important to notice the by convention pass by refernece means “we are going to change the parameters”, so if we want just to use them, we’ll use const, as in the attached image.*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe default values in parameters

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

What are namespaces for?

A

They are useful to avoid name collisions

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

What is the using keyboard for?

A

It is for including a namespace, so that we don’t need to explicitly write namespace::x, but just x.

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

How templates are used?

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

How can we dynamically allocate memory for one object/ several objects, and how can they be freed?

A
  • int* x = new int;*
  • int*x = new int[5]*
  • delete x;*
  • delete [] x;*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we use overloadable operators?

A

type operator sign (parameters) { /*… body …*/ }

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

What happens when we define a class as const?

A

It is like all its members are const. In addition, all its methods must be of type const.

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

Describe class template and class template specialization.

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

What is the difference between the copy constructor and the copy assignemnt.

A

Copy constructor is called when there’s an initialization of an object.

Object a(Object b);

or

Object a = object b;

After the initialization, the above procedure becomes an assignment, and the copy assignment is called.

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

What is the functionality of the move constructor and move assignment?

A

It is used for assigning, or initializng an object with temporary object. That is, object that has no name.

17
Q

describe the special members

A
18
Q

What are friend function/class?

when is it needed?

A

It is a class/method which has accesses to the members of its friend, even if they’re private.

Usually when we need function to manipulate data from two classes.

19
Q

Explain Polymorphism and the virtual keyboard

A

Polymorphism lets you assign child classes to a parent class. Using the virtual keyboard before a method defined in the parent class, gives us the oppurtunity to redefine that method in its children and call the suitable function right from a parent pointer which holds instances of its children.

If virtual is not mentioned, all calls to the shared method will execute the parent’s implementation.

20
Q

describe pure virtual function and abstract base classes

A

These classes cannot instantiate an object but we can create an object to them! so basically they serve for polymorphism.

Virutal function are defined using =0.