Define Friend classes. What are advantages of using friend classes?
The friend function is a ‘non member function’ of a class. It can access non public members of the class. A friend function is external to the class definition.
A friend function has the following advantages:
- Provides additional functionality which is kept outside the class.
- Provides functions that need data which is not normally used by the class.
- Allows sharing private class information by a non member function.
What are zombie objects in C++. Explain its purpose.
An object creation of a class is failed before executing its constructor. As a constructor would not return a value, there is no confirmation that whether the constructor is executed completely or not.
An object that is freed does not crash the applications. The application debugging is allowed by making the all states of the object intact.
When are memory leaks important? What are the easiest ways to avoid memory leak?
Memory Leaks:
Memory leaks take up more memory space and causes slow calculation.
Ensure that the loops or conditional statements are executed completely to prevent memory leak occurrence.
The best and easiest way to avoid memory leaks is to make changes at the time of writing programs. The cause for this is, a properly planned program can avoid memory leaks. In addition to it, make a code snippet can be made simple, small which executes fast.
Explain the difference between Stack and Queue.
Stack is a Last in First out (LIFO) data structure whereas Queue is a First in First out (FIFO) data structure.
What is Stacks? Explain its uses.
Stack is a Last in First out (LIFO) data structure. It is a linear structure and one can insert and delete from one end only which is called the top. It is very effective in rectifying syntax errors like missing parentheses etc.
What is pure virtual function?
A pure virtual function is one which is initialized with 0 at the time of declaration.
What is a Wrapper class?
Wrapper classes are classes that allow primitive types to be accessed as objects.
What do you mean by Stack unwinding?
When an exception is thrown, C++ calls destructors to destroy all the objects formed since the beginning of the try block. The objects are destroyed in the reverse order of their formation. This process is called Stack unwinding.
What is the use of ‘using’ declaration?
In using-declaration, we have using keyword followed by a fully qualified name of a namespace member. This allows using the non-qualified name of that member in the scope of the using-declaration.
What is the difference between Mutex and Binary semaphore?
Semaphore synchronizes processes where as mutex synchronizes threads running in the same process.
What is the scope resolution operator?
Scope resolution operator allows a program to reference an identifier in the global scope that is hidden by another identifier with the same name in the local scope.
What is abstraction?
It is the process of hiding unwanted details from the user.
What are the advantages of inheritance?
Code reusability
Saves time in program development.
What is a conversion constructor?
It is a constructor that accepts one argument of a different type.
What is a default constructor?
A constructor that has no argument is a default constructor.
Explain the advantages if inline functions.
It relieves the burden involved in calling a function. It is used for functions that need fast execution.
Explain the different access specifiers for the class member in C++.
private: It is default one and can be access from class member of the same class.
protected: The protected members can be access from member functions of the same class or friend classes or from the members of their immediate derived class.
public: The public members can be access from anywhere the class is visible.
What is the importance of mutable keyword?
The mutable keyword allows the data member of a class to change within a const member function.
Pointer vs. reference.
A reference must be initialized at the time of declaration whereas not needed in case of pointer. A pointer can point to a null object.
A reference once initialize to refer to the object can't be reassigned to refer to other whereas reassignment is possible with pointer.
Explain how to call C functions from C++.
The extern keyword is used to call C functions from C++
extern "C" void showme()
What is static member?
A static member is created and initialize once and shared among all class objects.
Explain static member functions.
The static member functions are created to access static variables of a class.
How do we declare a class as a friend of another class?
class a
{
friend class b; // with this declaration, class b can access all members of a
};
Explain the private, protected and public inheritance.
private inheritance: all the public and protected members in base become private.
protected inheritance: all the public and protected members in base class become protected.
public inheritance: in case of public inheritance, public remains public and protected remains protected.
Class vs. Structure
Class
Class has private as default access specifier.
Default inheritance type is private.
Structure
Structure has public as default access specifier
Default inheritance type is public.
Explain with an example how to call a base member function from derived class member function.
class emp
{
public:
void empName()
{
cout << "emp-XYZ" <<>
Output
dept-XYZ
emp-XYZ
Is it possible to overload a constructor?
Yes it is possible. A constructor can be overloaded to pass different arguments to the object.
Overriding vs. overloading
Overloading means having methods with same name but different signature
Overriding means rewriting the virtual method of the base class
What do you mean by binding? Static vs. dynamic binding
Binding means connecting the function call to a function implementation.
Connecting the function call to the function implementation before execution is static binding whereas connection them at the run-time is dynamic binding
Define abstract class.
A class whose object can't be created is abstract class. You can create a class as abstract by declaring one or more virtual functions to pure.
Pure virtual functions are declared as below
virtual void show() = 0 ;
Previous Part3