Thursday, December 4, 2008

Week 1, Day 1

Quick Primer
I am going to "do a class" on programming with C++. Why? Because it has been a childhood dream to learn to program, not just do scripting (like web stuff), but actual PROGRAMMING. So, WHY NOT?
I have just found out fairly recently that I can actually compile C++ code directly in Linux (for Windows you have to buy software that is usually in the range of 200 - 600 dollars to do this). That opens up the realistic possibility to actually PROGRAM. I wanted to learn JAVA, but I don't have a JAVA book. From what I have read, if you know C++ you know 90% of JAVA (and vice versa), so I might as well start with the foundation of all modern programming languages and move from there. Besides, C++ is faster than JAVA since it is not an interpreted language (a language that requires a program to translate the source code into machine code during runtime).
This is going to be a log of my progress and what I have learned.
I am learning from a book I have had for a long time, I have quickly found that there are a few things I need to "update" in order to get the code to work with the modern compilers. But I think this is doable for now. The book I am learning from is Sams Teach Yourself C++ in 21 Days (third edition) by Jesse Liberty. I figure what better way to sound down the information I learn than to "teach" it to others (or at least review it).

Today's "class"
Today was primarily an introduction to C++ and the way the book is structured. I read the introduction and the first lesson. Pretty easy lesson really, took me just under an hour to do the entire lesson, exercises and all. It only had two (basically) sample codes for me to compile and I had to update the samples to work in my inbuilt Linux compiler (g++ for those who are curious how I am doing it).

The overview of C++
So, the book starts out talking about the evolution of programming and how C++ got it's start. The skinny is that C++ is an object-oriented programming language. Object-oriented programming languages are structured on three principal "pillars" or I guess you could call them philosophies. (1) Encapsulation, the idea that code chunks should be usable. Book gave the illustration of a function, or rather a class (sorry, web lingo slip up), being like a resister in an electronic circuit. More common example to me would be that a class is like one of the pieces of bread, or a leaf of lettuce, or a slice of ham, etc on a sandwich. It is one piece of the puzzle. (2) Inheritance and Reuse, being able to use existing code in future programs. (3) Polymorphism, kind of a confusing way of saying that classes could be replaced or expanded upon. I was a bit confused by this part and had to read it slower one more time. To me, I think this is just a much larger (and more specific) application of the Encapsulation "pillar". The concept is that you can swap a class with a new class and it will do the job of it. Book built on a concept of a new model of car, the better engine does the job of the old engine... but either way, the user just "floors it" and the job is done. Polymorphism is the idea that the use just "floors it" and whatever class you have set up to respond will respond accordingly.

Preparing to Program and Your Development Environment
Pretty much skimmed these sections, since my setup is vastly different than this book. Not sure if this will come back to bite me... but I wanted to do this as cheaply and with the modern tools I have at hand. After all, I will NOT be spending a bunch of money on THIS particular hobby. So, if I can't do it with g++, make, or whatever other command line compiler I have access to I will probably not go very far. After skimming this, I looked up how to actually compile C++ in the linux command line, that is where I found out about g++. This quick tutorial on LinuxQuestions helped TREMENDOUSLY with this first lesson: http://www.linuxquestions.org/linux/answers/Programming/Building_C_programs_on_Linux_0, taught me a fair amount too.
I am using gedit to create my source code and g++ to compile (and link them I guess... saves me a step from the book!).

The Development Cycle
This lesson was a good learning experience. Back in High School... the development cycle was the biggest obstacle to me actually persuing a career in programming... all that debugging left me cold. I have since learned patience in my 6-ish years post-graduation. That, and I have debugged web scripting languages like PHP, PERL, JavaScript, HTML, CSS, etc for almost a decade now and am getting used to the cycle. I did find it interesting that you can get errors in three places with C++: compile errors, link errors, runtime errors.
hello.cpp
The book had this code as the first example program:
#include <iostream.h>

int main()
{
cout << "Hello World!\n";
return 0;
}


However, this had compile errors for me under g++, so that tutorial taught me to drop the ".h" after iostream and to include "using namespace std;" in between the hash-include and the "main" class. So, the functioning code looked like this for me:
#include <iostream>

using namespace std;

int main()
{
cout << "Hello World!\n";
return 0;
}


Ironically, on the next page it had a box about these very changes! Had I not seen the tutorial, I probably would have been fine if I read ahead a bit (probably would have stayed stumped on the failed code for a bit though). The box in the book says: "To ensure that readers who are using older compilers will not have trouble with the code in this book, we are using the older style include files..."
Fun. At least this will give me plenty of debugging practice in the weeks ahead.

Visual C++ 6 and Compile Errors
Skimmed these sections. It is slightly different for me, and I basically learned what I needed to know during the forum tutorial and my own experimenting.

Summary and Workshop
End of the day, and the summary wrapped things up tidy. Workshop quiz and exercises were simple enough. Answered the questions quickly and from memory (good start). The exercises posed no difficulty and breezed through them. One of the exercises showed some simple math and the other exercise had me debug some source code where they forgot the hash before the "include...".

No comments: