C++ Programming tips &Technical Interview Questions Set 2

Posted by , No Comments

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

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.
#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.

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

Ans: Take a look at the following program.

#include
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.

Latest Jobs , Government Jobs, Walkings,

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:
#include

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.

Related Jobs & Posts

C++ Programming tips &Technical Interview Que... 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 f...
CSS Interview Questions & Answers Set –... What is CSS? 1. CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. Every element...
HTML – Interview Questions & Answers Se... How can I check for errors? HTML validators check HTML documents against a formal definition of HTML syntax and then output a list of errors. Valid...
HTML Interview Question Answer Set – 1 What is HTML? Answer1 : HTML, or HyperText Markup Language, is a Universal language which allows an individual using special code to create web pag...
C++ Programming tips & Technical Interview Qu... 41.What is forward referencing and when should it be used? Ans: Forward referencing is generally required when we make a class or a function as a fri...
C++ Programming tips &Technical Interview Que... 31.Can I have a reference as a data member of a class? If yes, then how do I initialise it? Ans: Yes, we can have a reference as a data member of a c...

Comments

comments

Enter your email address:

Delivered by Apply Now