Tuesday, June 25, 2013

private vs protected vs publis

I'm always confused by the member type in class of c++. There are three types: private, protected and public. In inheritance, there are also there specifiers, private, protected and public. After doing some research on Internet, found two good tutorials for this.

http://www.parashift.com/c++-faq-lite/access-rules.html

http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/

Have fun!

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.






Saturday, April 27, 2013

'\r' vs '\n'


I've been confused by the difference between \r and \n for a long time. Now I found a really wonderful answer from Stackoverflow!

In terms of ascii code, it's 3 -- since they're 10 and 13 respectively;-).
But seriously, there are many:
  • in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special
  • as a consequence, in C and most languages that somehow copy it (even remotely), \n is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)
  • in old Mac systems (pre-OS X), \r was the code for end-of-line instead
  • in Windows (and many old OSs), the code for end of line is 2 characters, `\r\n', in this order
  • as a (surprising;-) consequence (harking back to OSs much older than Windows),\r\n is the standard line-termination for text formats on the Internet
  • for electromechanical teletype-like "terminals", \r commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), \n commands the roller to roll up one line (a much faster operation) -- that's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards!-)
  • for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller;-)
In practice, in the modern context of writing to a text file, you should always use \n(the underlying runtime will translate that if you're on a weird OS, e.g., Windows;-). The only reason to use \r is if you're writing to a character terminal (or more likely a "console window" emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy "ascii animation" effects of e.g. progress bars) -- this is getting pretty obsolete in a world of GUIs, though;-).


PS: when I coded in Xinu, things work like this.
/***Without '\r'*****************/
for (int i = 0; i < 3; i++)
     kprintf ("Hello World\n");
/*******************************/
Then the output is:
Hello World
     Hello World
          Hello World

/*******With '\r'******************/
for (int i = 0; i < 3; i++)
     kprintf ("Hello World\r\n");
/*********************************/

Output is:
Hello World
Hello World
Hello World

So I guess we can understand like this. '\n' moves the cursor to the next line. '\r' moves cursor to the leftmost of the current line.

Wednesday, March 20, 2013

strace in Linux

I ran into a interesting blog about strace.

http://timetobleed.com/hello-world/

time command in Linux

When working on cs252 lab, I was confused by the meaning of the output of time command. By searching the web, something useful was found.

http://www.dirac.org/linux/time/

To summarize,

Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processed and time the process spends blocked (for example if it is waiting for I/O to complete).

User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CUP time used by the process.

The equation is
user + Sys will tell you how much actual CPU time your process used.

Saturday, March 2, 2013

Tutorial for regular expression

Find a very good tutorial for regular expression!

http://www.regular-expressions.info/quickstart.html

This is a quick start for regular expression. Within this article, there is also a detailed version.