Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++ interview questions 2

Status
Not open for further replies.

jjjkkk77

Programmer
Jun 14, 2007
10
0
0
US
hi folks, I need help on how to answer the following questions. Could someone kindly help. thanks.

Q1

struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
a.fun();
Test b();
b.fun();
}

The above program generates a compilation error
"error C2228: left of '.fun' must have class/struct/union " at "b.fun();". Why?

--------------
Q2

struct CLS
{
int m_i;
CLS( int i ) : m_i(i) {}
CLS() {CLS(0);}
};
CLS obj1(1);
cout << obj1.m_i << endl;
CLS obj2;
cout << obj2.m_i << endl;

The output of the above program is
1
-858993460
So why does "CLS obj2;" fail to initialize obj2.m_i as 0?
----------------
Q3

float a = 1.0;
cout << (int&)a << endl;

The output of the above program is
1065353216
Why?

My guess is that (int&)a is interpreting the bit pattern of the float number a as an integer. Am I correct?
----------------
Q4

vector<int> array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// delete all 2's
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor ) array.erase( itor );
}

The above program generates a run-time error after removal of first 2. Looks like "++itor" is causing the error. What went wrong?
 
Q1:
Code:
Test b();
This defines a function called 'b' which takes no parameters and returns Test.


Q2:
I'm not sure, but I believe this:
Code:
CLS(0);
will create a new local CLS object initialized to 0, instead of calling the CLS contructor. Try in the debugger to verify.


Q3:
Yes, the cast is truncating the float value.


Q4:
Code:
IntArray::iterator itor
What is IntArray?
I don't see the typedef for it listed.
 
thanks cpjust.

Q1:

Does "Test b();" give a complete definition of a function? If it does, why does the following fail to compile when I tried to call a similarly-defined "int f();"?

class Base
{
public: int f();
};

void main()
{
Base b;
b.f();
}

error LNK2019: unresolved external symbol "public: int __thiscall Base::f(void)" (?f@Base@@QAEHXZ) referenced in function _main t.obj



Q3:

You said "truncating the float value." which I am not sure about what you mean. A float and an int are both 4 bytes. What I guessed is that (int&)fMyNumber will try to read the 32 bits of fMyNumber as an integer. So where does truncating comes about here?

Q4:

Sorry my bad. Replace "IntArray" with "vector<int>"

Thanks a lot
 
In Q1, you define Test b(), but you don't call b(); If you did, you'd get an unresoved external symbol.

Q3: Oops, I thought floats were bigger than that, but either way, 1.0 in a float is stored in a completely different way than 1 in an int, so it's getting mis-interpreted...

In Q4, when you call the vector's erase() function, the first iterator is deleted. Then you call ++itor, but itor is not an invalid object, which causes it to blow up.
To do this correctly, (erase() returns the next iterator after the one you're deleting) you'd do something like this:
Code:
for ( vector<int>::iterator itor = array.begin(); itor != array.end(); ++itor )
{
   if ( 2 == *itor )
   {
      itor = array.erase( itor );
   }
}
 
cpjust's answers for #3 and #4 aren't quite correct.

I don't want to give them away, but look again at the cast in #3, it isn't casting to int. For #4, the reasoning is correct, but the latest code still isn't right.
 
Oops, you're right. The code I posted will skip over some iterators. Move the ++itor out of the for (...) and into an else to fix it.

For Q3, if the code was:
Code:
(int)a
it should work fine, but it's the & that's causing the problem.
 
Q1 - some addition:
C++ Standard said:
Note: since () is not permitted by the syntax for initializer,
X a();
is not the declation of an object of class X, but the declaration of a function taking no argument and returning an X.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top