NetEase Frequently Asked C++ Interview Questions

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

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.

Explain The Term Memory Alignment?

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.

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

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.

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

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

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

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.

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

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.

What Is Meant By Forward Referencing And When Should It Be Used?

Forward referencing is generally required when we make a class or a function as a friend.Consider following program

class test
{
    public
        friend void fun ( sample, test ) ;
} ;

class sample
{
    public
        friend void fun ( sample, test ) ;
} ;

void fun ( sample s, test t )
{
    // code
}

void main( )
{
    sample s ;
    test t ;
    fun ( s, t ) ;
}
On compiling this program it gives error on the following statement of test class. It gives an error that sample is undeclared identifier. friend void fun ( sample, test ) ; This is so because the class sample is defined below the class test and we are using it before its definition. To overcome this error we need to give forward reference of the class sample before the definition of class test. The following statement is the forward reference of class sample.

class sample;

Write My Own Zero-argument Manipulator That Should Work Same As Hex?

This is shown in following program.

#include
ostream& myhex ( ostream &o )
{
  o.setf ( ios::hex) ;
  return o ;
}
void main( )
{
  cout << endl << myhex << 2000 ;
}

Post a Comment

Previous Post Next Post