(Paper) C++ Programming tips &Technical Interview Questions Source code examples Set-1

Paper : C++ Programming tips &Technical Interview Questions Source code examples Set-1

1.Can we declare a static function as virtual?

Ans: No. The virtual function mechanism is used on the specific object that determines which virtual function to call. Since the static functions are not any way related to objects, they cannot be declared as virtual.
-------------------------------------------------------------------------------------------------
2.Can user-defined object be declared as static data member of another class?

Ans: Yes. The following code shows how to initialize a user-defined object.

     #include

     class test
     {
     int i ;  
     public :
     test ( int ii = 0 )
     {
     i = ii ;  
     }
     } ;

     class sample
     {
     static test s ;
     } ;
     test sample::s ( 26 ) ;

     Here we have initialized the object s by calling the one-argument constructor. We can use the same convention to initialize the object by calling multiple-argument constructor.

-------------------------------------------------------------------------------------------------
3.What is forward referencing and when should it be used?

Ans: Consider the 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 ) ;
                       }
This program would not compile. It gives an error that sample is undeclared identifier in the statement friend void fun ( sample, test ) ; of the class 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. Forward referencing is generally required when we make a class or a function as a friend.
-------------------------------------------------------------------------------------------------
4.The istream_withassign class has been derived from the istream class and overloaded assignment operator has been added to it. The _withassign classes are much like their base classes except that they include overloaded assignment operators. Using these operators the objects of the _withassign classes can be copied. The istream, ostream, and iostream classes are made uncopyable by making their overloaded copy constructor and assignment operators private.
-------------------------------------------------------------------------------------------------
5.How do I write my own zero-argument manipulator that should work same as hex?

Ans: This is shown in following program.

#include

ostream& myhex ( ostream &o )
{

o.setf ( ios::hex) ;
return o ;

}

void main( )
{

cout << endl << myhex << 2000 ;

}
-------------------------------------------------------------------------------------------------

6.We all know that a const variable needs to be initialized at the time of declaration. Then how come the program given below runs properly even when we have not initialized p?

     #include
     void main( )
     {
         const char *p ;
         p = "A const pointer" ;
         cout << p ;
     }
     
Ans: The output of the above program is 'A const pointer'. This is because in this program p is declared as 'const char*' which means that value stored at p will be constant and not p and so  the program works properly

-------------------------------------------------------------------------------------------------

7.How do I refer to a name of class or function that is defined within a namespace?

Ans: There are two ways in which we can refer to a name of class or function that is defined within a namespace: Using scope resolution operator through the using keyword. This is shown in following example:
                             namespace name1
                             {
                             class sample1
                             {
                             // code
                             } ;
                             }
                            namespace name2
                             {
                             class sample2
                             {
                             // code
                             } ;
                             }
                             using namespace name2 ;
                             void main( )
                             {
                             name1::sample1 s1 ;
                             sample2 s2 ;
                             }
Here, class sample1 is referred using the scope resolution operator. On the other hand we can directly refer to class sample2 because of the statement using namespace name2 ; the using keyword declares all the names in the namespace to be in the current scope. So we can use the names without any qualifiers.
-------------------------------------------------------------------------------------------------

8.While overloading a binary operator can we provide default values?

Ans: No!. This is because even if we provide the default arguments to the parameters of the overloaded operator function we would end up using the binary operator incorrectly. This is explained in the following example:
                             sample operator + ( sample a, sample b = sample (2, 3.5f ) )
                             {
                             }

                             void main( )
                             {
                             sample s1, s2, s3 ;
                             s3 = s1 + ; // error
                             }

-------------------------------------------------------------------------------------------------
9.How do I carry out conversion of one object of user-defined type to another?

Ans: 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.
-------------------------------------------------------------------------------------------------
10.How do I write code that allows to create only one instance of a class?

Ans: 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.



comments powered by Disqus