So last night was my 1st night of C++ class, and I want to try and pass on what I learn in a blog series. I'm a total newb as far as C++ goes, but if you want to learn as I go this may help! The majority of our first class was just intro, talking about computer basics, and only in the last 45 minutes or so did we really get into coding, and we did a simple 'display some output' app.
One thing that was rather interesting was an illustrated example of converting a character value into binary, so I'm going to try and share that with you here. The example we used was an easy one, the capital letter 'A', which has a ASCII dec value of 65. Binary is base-2, so let me see how this little example translates to web-world. Each position of the 8-bit (or 1 byte) value can hold 1 or 0, from right to left its 2^0 (1), 2^1 (2), 2^2 (4), 2^3 (8), and so on up to 2^7 (128). To find the binary value for the letter A, which in ASCII is 65, we start on the left and say: can 128 go into 65? No, so put a 0 under 128. Can 64 go into 65? Yes, with a remainder of 1, so put a 1 under 64. Now for subsequent checks, we check against the remainder, so can 32 go into 1? no, 16? no, 8? no, all the way to 1, which CAN go into 1, so we put a 1 there, which gives us our binary value for A or 01000001 (woohoo!) Of course this is the simplest example, and as you ramp up from 8 bit systems or memory spaces you can store much larger values, but you get the idea =)
--------------------------------------
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
--------------------------------------
0 1 0 0 0 0 0 1
So on to making a C++ program. First off, this class is being taught with the MS Visual Studio 2005 IDE. I asked about using Eclipse or Bloodshed Dev, but since portions of this class will be using .NET classes, that was not possible. You can download Visual Studio 2008 C++ Express Edition for free directly from Microsoft. Download, install, then come back here and read on =)
Everything in Vis Studio is organized as a project, which is simply a folder with all associated files somewhere on your drive. Create a new project (note where it is going). You will get a screen that looks something like the following, the gist is: choose C++ on the left, then choose console application on the right.

Once you are in the project, you will end up at a source-code view that has some form of a 'main' method inside of it. the Main method is what gets called when your program starts up. Now, we didnt get into much of the meaning of the code here, we simply were going for 'how do I get something to happen here', so for now you can just paste this code into your application, and press <ctl> F5 to build and run without debugging enabled.
#include <span class='cc_value'>"stdafx.h"</span>
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char dateStr [9];
char timeStr [9];
_strdate( dateStr);
printf( <span class='cc_value'>"Goodbye cruel world! The date is %s \n"</span>, dateStr);
return 0;
}
So hopefully you got a black console box that opened up and said 'Goodbye cruel world! The date is' followed by your system date. If so, jump up and down. You just compiled your first C++ application. I did some googling to find other functions to play with, and I'm sure you can do the same. I hope to put up 1 entry per week in this series, and since I have C++ II the semester after this, I hope to take you from complete newb to something resembling a C++ programmer!








#1 by Sean Corfield - September 5, 2008 at 12:07 AM