Saturday, June 06, 2009

Some test questions - that I still can remember

1. Single inheritance - link

2. Static member function - link

The differences between a static member function and non-static member functions are as follows.
  • A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.
  • A static member function can be called, even when a class is not instantiated, a non-static member function can be called only after instantiating the class as an object.
  • A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
  • A static member function cannot have access to the 'this' pointer of the class.
3. std::auto_ptr - link
A smart pointer that deletes the instance it points to when going out of scope. It is supplied in the STL, in the header file memory.h.

4. Exception in destructor? - link
[17.3] How can I handle a destructor that fails?

Write a message to a log-file. Or call Aunt Tilda. But do not throw an exception!

5. Copy constructor is called when? - link

  • Automatically called: A copy constructor is called in the following circumstances.
        A x(y);  // Where y is of type A.
    f(x); // A copy constructor is called for value parameters.
    x = g(); // A copy constructor is called for value returns.

6. Map - local example

How to access the string?

for(mapType::const_iterator it = data.begin(); it != data.end(); ++it)
{
cout << "Who(key = first): " <<>first;
cout << " Score(value = second): " <<>second <<>

Not theMap[it] or theMap[i]

7. Prefix and postfix operator++() - link

 class Date {
//...
public:
Date& operator++(); //prefix
Date& operator--(); //prefix
Date& operator++(int unused); //postfix
Date& operator--(int unused); //postfix
};
Postfix operators are declared with a dummy int argument (which is ignored) in order to distinguish them from the prefix operators, which take no arguments:

8. const member function - link

int  capacity() const;  // max size before reallocation

Member functions should be declared with the const keyword after them if they can operate on a const (this) object. If the function is not declared const, in can not be applied to a const object, and the compiler will give an error message. A const function can be applied to a non-const object

A function can only be declared const if it doesn't modify any of its fields.

** Note

mutable

This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.

9. std:find(x, y, i) - link
    #include 
iterator find( iterator start, iterator end, const TYPE& val );

The find() algorithm looks for an element matching val between start and end. If an element matching val is found, the return value is an iterator that points to that element. Otherwise, the return value is an iterator that points to end.

10. command to start bash shell
= bash or sh
(bsh is wrong!)

11. Access - link
We can summarize the different access types according to who can access them in the following way:

Accesspublicprotectedprivate
members of the same classyesyesyes
members of derived classesyesyesno
not membersyesnono

friend yes yes yes

12. Pointer vs reference - link

No comments: