Tuesday, May 7, 2013

Notes for blackjack

To prepare the interviews, I have to delve into 'Crack the Code Interview'. To better understand the OO chapter, I decided to work on a 400-line project, blackjack. I found some good source code online and I'll take notes here.

1. enum
http://www.enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/

2. C++ operator overload
http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html

3. Polymorphism: Virtual member function, abstract function
http://www.cplusplus.com/doc/tutorial/polymorphism/

4. protected specifier
http://msdn.microsoft.com/en-us/library/e761de5s(v=vs.71).aspx

5. Can we pass a derived class if the function is expecting the base class as argument?

If you have a function void foo(base b) and pass my_derived to it, that derived is converted to base, loosing any information that was not in base. You can't recover that information in any way.

If you have void foo(base* b) and pass &my_derived to it, no conversion occurs. foo only thinks that it is working with a base. You still can't use members of derived though. foo must be written in such way that it works with base itself or its other children that don't have that member. It is possible, however, to check that b is in fact pointing to a derived object and cast it to derived* using dynamic_cast or some virtual function.


6. Slicing

If You have a base class A and a derived class B, then You can do the following.
void wantAnA(A myA)
{
   // work with myA
}

B derived;
// work with the object "derived"
wantAnA(derived);
Now the method wantAnA needs a copy of derived. However, the object derivedcannot be copied completely, as the class B could invent additional member variables which are not in its base class A.
Therefore, to call wantAnA, the compiler will "slice off" all additional members of the derived class. The result might be an object you did not want to create, because
  • it may be incomplete,
  • it behaves like an A-object (all special behaviour of the class B is lost).
7. const issues
When doing the coding, I noticed a scenario like this.

void aClass::foo const () {
      vector<T>::iterator iter; //This is wrong since it's within a const function
      vector<T>::const_iterator iter; //This works fine
}

7.1 The const for foo means that within the function, the value of the class members will not be changed. 
class aClass {
private:
     int a;
     int *b;
};

aClass::foo const {
     a = 5; //Wrong
    int x = a; //OK
    *b = 100; //OK, it's changing the variable pointed to by b, not b itself
    b = &x; //Wrong
}

To fully understand this, it's necessary to notice the difference between const pointer and pointer to const variables. Here is a good demo about this.

Back to the iterator, const_iterator acts like a pointer to const variable and iterator acts like a normal pointer. I didn't delve into the reason, just remember for a const function, if you need to use an iterator, const_iterator is always safer than iterator.