C++ Programming tips &Technical Interview Questions Set 1

Subscribe to Freshers Jobs

Search 1000’s of local Job Postings ! Increase your chances to get your dream job.

Welcome to FreshersPlane, FreshersPlane Provide you latest Job Notifications , Career Opportunities, Government Jobs Information,

Don't forget to subscribe to email notifications to get all latest jobs directly to your email daily. We post more than 1000+ jobs every week.So,

cplus1.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 ;



Find the Latest Jobs in our site for freshers , experience .List of walkings , fresher jobs, latest jobs,Govt Jobs, Sarkari Naukri for all engineering branches, all graduates,
Apply for all jobs mentioned in our site.

}

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

comments

Updated: April 6, 2017 — 9:31 AM

Freshersplane is online web Platform working for graduates with aim is to to make the career searching task simpler and easier by providing them information about jobs which are relevant to them. Job Search, Latest Jobs, Government Jobs, Results

Powered by Indian Servers Competitive Exams Syllabus Colleges List TCS Next Step

Latest Fresher Recruitments 2014 , 2015, 2016, 2017

Latest Govt Jobs

List of Government Jobs

AIIMS Recruitment ICAR Recruitment NIT Recruitment
ANU Recruitment ICMR Recruitment ONGC Recruitment 2017
AP Police Jobs IIPM Recruitment OPELIP Jobs 2017
AP Transco Recruitment IIt Kharagpur Ordnance Factory Recruitment
Army jobs Government Jobs in Tamil Nadu Indian Oil Recruitment
BECIL Recruitment Indian Oil Corporation Limited recruitment Railway Recruitment Board
BEL Recruitment 2017 Government Jobs in Telangana Rajya Sabha Recruitment
BHEL Recruitment Indian Post Office Recruitment RGAVP Recruitment
 Balmer Lawrie ITPO Recruitment RNTCP Recruitment 2017
bpcl recruitment VMMC & Safdarjung Hospital Recruitment satara zp recruitment 2017
BPRD Recruitment KSRTC Recruitment SCTIMST Recruitment
Cement Corporation of India (CCI) Recruitment 2017 Madras Fertilizers Limited Recruitment Singareni Collieries Company Limited (SCCL) Recruitment 2017
coal india recruitment 2017 MDL Recruitment SSB Recruitment
Cochin Shipyard Recruitment Mizoram PSC Recruitment 2017 SSC Recruitment
CPCL Recruitment Government Jobs in AP Tata Institute of Social Sciences
CRIS Recruitment MSU Baroda Tata Memorial Centre Recruitment
Delhi University Recruitment UCMS TNAU Recruitment 2017
DRDO Recruitment NABARD Recruitment TNPSC Recruitment 2017
ECHS Recruiment Navy jobs TSCAB Recruitment 2017
fci recruitment NCRTC Recruitment TSPSC Recruitment
Goa Shipyard Recruitment New India Assurance Company Ltd. Recruitment 2017 AP Govt Jobs
GPSC Recruitment NHIDCL UIIC Recruitment
HAL Recruitment NHM UKPSC Recruitment 2017
LIC Housing Recruitment NIELIT Recruitment UPSC Recruitment
HRTC Recruitment NIRT Recruitment 2017 uttarakhand gov jobs
VSSC Recruitment Telangana Govt Jobs Vizag steel plant  Recruitment
Freshers Plane - 2017, 2016 and 2015 Freshers Job updates | Freshersplane Jobs | 2016 Freshers Job Updates | Freshersplane.com © 2017 Frontier Theme