Friday, February 15, 2008

How to implement a remove function within a stack similar to the STL implmentation:

iterator &remove(iterator& iter)
{
// Check if its good
if (!iter.good()) return iter;

// Get the node
Node* pDelete = iter.m_pCurr;

// Fix the stack's missing links
if (iter.m_pPrev != NULL)
iter.m_pPrev->pNext = iter.m_pCurr->pNext;
else
m_pTop = iter.m_pCurr->pNext;

if (iter.m_pPrev == NULL)
iter.m_pCurr = iter.m_pPrev;
else
iter.m_pCurr = iter.m_pPrev->pNext;

// Set the current to the next node
iter.m_pCurr = pDelete->pNext;

// Delete the data
delete pDelete;

// One less
--m_nListSize;

// Return the fixed iterator
return iter;
}

Tuesday, February 12, 2008

C++ Interview Questions!

What is operator overloading?
Operator overloading is writing a polymorphic function that matches one of the C++ built in operator function signatures.

What is polymorphism?
Polymorphism is a concept that allows functions with different signatures to share the same name.

Type-define a function pointer which takes an int and float as a parameter and returns a float *.
float*(*pfunc)(int, float)

Declare a void pointer.
void* ptr

What are templates?
Templates are classes in C++ that allow abstraction of data types contained within the class.

How do you do dynamic memory allocation in C appliations?
malloc() allocates memory for a void pointer, free() frees the memory allocated by malloc().

What are the advantages and disadvantages of dynamic memory allocation and static memory allocation?
Dynamic memory allocation can be allocated and freed during runtime while static memory allocation is allocated when the process starts and freed when the process ends.

How do you call a C module within a C++ module?
extern "C" {}

Why does C/C++ give better run-time performance than Java?
Java code must be ran through a java environment during runtime before it is translated into machine code while C/C++ code is compiled into assembly.

Class A, derived B, and derived C all have the function foo(). If C is casted to A and foo() is called, what happens?
A's version of the foo() function will be called.

Does C++ come with in-built threading support?
Visual C++ can with microsoft's process.h library, and GCC++ can through libraries like boost.

What errors are caught at compile time?
Syntax, math, build, preprocessor, and run-time errors.

What errors are caught at link time?
Linker errors.

What does public and private mean in C++?
Public and private are access qualifiers used in structs, unions, and classes.

What is the difference of the copy constructor and assignment operator?
A copy constructor is called implicitly or explicitly when a new object is being assigned to an already existing object. The assignment operator is called when an already existing object is being assigned to another already existing object.

Why are arrays of references not possible?
Arrays are pointers and pointers cant point to references.

What are callback functions?
Any function that can be represented as a function pointer.

What is the volatile keyword?
Volatile forces the CPU to fetch the variable from system memory to cache every time it is accessed.

Can we recurse an inline function?
Yes, but it is very likely that the function will not be inlined by the compiler.

Which is the only operator in C++ that can be overloaded but not inherited?
The assignment operator.