Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

51.

Explain The Concept Of Memory Leak.

Answer»

When a variable does not exist longer in the memory, and deletion or reuse of that variable cannot be done, then its DESTRUCTION occurs AUTOMATICALLY. This concept is called memory LEAK.
For example, CONSIDER the following code snippet:

{ parent*p=new parent(); }

In the preceding code snippet, the p variable does not exist in the memory, but the variable is not deleted. Therefore, it becomes out of scope and its destruction occurs, which RESULTS in memory leak.

When a variable does not exist longer in the memory, and deletion or reuse of that variable cannot be done, then its destruction occurs automatically. This concept is called memory leak.
For example, consider the following code snippet:

In the preceding code snippet, the p variable does not exist in the memory, but the variable is not deleted. Therefore, it becomes out of scope and its destruction occurs, which results in memory leak.

52.

What Is A Dangling Pointer?

Answer»

When the LOCATION of the deallocated MEMORY is pointed by the pointer even after the DELETION or allocation of OBJECTS is done, without the modification in the value of the pointer, then this TYPE of pointer is called a dangling pointer.

When the location of the deallocated memory is pointed by the pointer even after the deletion or allocation of objects is done, without the modification in the value of the pointer, then this type of pointer is called a dangling pointer.

53.

How Can An Improvement In The Quality Of Software Be Done By Try/catch/throw?

Answer»

Error-handling is necessity while developing applications to account for unexpected situations, such as insufficient memory, resource allocation errors, inability to find/open files, division by zero, an arithmetic or array OVERFLOW, and the exhaustion of FREE HEAP space, which occur at runtime.

Programmers have various styles of dealing with such exceptions, such as try/catch /throw methods, which cause diversity in coding PRACTICE. This diversity increases with the use of user-defined classes, as each class brings with it potential class-specific errors. These methods provide a standard facility to deal with runtime exceptions. Moreover, try/catch/throw result in the code with fewer errors. It is low in cost in relation to development. With error-handling, your program can detect unexpected events and recover from them.

Error-handling is necessity while developing applications to account for unexpected situations, such as insufficient memory, resource allocation errors, inability to find/open files, division by zero, an arithmetic or array overflow, and the exhaustion of free heap space, which occur at runtime.

Programmers have various styles of dealing with such exceptions, such as try/catch /throw methods, which cause diversity in coding practice. This diversity increases with the use of user-defined classes, as each class brings with it potential class-specific errors. These methods provide a standard facility to deal with runtime exceptions. Moreover, try/catch/throw result in the code with fewer errors. It is low in cost in relation to development. With error-handling, your program can detect unexpected events and recover from them.

54.

What Is The Role Of Copy Constructor In Copying Of Thrown Objects?

Answer»

A COPY CONSTRUCTOR with a PUBLIC access SPECIFIER must be applied to the objects which are thrown.With the help of a compiler, the code is generated through which COPYING of thrown objects can be done. The thrown objects must have a copy constructor with the public access specifier.

A copy constructor with a public access specifier must be applied to the objects which are thrown.With the help of a compiler, the code is generated through which copying of thrown objects can be done. The thrown objects must have a copy constructor with the public access specifier.

55.

How Can I Be Thrown Polymorphically?

Answer»

The following code can be used to throw i polymorphically:

classExceptionParent { ); CLASSE,cceptionChild : public ExceptionParent ( ); void f(ExceptionParent8 e) /1 throw e; void go ExceptionChild e; TRY( fCc); catch (ExceptionChild& e) ( //...code to handle ExceptlonChild... catch C...) ( //...code to handle other exceptions... ) )

In the PRECEDING code, you can enter the catch (...) clause in the absence of throwing i polymorphically. The throw e in the function f () throws the same type of an object as an expression of STATIC type.

The following code can be used to throw i polymorphically:

In the preceding code, you can enter the catch (...) clause in the absence of throwing i polymorphically. The throw e in the function f () throws the same type of an object as an expression of static type.

56.

Discuss The Effects Occur, After An Exception Thrown By A Member Function Is Unspecified By An Exception Specification?

Answer»

When a function THROWS an exception, not given in the specification, the exception is passed to a system function named unexpected. The unexpected function CALLS the latest function named as an ARGUMENT in a CALL to the set_unexpected function, which RETURNS its current settings. A function with no exception specification, by default, calls the terminate function, which finally calls the abort (function to terminate the program).

When a function throws an exception, not given in the specification, the exception is passed to a system function named unexpected. The unexpected function calls the latest function named as an argument in a call to the set_unexpected function, which returns its current settings. A function with no exception specification, by default, calls the terminate function, which finally calls the abort (function to terminate the program).

57.

Is It Possible For A Member Function To Use Delete This?

Answer»

It is possible for a member function to use delete this but on certain conditions which are as follows:

  1.  Ensure that the allocation of this OBJECT is done through new[] operator.
  2. Ensure that the invocation of a member function done on this (current) object will be the last member function.
  3. Ensure that calling of other member functions and data members should not be done after the line of code which includes delete this.
  4. Examination or comparison of this pointer with other pointers and NULL, printing or casting it, MUST be avoided after using the delete this.

The preceding points are applied in the cases where the this pointer BELONGS to the base class in the absence of the VIRTUAL destructor.

It is possible for a member function to use delete this but on certain conditions which are as follows:

The preceding points are applied in the cases where the this pointer belongs to the base class in the absence of the virtual destructor.

58.

Write About The Retrieval Of N Number Of Objects During The Process Of Delete[]p?

Answer»

<P>The n NUMBER of OBJECTS can be retrieved with the help of the pointer p (it stores the n number of objects) at the run TIME.
This can be achieved by the application of two techniques, which are:

  1. An ARRAY which can be associated with p in the form of key and a value such as n.
  2. Over-allocation of the array can be done. The preceding techniques are used by the compilers of commercial grade.

The n number of objects can be retrieved with the help of the pointer p (it stores the n number of objects) at the run time.
This can be achieved by the application of two techniques, which are:

59.

What Would Happen On Forgetting [], While Deallocating An Array Through New?

Answer»

If you forget to use [] while deallocating an array through NEW, it throws a run time or compile time EXCEPTION and results in the corruption of the heap.Therefore, it is the responsibility of the programmer to establish the CONNECTION between T[n] and DELETE[]p correctly.

If you forget to use [] while deallocating an array through new, it throws a run time or compile time exception and results in the corruption of the heap.Therefore, it is the responsibility of the programmer to establish the connection between T[n] and delete[]p correctly.

60.

On Throwing An Exception By The Animal Constructor In P = New Animalq, Can Memory Leak Occur?

Answer»

Memory of an animal class cannot leak by throwing an EXCEPTION in p = new Animal(). In case of OCCURRENCE of an exception during the Animal constructor of p = new Animal(),there is a surety of the automatic releasing of the ALLOCATED memory BACK to the heap.

Memory of an animal class cannot leak by throwing an exception in p = new Animal(). In case of occurrence of an exception during the Animal constructor of p = new Animal(),there is a surety of the automatic releasing of the allocated memory back to the heap.

61.

Is It Possible To Use A New For The Reallocation Of Pointers ?

Answer»

The REALLOCATION of POINTERS cannot be DONE by USING new. It can be done by using the reallocQ operator.

The reallocation of pointers cannot be done by using new. It can be done by using the reallocQ operator.

62.

Can A New Be Used In Place Of Old Mallocq? If Yes, Why?

Answer»

The new operator should be used in place of OLD malloc because the new operator ENSURES the CALLING of an appropriate DESTRUCTOR at the TIME of execution and also it is more type-safe than mallocQ.

The new operator should be used in place of old malloc because the new operator ensures the calling of an appropriate destructor at the time of execution and also it is more type-safe than mallocQ.

63.

Explain The Term Memory Alignment?

Answer»

The primary meaning of the term alignment is to maintain the appropriate positioning of different components in the memory with respect to each other. In C++, there is a requirement of setting of various OBJECTS and VARIABLES in a particular way in the system's memory.Therefore, many data variables are aligned automatically by the COMPILER ACCORDING to their processor and TYPE.

The primary meaning of the term alignment is to maintain the appropriate positioning of different components in the memory with respect to each other. In C++, there is a requirement of setting of various objects and variables in a particular way in the system's memory.Therefore, many data variables are aligned automatically by the compiler according to their processor and type.

64.

How A New Operator Differs From The Operator New?

Answer»

The NEW operator creates a class's new instance. On the other hand, overloading of a new operator is done globally with the help of the operator new. The new operator allocates memory for the ITEM and assigns the address of that memory to the pointer by using the name of an item with a pointer of a DATA type, structure, or array. For example, consider the following code snippet:

Double * pi = new double;

In the PRECEDING code snippet, the new operator returns a pointer to the double variable, because it allocates the SPACE for a double value.

The new operator creates a class's new instance. On the other hand, overloading of a new operator is done globally with the help of the operator new. The new operator allocates memory for the item and assigns the address of that memory to the pointer by using the name of an item with a pointer of a data type, structure, or array. For example, consider the following code snippet:

In the preceding code snippet, the new operator returns a pointer to the double variable, because it allocates the space for a double value.

65.

How The Delete Operator Differs From The Delete[]operator?

Answer»

When the new[] OPERATOR is USED to allocate memory dynamically, the DELETE[]operator is used to free the memory. The new[] operator is used to allocate memory to an array of VALUES, which starts with the index 0.

When the new[] operator is used to allocate memory dynamically, the delete[]operator is used to free the memory. The new[] operator is used to allocate memory to an array of values, which starts with the index 0.

66.

How New/delete Differs From Malloc()/free?

Answer»

The memory uses the MALLOC() operator, which in turn uses the FREE operator, to remove the unnecessary PROGRAMS. On the other hand, the program builds the array by using the NEW operator, fills it with random numbers, DISPLAYS each of the elements in the array, and deletes the array by using the delete operator. The new and delete operators should be used in C++ because they are type safe. The malloc operator is used when there is a need of forcing a type on an object because a void pointer is returned by it. Moreover, that object cannot be assigned to other types.

The memory uses the malloc() operator, which in turn uses the free operator, to remove the unnecessary programs. On the other hand, the program builds the array by using the new operator, fills it with random numbers, displays each of the elements in the array, and deletes the array by using the delete operator. The new and delete operators should be used in C++ because they are type safe. The malloc operator is used when there is a need of forcing a type on an object because a void pointer is returned by it. Moreover, that object cannot be assigned to other types.

67.

How The Keyword Struct Is Different From The Keyword Class In C++?

Answer»

In C++, a class is similar to a STRUCT with the exception that, by default, all the MEMBERS of a class are private; while the members of a struct are public. Encapsulation is not supported by STRUCTURES but supported by classes.

In C++, a class is similar to a struct with the exception that, by default, all the members of a class are private; while the members of a struct are public. Encapsulation is not supported by structures but supported by classes.

68.

Can Encapsulation Be Called As A Security Device?

Answer»

Encapsulation hides important and classified data from accidental manipulation by members of the same program. Note that the encapsulated data of a class can be accessible outside the class only when the data is USED in the function of the class. However, a LIMITED number of operations can be performed on encapsulated data by executing the functions or methods of the class. An encapsulation prevents the internal error to OCCUR and not the explicit attacks or intentional attacks and therefore cannot be called as a SECURITY device.

Encapsulation hides important and classified data from accidental manipulation by members of the same program. Note that the encapsulated data of a class can be accessible outside the class only when the data is used in the function of the class. However, a limited number of operations can be performed on encapsulated data by executing the functions or methods of the class. An encapsulation prevents the internal error to occur and not the explicit attacks or intentional attacks and therefore cannot be called as a security device.

69.

Can Non-public Members Of Another Instance Of The Class Be Retrieved By The Method Of The Same Class?

Answer»

Yes, the non-public members of ANOTHER instance of the CLASS can be RETRIEVED by the method of the same class. RETRIEVAL depends on the TYPE of the class whether it is of a reference/pointer/object class. It seems to break the rule of encapsulation. However, there is a need of get () method with a public specifier for the non-public members to retrieve the method of the same class because an explicit argument (means the pointer by which "this" is not called) can be taken by at least one method of the same class.

Yes, the non-public members of another instance of the class can be retrieved by the method of the same class. Retrieval depends on the type of the class whether it is of a reference/pointer/object class. It seems to break the rule of encapsulation. However, there is a need of get () method with a public specifier for the non-public members to retrieve the method of the same class because an explicit argument (means the pointer by which "this" is not called) can be taken by at least one method of the same class.

70.

How Can You Prevent Accessing Of The Private Parts Of My Class By Other Programmers (violating Encapsulation)?

Answer»

ENCAPSULATION only hides the details of the implementation from users of the data type, and PROVIDES a public user INTERFACE. Encapsulation only maintains the INTEGRITY of the internal coding inside the methods of a class, but not able to prevent accessing the private or PROTECTED parts of a class by the user.

Encapsulation only hides the details of the implementation from users of the data type, and provides a public user interface. Encapsulation only maintains the integrity of the internal coding inside the methods of a class, but not able to prevent accessing the private or protected parts of a class by the user.

71.

Write About The Role Of C++ In The Tradeoff Of Safety Vs. Usability?

Answer»

Earlier in C, an encapsulation is achieved by MAKING the methods static in a class. On the contrary, in C++, it would not work.

Programmers USE structs in C for making multiple instances. The creation of multiple instances is not supported directly by the data with static keyword in a class. This situation made the concept of tradeoff between safety (information hiding) and usability (multiple instances) WORSE.
On the contrary, C++ supported both multiple instances and encapsulation with the help of the concept of a class. The public part of a class includes the interface of the class which includes public member functions and friends functions of a class. The private and/or protected parts of a class includes an IMPLEMENTATION of the class which includes the data. This results in a struct which is encapsulated. This depicts the STRONG tradeoff between safety (information hiding) and usability(multiple instances).

Earlier in C, an encapsulation is achieved by making the methods static in a class. On the contrary, in C++, it would not work.

Programmers use structs in C for making multiple instances. The creation of multiple instances is not supported directly by the data with static keyword in a class. This situation made the concept of tradeoff between safety (information hiding) and usability (multiple instances) worse.
On the contrary, C++ supported both multiple instances and encapsulation with the help of the concept of a class. The public part of a class includes the interface of the class which includes public member functions and friends functions of a class. The private and/or protected parts of a class includes an implementation of the class which includes the data. This results in a struct which is encapsulated. This depicts the strong tradeoff between safety (information hiding) and usability(multiple instances).

72.

Define A Good Interface?

Answer»

A good interface is the one that HIDES unnecessary details and PROVIDES a SIMPLIFIED way for making applications with all the required information and different software related to different applications. A good interface should PROVIDE users an opportunity to EXPLORE and understand the different concepts related to the tasks they want to do. In this way, it ensures good communication between the user and the computer.

A good interface is the one that hides unnecessary details and provides a simplified way for making applications with all the required information and different software related to different applications. A good interface should provide users an opportunity to explore and understand the different concepts related to the tasks they want to do. In this way, it ensures good communication between the user and the computer.

73.

What Is An Object?

Answer»

An object is a declared instance of a data type, which includes standard C++ data types as well as objects of classes. In other words, they are the variables that you declare in the class. In OOP, objects REPRESENT REAL world entities, such as students, doctors, and bank accounts. These objects can interact with each other by SENDING or RECEIVING messages. An object has the following THREE main characteristics:

  •  It has a state
  •  It may or may not display a behavior
  •  It has a unique identity

An object is a declared instance of a data type, which includes standard C++ data types as well as objects of classes. In other words, they are the variables that you declare in the class. In OOP, objects represent real world entities, such as students, doctors, and bank accounts. These objects can interact with each other by sending or receiving messages. An object has the following three main characteristics:

74.

Describe The Process Of Creation And Destruction Of A Derived Class Object?

Answer»

When you declare an OBJECT of a derived class, stacks or heaps allocate the space for the object. This space contains the inherited data members from the base class and also the members defined in its derived class. The initialization of the inherited data members from the base class is done with the help of the constructor of a base class. Therefore, compiler executes the constructor function of the base class followed by the constructor function of the derived class.
When an object goes out of scope, the destructors EXECUTE in the REVERSE order of the CONSTRUCTORS. Firstly, the destructor of the derived class is invoked to destroy the derived class object, and then the destructor of a base class is invoked. This process helps to allocate the required space.

When you declare an object of a derived class, stacks or heaps allocate the space for the object. This space contains the inherited data members from the base class and also the members defined in its derived class. The initialization of the inherited data members from the base class is done with the help of the constructor of a base class. Therefore, compiler executes the constructor function of the base class followed by the constructor function of the derived class.
When an object goes out of scope, the destructors execute in the reverse order of the constructors. Firstly, the destructor of the derived class is invoked to destroy the derived class object, and then the destructor of a base class is invoked. This process helps to allocate the required space.

75.

Write About The Members That A Derived Class Can Add?

Answer»

DERIVED classes can use the constructors, DESTRUCTORS, and assignment operator methods of their base classes. It can also override the member functions of the base CLASS. For EXAMPLE, if the base class has an assignment operator method, the compiler uses that method for the derived class as well, which is fine unless the derived class also adds its own data members (in which CASE, you should create a new operator = method for the derived class).

Derived classes can use the constructors, destructors, and assignment operator methods of their base classes. It can also override the member functions of the base class. For example, if the base class has an assignment operator method, the compiler uses that method for the derived class as well, which is fine unless the derived class also adds its own data members (in which case, you should create a new operator = method for the derived class).

76.

What Is Overriding?

Answer»

To override a method, a subclass of the class that originally declared the method MUST declare a method with the same NAME, return type (or a subclass of that return type), and same parameter LIST.
Overriding a method means replacing the method's functionality in a CHILD class. To implement overriding functionality, you NEED parent and child classes. In the child class, you define the same method signature as the one defined in the parent class.

To override a method, a subclass of the class that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list.
Overriding a method means replacing the method's functionality in a child class. To implement overriding functionality, you need parent and child classes. In the child class, you define the same method signature as the one defined in the parent class.

77.

Write About All The Implicit Member Functions Of A Class?

Answer»

All the implicit member functions of a class are LISTED as follows:
a. Default constructor —Defines a constructor with no parameters.
b. Copy CONSTRUCTORS —Initializes the VALUE from an existing object of a class to a new, instantiated object of that same class.
c. Assignment operator—-Returns a value.
d. Default destructor—Runs automatically when an object is being destroyed.
e. Address operator —Takes one OPERAND and returns the MEMORY address of a variable or function.

All the implicit member functions of a class are listed as follows:
a. Default constructor —Defines a constructor with no parameters.
b. Copy constructors —Initializes the value from an existing object of a class to a new, instantiated object of that same class.
c. Assignment operator—-Returns a value.
d. Default destructor—Runs automatically when an object is being destroyed.
e. Address operator —Takes one operand and returns the memory address of a variable or function.

78.

What Are The Effects After Calling The Delete This Operator ?

Answer»

It is difficult for the compiler to KNOW whether an object is allocated on a stack or a heap. On INVOKING the delete this operator, the destructor is CALLED twice on the object related to the current context. Firstly implicitly and SECONDLY explicitly. It results in undefined behavior and error-prone conditions. The delete this operator can only WORK on three conditions:

  •  An Instantiation of a local variable should not take place, after calling the delete this destructor.
  •  The this pointer should not be used after calling the delete this operator.
  •  The object of a derived class should be allocated and initialized with the new operator

It is difficult for the compiler to know whether an object is allocated on a stack or a heap. On invoking the delete this operator, the destructor is called twice on the object related to the current context. Firstly implicitly and secondly explicitly. It results in undefined behavior and error-prone conditions. The delete this operator can only work on three conditions:

79.

Write A Note On Encapsulation?

Answer»

Encapsulation is an OBJECT-oriented design approach that closely binds the implementation of a class to its data representation, logically hides the details of the implementation from users of the data TYPE, and PROVIDES a public interface to the data type's BEHAVIOR. It implies an implementation, which is hidden from the class user, and an interface, which is visible to the class user. It helps you to handle large scale programming tasks.The idea behind encapsulation is to take a complex system that demands a lot of attention and to turn it into an object that handles all its WORK internally and can easily form a concept.

Encapsulation is an object-oriented design approach that closely binds the implementation of a class to its data representation, logically hides the details of the implementation from users of the data type, and provides a public interface to the data type's behavior. It implies an implementation, which is hidden from the class user, and an interface, which is visible to the class user. It helps you to handle large scale programming tasks.The idea behind encapsulation is to take a complex system that demands a lot of attention and to turn it into an object that handles all its work internally and can easily form a concept.

80.

Write About The Use Of The Virtual Destructor?

Answer»

When an object is declared with the new operator and the pointer type is that of a base class with a non-virtual destructor, the base destructor executes instead of the derived destructor. When a base class destructor is virtual, the compiler calls the correct destructor function irrespective of the type of the pointer.
If the base class needs no custom destruction, you must still provide a virtual destructor (with an empty BLOCK) to permit the PROPER destructor calls for dynamically allocated objects. Moreover, by making the destructor of the base class as virtual, you can INVOKE the DESTRUCTORS of both the base and derived classes in the reverse order.

When an object is declared with the new operator and the pointer type is that of a base class with a non-virtual destructor, the base destructor executes instead of the derived destructor. When a base class destructor is virtual, the compiler calls the correct destructor function irrespective of the type of the pointer.
If the base class needs no custom destruction, you must still provide a virtual destructor (with an empty block) to permit the proper destructor calls for dynamically allocated objects. Moreover, by making the destructor of the base class as virtual, you can invoke the destructors of both the base and derived classes in the reverse order.

81.

How Do C++ Struct Differs From The C++ Class?

Answer»

C++ defines structures and classes ALMOST identically. To declare a class, you USE the class keyword in place of the struct keyword. The only other differences are related to the default access SPECIFIERS. The members of a structure have public access by default; WHEREAS, the member of a class has private access by default. The concept of ENCAPSULATION is supported by the class only and not the structure.

C++ defines structures and classes almost identically. To declare a class, you use the class keyword in place of the struct keyword. The only other differences are related to the default access specifiers. The members of a structure have public access by default; whereas, the member of a class has private access by default. The concept of encapsulation is supported by the class only and not the structure.

82.

Describe The Setting Up Of My Member Functions To Avoid Overriding By The Derived Class?

Answer»

To avoid overriding of the member functions by the derived CLASS, the leaf method is used. With the HELP of the leaf method, it is possible to leave the code unaligned at the TIME of execution by simply ADDING a comment next to the method. This method is easy, fast, and INEXPENSIVE to use.

To avoid overriding of the member functions by the derived class, the leaf method is used. With the help of the leaf method, it is possible to leave the code unaligned at the time of execution by simply adding a comment next to the method. This method is easy, fast, and inexpensive to use.

83.

Which Should Be More Useful: The Protected And Public Virtuals?

Answer»

PUBLIC virtuals are more preferable than protected virtuals. Public virtuals permit a PROGRAM to directly modify the values of data members. It can be accessible to any function that is within the scope of the structure. It translates the IMPLEMENTATION for the user of the class. It is the most common, convenient, and easiest way to implement by any of the programs.
On the other hand, protected virtual functions are USED for hiding some methods or data members from the OUTSIDE class making it consistent and symmetrical in its approach. It is condition specific, so not commonly used.

Public virtuals are more preferable than protected virtuals. Public virtuals permit a program to directly modify the values of data members. It can be accessible to any function that is within the scope of the structure. It translates the implementation for the user of the class. It is the most common, convenient, and easiest way to implement by any of the programs.
On the other hand, protected virtual functions are used for hiding some methods or data members from the outside class making it consistent and symmetrical in its approach. It is condition specific, so not commonly used.

84.

Write About Abstract Base Classes?

Answer»

An abstract base CLASS is a class definition that is always a base class for other classes to be derived from. No specific objects of the base class are declared by the program. A C++ abstract base class is one that has a pure virtual FUNCTION, a protected constructor, or a protected destructor.
At the design LEVEL, you create a pure virtual method by using = 0 in place of a method body. This notation specifies that the MEMBER function is a pure virtual function. This means that the base class is an abstract base class, and the class designer intends the class to be used only as a base class. The base class may or may not provide a function body for the pure virtual function. In either CASE, a program that uses this class may not directly declare any objects of the abstract base class. If the program declares an object of a class directly or indirectly derived from the abstract base class, the pure virtual function must be overridden explicitly.

An abstract base class is a class definition that is always a base class for other classes to be derived from. No specific objects of the base class are declared by the program. A C++ abstract base class is one that has a pure virtual function, a protected constructor, or a protected destructor.
At the design level, you create a pure virtual method by using = 0 in place of a method body. This notation specifies that the member function is a pure virtual function. This means that the base class is an abstract base class, and the class designer intends the class to be used only as a base class. The base class may or may not provide a function body for the pure virtual function. In either case, a program that uses this class may not directly declare any objects of the abstract base class. If the program declares an object of a class directly or indirectly derived from the abstract base class, the pure virtual function must be overridden explicitly.

85.

How Can You Differentiate Between Inheritance And Implementation In C++?

Answer»

With the help of ABSTRACT base classes, we can DIFFERENTIATE between interface and implementation in C++.

With the help of abstract base classes, we can differentiate between interface and implementation in C++.

86.

Why Do We Separate Interface From Implementation?

Answer»

The INTERFACE is visible to the user of the class and consists of public members, which are usually member FUNCTIONS. The class user reads and modifies values in data representation by CALLING public member functions. The interface is generic in that it is not bound to any particular implementation.

The implementation of a class, which consists of private data members and private member functions, is essentially hidden from the program. The implementation defines the details of how the class implements the behavior of the abstract BASE type. The class author should be able to CHANGE the implementation without affecting the program.

The interface is visible to the user of the class and consists of public members, which are usually member functions. The class user reads and modifies values in data representation by calling public member functions. The interface is generic in that it is not bound to any particular implementation.

The implementation of a class, which consists of private data members and private member functions, is essentially hidden from the program. The implementation defines the details of how the class implements the behavior of the abstract base type. The class author should be able to change the implementation without affecting the program.

87.

Can Circle Be Called An Ellipse?

Answer»

Yes, a CIRCLE can be called an ellipse. LET's understand this concept with the help of an EXAMPLE, if ellipse has a member function named as setsize with the widthQ of the object as x and its HEIGHT() as y. There are two kinds of relationships that exist between a circle and an ellipse:

  • Circle and ellipse can be made as two different classes :

In this case, ellipse can be derived from AsymmetricShape class and has a member function named as setSize(x,y). On the contrary, circle can be derived from SymmetricalShape class and has a member function named as setSize(size). Therefore, they both are UNRELATED in its member functions as well as in their derivations.

  •  Circle and ellipse can be derived from a base class :

In this case, circle and ellipse both can be inherited from the class Oval because class Oval can only have setSize(size) member function which sets the height() and widthQto the size of the object.

Yes, a circle can be called an ellipse. Let's understand this concept with the help of an example, if ellipse has a member function named as setsize with the widthQ of the object as x and its height() as y. There are two kinds of relationships that exist between a circle and an ellipse:

In this case, ellipse can be derived from AsymmetricShape class and has a member function named as setSize(x,y). On the contrary, circle can be derived from SymmetricalShape class and has a member function named as setSize(size). Therefore, they both are unrelated in its member functions as well as in their derivations.

In this case, circle and ellipse both can be inherited from the class Oval because class Oval can only have setSize(size) member function which sets the height() and widthQto the size of the object.

88.

Should The Member Functions Which Are Made Public In The Base Class Be Hidden?

Answer»

As the PUBLIC member FUNCTIONS of the base CLASS are necessary to implement the public interface of the class, the member functions which are made public in the base class should never be hidden. When you are designing a class, make the derived data members PRIVATE because a private member of the derived class is accessed through the protected or public member functions of the base class.

As the public member functions of the base class are necessary to implement the public interface of the class, the member functions which are made public in the base class should never be hidden. When you are designing a class, make the derived data members private because a private member of the derived class is accessed through the protected or public member functions of the base class.

89.

Write A Note About The Virtual Member Function?

Answer»

A virtual FUNCTION is a member function of the BASE class and relies on a specific object to determine which IMPLEMENTATION of the function is called. However, a virtual function can be declared a friend of ANOTHER class. If a function is declared virtual in a base class, you can still access it directly USING the :: operator. Note that if you do not override a virtual member function in a derived class, a call to that function uses the function implementation defined in the base class.

A virtual function is a member function of the base class and relies on a specific object to determine which implementation of the function is called. However, a virtual function can be declared a friend of another class. If a function is declared virtual in a base class, you can still access it directly using the :: operator. Note that if you do not override a virtual member function in a derived class, a call to that function uses the function implementation defined in the base class.

90.

Describe Inheritance And Non-inheritance Of A Derived Class?

Answer»

Using inheritance, you can derive all the data members and all the other common FUNCTIONS of the BASE class and still retain the functionality of the common methods of the base class. For example, the animal class with eat, sleep and breathe methods COMPRISES the base class because they are common to all animals. Now, a new class ELEPHANT with a method trumpet is derived from the class animal. With the help of inheritance, the Elephant class inherits the trumpet method and still retains the eat, sleep, and breathe methods from the base class, animal.

Therefore, you can add new methods to an existing class with the retaining of common methods. There are few methods that are not inherited on creating a derived class which includes constructors, destructors, and assignment operator methods of their base CLASSES.

Using inheritance, you can derive all the data members and all the other common functions of the base class and still retain the functionality of the common methods of the base class. For example, the animal class with eat, sleep and breathe methods comprises the base class because they are common to all animals. Now, a new class Elephant with a method trumpet is derived from the class animal. With the help of inheritance, the Elephant class inherits the trumpet method and still retains the eat, sleep, and breathe methods from the base class, animal.

Therefore, you can add new methods to an existing class with the retaining of common methods. There are few methods that are not inherited on creating a derived class which includes constructors, destructors, and assignment operator methods of their base classes.

91.

Explain The Concept Of Multiple Inheritance (virtual Inheritance). Write About Its Advantages And Disadvantages?

Answer»

Multiple inheritance is defined as the ability of a derived class to inherit the characteristics of more than one base class. For example, you MIGHT have a class zoo which inherits VARIOUS animal classes. The advantage of multiple inheritance is that you can create a single derived class from several base classes. It HELPS in understanding the logic behind these complex relationships.

The multiple inheritance is not preferred by the programmers because with multiple base classes, there are chances of conflicts while calling a method in the sub class that exists in both the base classes. For example, if you have a class NAMED zoo that inherits both the Anaconda and Zebra classes, then each base class might define a different version of the method walk, which creates a CONFLICTING situation.

Multiple inheritance is defined as the ability of a derived class to inherit the characteristics of more than one base class. For example, you might have a class zoo which inherits various animal classes. The advantage of multiple inheritance is that you can create a single derived class from several base classes. It helps in understanding the logic behind these complex relationships.

The multiple inheritance is not preferred by the programmers because with multiple base classes, there are chances of conflicts while calling a method in the sub class that exists in both the base classes. For example, if you have a class named zoo that inherits both the Anaconda and Zebra classes, then each base class might define a different version of the method walk, which creates a conflicting situation.

92.

How Can You Say That A Template Is Better Than A Base Class?

Answer»

In case of designing a generic data TYPE to MANAGE OBJECTS of other data TYPES which are unknown to their container class, a template is preferred over a base class. A template can contain more than one data type parameter, making it possible to build parameterized data types of considerable complexity. The class template also allows the declaration of more than one object in the same program.

In case of designing a generic data type to manage objects of other data types which are unknown to their container class, a template is preferred over a base class. A template can contain more than one data type parameter, making it possible to build parameterized data types of considerable complexity. The class template also allows the declaration of more than one object in the same program.

93.

What Is The Need Of Multiple Inheritance?

Answer»

The multiple inheritance allows you to define a new class that inherits the CHARACTERISTICS of several base CLASSES which are not related to each other. The vehicle class encapsulates the data and behavior that describe vehicles along with other information, such as date of purchase, life, and maintenance schedules. Classes that SUPPORT specific kinds of vehicles, such as trucks, airplanes, and cars are also derived from the vehicle class. The asset class encapsulates the data and behavior of the ORGANIZATION's assets, including acquiring date, and depreciation schedule data for accounting purposes. A company car, being both a vehicle and an asset, is represented by a class that derives from both base classes.

The multiple inheritance allows you to define a new class that inherits the characteristics of several base classes which are not related to each other. The vehicle class encapsulates the data and behavior that describe vehicles along with other information, such as date of purchase, life, and maintenance schedules. Classes that support specific kinds of vehicles, such as trucks, airplanes, and cars are also derived from the vehicle class. The asset class encapsulates the data and behavior of the organization's assets, including acquiring date, and depreciation schedule data for accounting purposes. A company car, being both a vehicle and an asset, is represented by a class that derives from both base classes.

94.

Write A Note About Inheritance?

Answer»

Inheritance is a mechanism that allows the object of one class to utilize the form and MODEL or BEHAVIOR of another class. A class derived from the base class inherits attributes, functions, or methods that implement the base class to the derived class and ALSO EXTENDS the derived class by overriding functions and adding new ADDITIONAL attributes and functionalities.

Inheritance is a mechanism that allows the object of one class to utilize the form and model or behavior of another class. A class derived from the base class inherits attributes, functions, or methods that implement the base class to the derived class and also extends the derived class by overriding functions and adding new additional attributes and functionalities.

95.

How The Virtual Functions Maintain The Call Up?

Answer»

The call is maintained through vtable. Each object INCLUDES a variable known as VPTR, which points to the class's COMMON vtable. It contains an entry for every VIRTUAL function. On calling the function, vtable calls the appropriate function USING vptr.

The call is maintained through vtable. Each object includes a variable known as vptr, which points to the class's common vtable. It contains an entry for every virtual function. On calling the function, vtable calls the appropriate function using vptr.

96.

What Do You Understand By Pure Virtual Function? Write About Its Use?

Answer»

A pure virtual function in a base CLASS must have a matching function in a derived class. A program MAY not declare an instance of a class that has a pure virtual function. A program may not declare an instance of a derived class if that derived class has not provided an OVERRIDING function for each pure virtual function in the base. If you WANT PROGRAMMERS to override certain functions in a class, such as those that need information customized for a particular installation, you should make these functions pure virtual.

A pure virtual function in a base class must have a matching function in a derived class. A program may not declare an instance of a class that has a pure virtual function. A program may not declare an instance of a derived class if that derived class has not provided an overriding function for each pure virtual function in the base. If you want programmers to override certain functions in a class, such as those that need information customized for a particular installation, you should make these functions pure virtual.

97.

How Can Virtual Functions In C++ Be Implemented?

Answer»

When a class has at least one virtual function, the compiler builds a table of virtual function pointers for that class. This table, commonly called the v-table, contains an entry for each virtual function in the class. The constructor of the class is used to create this table. Each object of the class in memory includes a VARIABLE called the vptr, which points to the class's common vtbl. The _rst (which creates the vtable) is constructed by the base classes after the construction of the DERIVED classes. The derived class constructor overwrites the entries of the vtable, if its constructor overrides any virtual function of the base class. Therefore, you need not call every function from a constructor just to avoid the ERROR prone SCENARIO of calling the Base class constructor instead of the derived class constructor (which fails to set the entries of objects of vtable).

When a class has at least one virtual function, the compiler builds a table of virtual function pointers for that class. This table, commonly called the v-table, contains an entry for each virtual function in the class. The constructor of the class is used to create this table. Each object of the class in memory includes a variable called the vptr, which points to the class's common vtbl. The _rst (which creates the vtable) is constructed by the base classes after the construction of the derived classes. The derived class constructor overwrites the entries of the vtable, if its constructor overrides any virtual function of the base class. Therefore, you need not call every function from a constructor just to avoid the error prone scenario of calling the Base class constructor instead of the derived class constructor (which fails to set the entries of objects of vtable).

98.

What Do You Understand By A Pure Virtual Member Function?

Answer»

A virtual function with no body structure is called a pure virtual MEMBER function. You can declare a pure virtual member function by ADDING the notation =0 to the virtual member function declaration. For example, AREA () is a virtual function, but when it is DECLARED as shown in the FOLLOWING expression, it becomes a pure virtual member function: virtual int area () =0;

To avoid a compilation error, all the classes need to implement pure virtual classes that are derived from the abstract class.

A virtual function with no body structure is called a pure virtual member function. You can declare a pure virtual member function by adding the notation =0 to the virtual member function declaration. For example, area () is a virtual function, but when it is declared as shown in the following expression, it becomes a pure virtual member function: virtual int area () =0;

To avoid a compilation error, all the classes need to implement pure virtual classes that are derived from the abstract class.

99.

Should The This Pointer Can Be Used In The Constructor?

Answer»
  • We can use the this pointer in the constructor in the initialization list and ALSO in the body. However, there is a feeling that the object is not fully formed so we should not use this pointer. Let's understand the use of the this pointer with the help of the following example. The declared data members of a base CLASS and/or the declared data members of the constructor's own class can be accessed by the constructor {body} and/or a function called from the constructor. This is possible because of the FULL construction of constructor's body at the time of execution. The preceding example always works.
  • The override in the derived class is not possible for a function called from the constructor and the {body} of a constructor which is independent of calling the virtual member function by the explicit use of the this pointer.
  • Please make sure that the initialization of other data member has already been done before passing the data member to another data member's initializer in this object. With the help of some straightforward language RULES (independent of the specific COMPILER that you are using), you can confirm about the determination of initialization of data members.
  • Without the prior knowledge of these rules, there is no use of passing any data member from the this object.

100.

What Do You Understand By Zombie Objects In C++?

Answer»

In a situation, where an object is created in a class, a constructor fails before its full execution. It is very difficult to ensure about the execution of the constructor whether the constructor WOULD return a value or not. OBJECTS that are no more REQUIRED for an APPLICATION are called zombie objects. These zombie objects occupy a space in the memory and wait for their TERMINATION.

In a situation, where an object is created in a class, a constructor fails before its full execution. It is very difficult to ensure about the execution of the constructor whether the constructor would return a value or not. Objects that are no more required for an application are called zombie objects. These zombie objects occupy a space in the memory and wait for their termination.