C++ Flashcards
How an can we define a method outside the class?
Declare it inside the class, and define it later on using the class name ‘::’ method name, at attached.
Describe public, private and protected
what is the access specifier by default?
- 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 inheritance is defined in C++?
- 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.*
Describe the alternative usage of the for loop?
What is stack unwinding? why is it problematic when using goto?
- 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.
Describe pass by reference
- 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.*
Describe default values in parameters
What are namespaces for?
They are useful to avoid name collisions
What is the using keyboard for?
It is for including a namespace, so that we don’t need to explicitly write namespace::x, but just x.
How templates are used?
How can we dynamically allocate memory for one object/ several objects, and how can they be freed?
- int* x = new int;*
- int*x = new int[5]*
- delete x;*
- delete [] x;*
How can we use overloadable operators?
type operator sign (parameters) { /*… body …*/ }
What happens when we define a class as const?
It is like all its members are const. In addition, all its methods must be of type const.
Describe class template and class template specialization.
What is the difference between the copy constructor and the copy assignemnt.
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.