C++ Interview Q's Flashcards
What is the difference b/w Shallow Copy & Deep Copy
Shallow Copy:
- Stored the reference of the object to the original memory address
- Faster
- Shallow copy reflects changes made to the new/copied object in the original object
Deep Copy:
- Makes a new separate copy of an entire object with a unique memory address
- Slower
- DC doesn’t reflect the changes in the new/copied object to the original object
What is a Copy Constructor?
- a constructor that initializes an object using another object of the same class.
- these can be defined, otherwise, the default constructor will be called which does a member-wise copy between objects
Explain Inheritance
- the process of creating new classes that derive from an existing (base) class
- these derived classes inherit all the capabilities of the base class but also add new features and refinements of their own
What are static members and static member functions?
- static member: when a member is declared static, memory is allocated for this variable for the lifetime of the program. There will only be one copy of the static member, no matter how many objects of that class have been created.
- static member function: a function that can be called even if no object of that class has been created. It can be accessed by using the class name followed by the scope.
What are destructor?
a function that gets called when an object is destroyed. it has the same name as the constructor or class but is preceded by a tilde
What is call-by-value and call-by-reference?
- call-by-value : a copy of the parameter is passed to the function, memory is assigned for these copied values and any changes do not reflect the variable in the main function.
- call-by-reference: the address of the variable is passed and the address is used to access the variable, so changes made in the parameter alter the passing argument.
Is destructor overloading possible?
no, because destructors don’t take any arguments. since there are no arguments, there is only one way to destroy an object
What is meant by ‘abstraction’ in C++?
abstraction is the process of showing the essential details to the user and hiding the irrelevant details from the user or hiding details we don’t want to show to the user
What is a reference?
a reference is like a pointer, it is another name for an already existing variable. once a reference name is initialized that variable can be accessed by the variable name or the reference name. once the reference is initialized, it cannot refer to any other variable.
and an array of references is not possible like an array of pointers
what is an inline function?
if a function is inline, the compiler places a copy of the code at each invocation of it. this is to avoid function calling overhead