Technical Interview Questions and Answers :: C++
21 / 187
Syntax:
void operator delete(void *p)
{
/*free memory pointed to by p. the destructor is called automatically.*/
}
The delete function receives a pointer to the region of memory to be freed. It then releases the previously allocated memory back to the system. When an object is deleted, its destructor is automatically called.
22 / 187
Syntax:
void *operator new (size_t size)
{
/*performs the allocation operation.
The constructor is called automatically.
returns the pointer to memory*/
}
size_t - It is a defined type capable of containing the largest single piece of memory that can be allocated.
Size - It contains the number of bytes needed to hold the object being allocated.
When we allocate an object using new, the object’s constructor is automatically called.
23 / 187
• It prevents us from having to provide an overloaded constructor that takes no parameters.
• It is convenient by providing the default common initial values than specifying them each time an object is declared.
24 / 187
• We can use default arguments to add new parameters to the existing functions.
• Default arguments can be used to combine similar functions into one.
25 / 187
Functions are said to be overloaded when they have the same name with the different number and the type of the arguments. Two functions differing only in their return types cannot be overloaded.
26 / 187
Two or more functions can share the same name that perform a variety of different tasks as long as their parameter declarations are different. The functions that share the same name are said to be overloaded, and the process is referred to as function overloading.
27 / 187
Class objects can be initialized dynamically too. That is, the initial value of an object may; be provided during run time.
Advantages:
We can provide various initialization formats, using overloaded constructor. This provides the flexibility of using different format of data at run time depending upon the situation.
28 / 187
Default constructor:
1. The constructor that accepts no parameter is called is a default constructor.
2.it is used in class by
A :: A( )
Default argument constructor:
1. The default argument constructor can be called with either one argument or no arguments.
2. It is used in class by
A:: A(int =0)
29 / 187
To pass values as arguments to the constructor function when an object is declared.
By calling the constructor implicitly
Ex:
Implicit call:
Integer int1 (0,100);
This method is called as shorthand method, is used very often as it is shorter, looks better and is easy to implement.
30 / 187
To pass values as arguments to the constructor function when an object is declared. This done in two ways.
1. By calling the constructor explicitly.
2. By calling the constructor implicitly.
Eg :
Explicit call:
Integer int1=integer(0,200);
Implicit call:
Integer int1(0,100);