Intuit Latest C++ Interview Questions Answers

Intuit Most Frequently Asked Latest C++ Interview Questions Answers

Can I Have A Reference As A Data Member Of A Class? If Yes, Then How Do I Initialise It?

Yes, we can have a reference as a data member of a class. A reference as a data member of a class is initialised in the initialisation list of the constructor. This is shown in following program.

#include
class sample
{
   private
     int& i ;
     public
       sample ( int& ii ) : i ( ii )
       {
       }
       void show( )
       {
         cout << i << endl ;
       }
} ;

void main( )
{
  int j = 10 ;
  sample s ( j ) ;
  s.show( ) ;
}

Here, i refers to a variable j allocated on the stack. A point to note here is that we cannot bind a reference to an object passed to the constructor as a value. If we do so, then the reference i would refer to the function parameter (i.e. parameter ii in the constructor), which would disappear as soon as the function returns, thereby creating a situation of dangling reference.

Why Does The Following Code Fail?

#include<iostream.>
#include<string.h>
Class Sample
{
Private :char *str ;
Public : Sample ( Char *s )
{
Strcpy ( Str, S ) ;
}
~sample( )
{
Delete Str ;
}
} ;
Void Main( )
{
Sample S1 ( "abc" ) ;
}

Here, through the destructor we are trying to deal locate memory, which has been allocated statically. To remove an exception, add following statement to the constructor.

sample ( char *s )
{
   str = new char[strlen(s) + 1] ;
   strcpy ( str, s ) ;
}
Here, first we have allocated memory of required size, which then would get deal located through the destructor.

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

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.
Intuit Most Frequently Asked Latest C++ Interview Questions Answers
Intuit Most Frequently Asked Latest C++ Interview Questions Answers

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

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.

Describe Inheritance And Non-inheritance Of A Derived Class?

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.

Write A Note About The Virtual Member Function?

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.

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

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.

Can Circle Be Called An Ellipse?

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.

Write About All The Implicit Member Functions Of A Class?

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.

What Is Overriding?

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.

Write About The Members That A Derived Class Can Add?

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).

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

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.

What Is An Object?

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

Define A Good Interface?

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.

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

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).

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

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.

Can Non-public Members Of Another Instance Of The Class Be Retrieved By The 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.

Can Encapsulation 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.

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

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.

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

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.

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

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.

How A New Operator Differs From The Operator New?

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.

Post a Comment

Previous Post Next Post