C++ Programming tips &Technical Interview Questions Set 2

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,

cplus11.How do I write code to add functions, which would work as get and put properties of a class?

Ans: This is shown in following code.

class sample
int data ;
__declspec ( property ( put = fun1, get = fun2 ) )
int x ;
void fun1 ( int i )
if ( i < 0 )
data = 0 ;
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.

12.How do I write code to make an object work like a 2-D array?

Ans: Take a look at the following program.

class emp
public :
int a[3][3] ;
emp( )
int c = 1 ;
for ( int i = 0 ; i <= 2 ; i++ )
for ( int j = 0 ; j <= 2 ; j++ )
a[i][j] = c ;
c++ ;

int* operator[] ( int i )
return a[i] ;
} ;

void main( )
emp e ;
cout << e[0][1] ;
The class emp has an overloaded operator [ ] function. It takes one argument an integer representing an array index and returns an int pointer. The statement cout << e[0][1] ; would get converted into a call to the overloaded [ ] function as e.operator[ ] ( 0 ). 0 would get collected in i. The function would return a[i] that represents the base address of the zeroeth row. Next the statement would get expanded as base address of zeroeth row[1] that can be further expanded as *( base address + 1 ). This gives us a value in zeroth row and first column.

13.What are formatting flags in ios class?

Ans: The ios class contains formatting flags that help users to format the stream data. Formatting flags are a set of enum definitions. There are two types of formatting flags:On/Off flagsFlags that work in-group The On/Off flags are turned on using the setf( ) function and are turned off using the unsetf( ) function. To set the On/Off flags, the one argument setf( ) function is used. The flags working in groups are set through the two-argument setf( ) function. For example, to left justify a string we can set the flag as,cout.setf ( ios::left ) ;cout << “KICIT Nagpur” ;To remove the left justification for subsequent output we can say,cout.unsetf ( ios::left ) ;The flags that can be set/unset include skipws, showbase, showpoint, uppercase, showpos, unitbufand stdio. The flags that work in a group can have only one of these flags set at a time.

14.What is the purpose of ios::basefield in the following statement?
cout.setf ( ios::hex, ios::basefield ) ;

Ans: This is an example of formatting flags that work in a group. There is a flag for each numbering system (base) like decimal, octal and hexadecimal. Collectively, these flags are referred to as basefield and are specified by ios::basefield flag. We can have only one of these flags on at a time. If we set the hex flag as setf ( ios::hex ) then we will set the hex bit but we won’t clear the dec bit resulting in undefined behavior. The solution is to call setf( ) as setf ( ios::hex, ios::basefield ). This call first clears all the bits and then sets the hex bit.

15.Can we get the value of ios format flags?

Ans: Yes! The ios::flags( ) member function gives the value format flags. This function takes no arguments and returns a long ( typedefed to fmtflags) that contains the current format flags.

16.Is there any function that can skip certain number of characters present in the input stream?

Ans: Yes! This can be done using cin::ignore( ) function. The prototype of this function is as shown below:
istream& ignore ( int n = 1, int d =EOF ) ;
Sometimes it happens that some extra characters are left in the input stream while taking the input such as, the ‘n’ (Enter) character. This extra character is then passed to the next input and may pose problem.

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.

To get rid of such extra characters the cin::ignore( ) function is used. This is equivalent to fflush ( stdin ) used in C language. This function ignores the first n characters (if present) in the input stream, stops if delimiter d is encountered.

17.Write a program that implements a date class containing day, month and year as data members. Implement assignment operator and copy constructor in this class.

Ans: This is shown in following program:

class date
private :
int day ;
int month ;
int year ;
public :
date ( int d = 0, int m = 0, int y = 0 )
day = d ;
month = m ;
year = y ;

// copy constructor
date ( date &d )
day = d.day ;
month = d.month ;
year = d.year ;

// an overloaded assignment operator
date operator = ( date d )
day = d.day ;
month = d.month ;
year = d.year ;
return d ;
void display( )
cout << day << “/” << month << “/” << year ;
} ;

void main( )
date d1 ( 25, 9, 1979 ) ;
date d2 = d1 ;
date d3 ;
d3 = d2 ;
d3.display( ) ;

18.When should I use unitbuf flag?

Ans: The unit buffering (unitbuf) flag should be turned on when we want to ensure that each character is output as soon as it is inserted into an output stream. The same can be done using unbuffered output but unit buffering provides a better performance than the unbuffered output.

19.What are manipulators?

Ans: Manipulators are the instructions to the output stream to modify the output in various ways. The manipulators provide a clean and easy way for formatted output in comparison to the formatting flags of the ios class. When manipulators are used, the formatting instructions are inserted directly into the stream. Manipulators are of two types, those that take an argument and those that don’t.

20.What is the difference between the manipulator and setf( ) function?

Ans: The difference between the manipulator and setf( ) function are as follows:
The setf( ) function is used to set the flags of the ios but manipulators directly insert the formatting instructions into the stream. We can create user-defined manipulators but setf( ) function uses data members of ios class only. The flags put on through the setf( ) function can be put off through unsetf( ) function. Such flexibility is not available with manipulators.



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