PricewaterhouseCoopers Most Frequently Asked C++ Interview Questions Answers
Explain The Concept Of Friend Function In C++?
The friend function in C++ refers to a class or function that works with the private or protected members of the other class. It gets privileges to access and work with the private members of the other class. The programmer has full control over both the friend and member functions of the class and can use the features of both. The friend function does not require the use of an object and can be invoked without creating the object.
How The Programmer Of A Class Should Decide Whether To Declare Member Function Or A Friend Function?
A programmer should analyze the situations or requirements for deciding whether to declare the member function or friend function for a class. When a function is declared as a friend, the function is able to access all the private and protected member of the class. A member function can be used with encapsulation point of view as friend functions violate the encapsulation and can also access all the private members although they are not a part of the class. From the security point of view, use of member function is safer than the friend function.
Why Should We Use Null Or Zero In A Program?
In C++, <cstdio> header defines NULL, a global symbol that represents a null pointer. NULL has nothing to do with standard input/output except that some functions return a null pointer. C++ programmers do not use the NULL global symbol, preferring to address zero pointer values with the constant integer value 0.
Another problem is that ignoring a 0 return could crash the system when the program tries to assign values through a zero-value pointer.
Write Code That Allows To Create Only One Instance Of A Class?
This is shown in following code snippet.
#include
class sample
{
static sample *ptr ;
private
sample( )
{
}
public
static sample* create( )
{
if ( ptr == NULL )
ptr = new sample ;
return ptr ;
}
} ;
sample *sample::ptr = NULL ;
void main( )
{
sample *a = sample::create( ) ;
sample *b = sample::create( ) ;
}
Here, the class sample contains a static data member ptr, which is a pointer to the object of same class. The constructor is private which avoids us from creating objects outside the class. A static member function called create( ) is used to create an object of the class. In this function the condition is checked whether or not ptr is NULL, if it is then an object is created dynamically and its address collected in ptr is returned. If ptr is not NULL, then the same address is returned. Thus, in main( ) on execution of the first statement one object of sample gets created whereas on execution of second statement, b holds the address of the first object. Thus, whatever number of times you call create( ) function, only one object of sample class will be available.
Write Code To Add Functions, Which Would Work As Get And Put Properties Of A Class?
This is shown in following code.
#include
class sample
{
int data ;
public
__declspec ( property ( put = fun1, get = fun2 ) )
int x ;
void fun1 ( int i )
{
if ( i < 0 )
data = 0 ;
else
data = i ;
}
int fun2( )
{
return data ;
}
} ;
void main( )
{
sample a ;
a.x = -99 ;
cout << a.x ;
}
Here, the function fun1( ) of class sample is used to set the given integer value into data, whereasfun2( ) returns the current value of data. To set these functions as properties of a class we havegiven the statement as shown below
__declspec ( property ( put = fun1, get = fun2 )) int x ;
As a result, the statement a.x = -99 ; would cause fun1( ) to get called to set the value in data. On the other hand, the last statement would cause fun2( ) to get called to return the value of data.
Discuss The Possibilities Related To The Termination Of A Program Before Entering The Mainq Method?
The global variables are initialized dynamically before invoking the main() method. The process of invoking the global variables is slow. If the function is called by initialization of the global variables, then the program is terminated before entering the main() method.
What Is Meant By The Term Name Mangling In C++?
The mangled name includes tokens that identify the function's return type and the types of arguments. Calls to the function and the function definition itself are recorded in the relocatable object file as references to the mangled name, which is unique even though several functions might have the same similar name. Through name mangling, the name of the function is changed into coded and unique names or symbols which can be understand by the user. In this way, the function with the same name can be differentiated with the help of this coded language.
How A Macro Differs From A Template?
A macro defines the meaning for an identifier.The preprocessor replaces macro in the form of strings in the source code with values derived from the macro definition.The most common usage for macros defines a global symbol that represents a value. A macro cannot call itself.
On the contrary, a template allows you to create generic functions that admit any data type as parameters and return a value without having to overload the function with all the possible data types. A template can call itself.
Carry Out Conversion Of One Object Of User-defined Type To Another?
To perform conversion from one user-defined type to another we need to provide conversion function. Following program demonstrates how to provide such conversion function.
class circle
{
private
int radius ;
public
circle ( int r = 0 )
{
radius = r ;
}
} ;
class rectangle
{
private
int length, breadth ;
public
rectangle( int l, int b )
{
length = l ;
breadth = b ;
}
operator circle( )
{
return circle ( length ) ;
}
} ;
void main( )
{
rectangle r ( 20, 10 ) ;
circle c;
c = r ;
}
Here, when the statement c = r ; is executed the compiler searches for an overloaded assignment operator in the class circle which accepts the object of type rectangle. Since there is no such overloaded assignment operator, the conversion operator function that converts the rectangle object to the circle object is searched in the rectangle class. We have provided such a conversion function in the rectangle class. This conversion operator function returns a circle object. By default conversion operators have the name and return type same as the object type to which it converts to. Here the type of the object is circle and hence the name of the operator function as well as the return type is circle.
Explain The Concept Of Friend Function In C++?
The friend function in C++ refers to a class or function that works with the private or protected members of the other class. It gets privileges to access and work with the private members of the other class. The programmer has full control over both the friend and member functions of the class and can use the features of both. The friend function does not require the use of an object and can be invoked without creating the object.
How The Programmer Of A Class Should Decide Whether To Declare Member Function Or A Friend Function?
A programmer should analyze the situations or requirements for deciding whether to declare the member function or friend function for a class. When a function is declared as a friend, the function is able to access all the private and protected member of the class. A member function can be used with encapsulation point of view as friend functions violate the encapsulation and can also access all the private members although they are not a part of the class. From the security point of view, use of member function is safer than the friend function.
Why Should We Use Null Or Zero In A Program?
In C++, <cstdio> header defines NULL, a global symbol that represents a null pointer. NULL has nothing to do with standard input/output except that some functions return a null pointer. C++ programmers do not use the NULL global symbol, preferring to address zero pointer values with the constant integer value 0.
Another problem is that ignoring a 0 return could crash the system when the program tries to assign values through a zero-value pointer.
Write Code That Allows To Create Only One Instance Of A Class?
This is shown in following code snippet.
#include
class sample
{
static sample *ptr ;
private
sample( )
{
}
public
static sample* create( )
{
if ( ptr == NULL )
ptr = new sample ;
return ptr ;
}
} ;
sample *sample::ptr = NULL ;
void main( )
{
sample *a = sample::create( ) ;
sample *b = sample::create( ) ;
}
Here, the class sample contains a static data member ptr, which is a pointer to the object of same class. The constructor is private which avoids us from creating objects outside the class. A static member function called create( ) is used to create an object of the class. In this function the condition is checked whether or not ptr is NULL, if it is then an object is created dynamically and its address collected in ptr is returned. If ptr is not NULL, then the same address is returned. Thus, in main( ) on execution of the first statement one object of sample gets created whereas on execution of second statement, b holds the address of the first object. Thus, whatever number of times you call create( ) function, only one object of sample class will be available.
PricewaterhouseCoopers Most Frequently Asked C++ Interview Questions Answers |
Write Code To Add Functions, Which Would Work As Get And Put Properties Of A Class?
This is shown in following code.
#include
class sample
{
int data ;
public
__declspec ( property ( put = fun1, get = fun2 ) )
int x ;
void fun1 ( int i )
{
if ( i < 0 )
data = 0 ;
else
data = i ;
}
int fun2( )
{
return data ;
}
} ;
void main( )
{
sample a ;
a.x = -99 ;
cout << a.x ;
}
Here, the function fun1( ) of class sample is used to set the given integer value into data, whereasfun2( ) returns the current value of data. To set these functions as properties of a class we havegiven the statement as shown below
__declspec ( property ( put = fun1, get = fun2 )) int x ;
As a result, the statement a.x = -99 ; would cause fun1( ) to get called to set the value in data. On the other hand, the last statement would cause fun2( ) to get called to return the value of data.
The global variables are initialized dynamically before invoking the main() method. The process of invoking the global variables is slow. If the function is called by initialization of the global variables, then the program is terminated before entering the main() method.
What Is Meant By The Term Name Mangling In C++?
The mangled name includes tokens that identify the function's return type and the types of arguments. Calls to the function and the function definition itself are recorded in the relocatable object file as references to the mangled name, which is unique even though several functions might have the same similar name. Through name mangling, the name of the function is changed into coded and unique names or symbols which can be understand by the user. In this way, the function with the same name can be differentiated with the help of this coded language.
How A Macro Differs From A Template?
A macro defines the meaning for an identifier.The preprocessor replaces macro in the form of strings in the source code with values derived from the macro definition.The most common usage for macros defines a global symbol that represents a value. A macro cannot call itself.
On the contrary, a template allows you to create generic functions that admit any data type as parameters and return a value without having to overload the function with all the possible data types. A template can call itself.
Carry Out Conversion Of One Object Of User-defined Type To Another?
To perform conversion from one user-defined type to another we need to provide conversion function. Following program demonstrates how to provide such conversion function.
class circle
{
private
int radius ;
public
circle ( int r = 0 )
{
radius = r ;
}
} ;
class rectangle
{
private
int length, breadth ;
public
rectangle( int l, int b )
{
length = l ;
breadth = b ;
}
operator circle( )
{
return circle ( length ) ;
}
} ;
void main( )
{
rectangle r ( 20, 10 ) ;
circle c;
c = r ;
}
Here, when the statement c = r ; is executed the compiler searches for an overloaded assignment operator in the class circle which accepts the object of type rectangle. Since there is no such overloaded assignment operator, the conversion operator function that converts the rectangle object to the circle object is searched in the rectangle class. We have provided such a conversion function in the rectangle class. This conversion operator function returns a circle object. By default conversion operators have the name and return type same as the object type to which it converts to. Here the type of the object is circle and hence the name of the operator function as well as the return type is circle.
Post a Comment