Some basics

July 25, 2006

Learned and refreshed many interesting things:

  • int a(2); can be used to initialize the variable a to 2, just like int a = 2;
  • A String is not a fundamental data type in C++, but a part of the standard C++ library. We have to include <string> to use this class.
  • This is how we create a string consisting of wide chars. string s = L”Hello”;
  • In C++ we can do typecasting using a functional notation, so that int i =(int)f; is the same as int i = int(f); I think this is possible because C++ contains functions like int, float, etc, that are overloaded with the appropriate types and return an int, float, respectively. Must verify this fact. Will this work with user defined types as well?
  • cout does not add a newline by default. We must add it explicitly by using “\n” or the constant endl. endl will also flush the stream, however since cout because it is not buffered, we can use them interchangeably.
  • In the following line of code int i; cin >> i; cin will process bytes from the input stream based on what we expect from the stream. In this case it will process 4 bytes and store the int value in the variable i. cin will not process the input stream unless we explicitly “RETURN”. The code cin >> a >> b; will process 2 values entered by the user. These values must be seperated by a space, tab, or a newline character.
  • cin does not verify the values entered. It also has problems with accepting strings with a space in them, since it will treat multiple words as seperate data values. To resolve these issues we use the function getline(cin, variableName); to accept a line of input from the user into a string. We then use the string as a stream and extract the data we need, using stringstream(str) >> variable;

Things that I am bit embarrased to include, but will include them anyways 🙂

  • Any character can be written with a \ followed by it’s ACII code. char c = \97;
  • #define does not need a ; at the end of the statement. It is optional.
  • int i += 3; The operator that would otherwise have been after the = and between the operators is now before the = sign. I wonder why.
  • A string consists of const char*.

Learning C++

July 25, 2006

Now that the basics (C programming) are covered, I will move on to C++.

The study plan is to read the basic online tutorial at and then follow up with Bruce Eckel’s “Thinking in C++”. I might complement it with ACM’s online C++ course.