Haven't been in a programming mood as of late... but I pushed myself to do this one anyway after taking a few days off.
Today's "Class"
References. A reference are just a pseudonym of a variable you already have declared. They cannot be null... EVER. Or rather, they SHOULD not ever be null or referencing a variable that does not exist. References are fantastic, enabling you to use many of the useful features of pointers, WITHOUT the extra syntax and the extra difficulty of doing so. Here is a simple definition of a reference:
...
int myVariable = 5;
int & rMyVariable = myVariable;
...
Now, whether you use the variable name "myVariable" or "rMyVariable", the same piece of data is utilized. That might not seem like a very useful thing to do, and in that little snippet... it wouldn't be. The power behind references is the ability to pass the entire variable into a function and let the function actually edit the variable. Here is a sample program that does just this:
// Example of how to use references in a function
#include <iostream>
using namespace std;
void swapValues( int & x, int & y );
int main()
{
int x = 15, y = 33;
cout << "x: " <<
swapValues( x, y );
cout << "x: " <<>
return 0;
}
void swapValues( int & rX, int & rY )
{
int temp;
cout << "swapping...\n";
temp = rX;
rX = rY;
rY = temp;
}
If you do compile that guy and run it, you should see exactly what you THINK you should see. The restrictions for references are as follows: (1) you MUST initialize the reference (since you cannot have a reference that is null) and (2) you CANNOT reassign a reference. References should be used in place of pointers when at al possible. You can obviously reference ANY type of variable or any object, just like with pointers.
References also have the added bonus of reducing memory usage and speeds up the program slightly. This doesn't matter too very much when you are just passing in some number, but using user defined objects with references is a very good idea.
Summary and Workshop
Summary was quick and to the point. Reviewed the essentials of references. The exercises were rather tedious to me. I actually skipped a few since I just wasn't in the mood and I knew the topic pretty well from the chapter. I may finish up the exercises another day, not sure yet. Either way, the exercises were very easy and didn't really make me think any.
Sunday, December 21, 2008
Sunday, December 14, 2008
Week 2, Day 1
All about the Pointers.
Today's "Class"
Today featured a lengthy introduction to pointers and an explanation of how they actually function. Apparently pointers are one of the more difficult concepts to grasp for new C++ programmers. I can imagine. It isn't that the concept is confusing in theory, it is the practice that is a bit bothersome for me. To a degree, I understand WHY pointers are valuable. Being able to store variables on the "free store" of the RAM, safely tucked awa... int * pMyVariable = 0...y for use by any functions, is like creating SUPER GLOBAL variables. And yet you still get to control how much access is given to these variables by carefully using objects and the const keywoard to protect writing to the variable (and / or the pointer).
What is difficult to grasp is exactly WHY you would need to use pointers. The book says by the end of the "week" the reader will have a good grasp of what to use pointers for and whatnot. I guess I'll have to take it on faith then.
pointers - What are they?
pointers hold an address of a variable. Every variable in C++ is stored on the RAM. Well, this should be true of any programming language really. Anyway, so in C++ when you have variables on the RAM you can actually retrieve it's address in a fairly straightforward manner. Example code snippet to see an address of a variable:
...
int myVariable = 5;
cout << "myVariable is located at: " << &myVariable;
...
This little bit of code (when placed in a properly set up program), will print the address of the variable you declared the line above. It will not print 5. Instead it will print a hexidecimal (I think) code of WHERE the value for myVariable is PHYSICALLY stored on your RAM. Run this program a dozen times and you could get a dozen different values. Or they could all be the same, it just depends on your OS and your physical hardware.
So, like I said... a pointer STORES the address of a variable. Which means I have not shown you a pointer yet. You define a pointer in a very similar fashion to defining a variable. Let's redo the code snippet above to actually use a pointer shall we?
...
int myVariable = 5;
int * pMyVariable = &myVariable;
cout << "myVariable is located at: " << pMyVariable;
...
As you can see, it is pretty easy, like anything else in C++ once you understand the concepts. Now, there are three VERY important things to pay attention to when declaring a pointer.
(1) a pointer needs to be defined as the exact same type of variable as what it is pointing at. We were pointing to an int, so the pointer had to be an int as well.
(2) don't forget the asterisk. That is what is letting the compiler know this is a pointer.
(3) either point the pointer to an address of a variable, define a new variable for the pointer, or POINT IT AT NULL (... int * pMyVariable = 0; ...). If you have the pointer "dangling" or "wild" or "stray" it could cause some rather unpleasant bugs in your program that will be a nightmare to find in the future.
What's the Point
To understand why you need to be able to use pointers in the first place, you need to understand the basic structure of how computers store information in the RAM. Go get a book, as that is a topic and a half. You don't need to know the mechanical function of RAM or the technical specs, but you do need to know how the RAM houses information in different "areas" and what happens to each area in C++ while the program functions. I have said it before, and I will say it again... if you want to actually learn C++, go beyond just reading me review what I have learned... buy a book or enroll in a class. The benefits of being taught correctly the first time through is muy bien.
Pointers can access the data too!
So, pointers also can access the data of the variable they point to. It could be slightly confusing I guess, but with practice it doesn't seem to bother me anymore. The example we wrote before... let's change the variable data and see how it works eh?
...
int myVariable = 5;
int * pMyVariable = &myVariable;
cout << "myVariable, located at " << pMyVariable
*pMyVariable = 10;
cout << "myVariable, located at " << pMyVariable
...
Look confusing at all? The asterisk has dual function. When you are defining the pointer, it informs the compiler that this variable contains an address. When you are using it any other time in your code, it says that you are accessing the VALUE stored at the address the pointer points to.
So, in the above example:
pMyVariable would equal some hexidecimal address
*pMyVariable starts out as 5, and then we change it to 10. *pMyVariable is identical to myVariable in this example. When one changes, the other changes as well. They are just different labels for the same data.
Practical Uses of pointers
I don't have a full grasp on what the usages are yet, that will come tomorrow and in the following days, but I can give an overview.
pointers store data on the "free store" part of the RAM, which means after the function is over, the data is still there (unless you explicitely delete it). Of course, you have to make sure you can still ACCESS that data... otherwise you have a memory leak. The data will still reside in the address the pointer points to, BUT the pointer variable itself is stored in the "stack" of the function, so after it is done it will be GONE, unless you have the pointer somewhere other than the function and you are passing it into the function in the first place.
The one example the book gave (and the author didn't have me make this so I am not sure how exactly it works yet), is a word processor. A dialog box needs to have access to the document, but once the dialog box finishes it's task... we don't want the document disappearing. Makes sense... but why not just use an object for the document? I don't know, I'm sure there are reasons.
pointers can also CREATE a variable. The example we listed before, here is yet another version of it:
...
int * pMyVariable = new int (5);
cout << "pMyVariable points to: " << pMyVariable
cout << "*pMyVariable is: " << *pMyVariable << "\n";
...
This should demonstrate what I am talking about. You can of course use pointers for objects as well. It would be the same as before, but instead of type int, you would use your class name. To access functions from the pointer for that object... you would do something like this:
...
fish * pGoldfish = new fish;
cout << "pGoldfish is a fish with the default weight of " << pGoldfish->GetWeight() << " lbs.\n";
pGoldfish->SetWeight(10);
cout << "pGoldfish is now 10 lbs, see: " << pGoldfish->GetWeight() << " lbs.\n";
...
Obviously, you would need to have a class named "fish" with the functions "GetWeight()" and "SetWeight( int weight )". And having a default weight in the constructor would be a very good thing if you are going to copy and paste that little snippet above. The "->" accesses functions of an object through a pointer. Notice that you don't need an asterisk to get into the functions.
Summary and Workshop
That is just about all they covered. The end of the chapter demonstrated a program that seperates a string into words. The program obviously used pointers to do a lot of brunt work and showed how flexible they are when traversing functions. The exercises were very, very easy. Just making sure you understood how to use pointers really.
Today's "Class"
Today featured a lengthy introduction to pointers and an explanation of how they actually function. Apparently pointers are one of the more difficult concepts to grasp for new C++ programmers. I can imagine. It isn't that the concept is confusing in theory, it is the practice that is a bit bothersome for me. To a degree, I understand WHY pointers are valuable. Being able to store variables on the "free store" of the RAM, safely tucked awa... int * pMyVariable = 0...y for use by any functions, is like creating SUPER GLOBAL variables. And yet you still get to control how much access is given to these variables by carefully using objects and the const keywoard to protect writing to the variable (and / or the pointer).
What is difficult to grasp is exactly WHY you would need to use pointers. The book says by the end of the "week" the reader will have a good grasp of what to use pointers for and whatnot. I guess I'll have to take it on faith then.
pointers - What are they?
pointers hold an address of a variable. Every variable in C++ is stored on the RAM. Well, this should be true of any programming language really. Anyway, so in C++ when you have variables on the RAM you can actually retrieve it's address in a fairly straightforward manner. Example code snippet to see an address of a variable:
...
int myVariable = 5;
cout << "myVariable is located at: " << &myVariable;
...
This little bit of code (when placed in a properly set up program), will print the address of the variable you declared the line above. It will not print 5. Instead it will print a hexidecimal (I think) code of WHERE the value for myVariable is PHYSICALLY stored on your RAM. Run this program a dozen times and you could get a dozen different values. Or they could all be the same, it just depends on your OS and your physical hardware.
So, like I said... a pointer STORES the address of a variable. Which means I have not shown you a pointer yet. You define a pointer in a very similar fashion to defining a variable. Let's redo the code snippet above to actually use a pointer shall we?
...
int myVariable = 5;
int * pMyVariable = &myVariable;
cout << "myVariable is located at: " << pMyVariable;
...
As you can see, it is pretty easy, like anything else in C++ once you understand the concepts. Now, there are three VERY important things to pay attention to when declaring a pointer.
(1) a pointer needs to be defined as the exact same type of variable as what it is pointing at. We were pointing to an int, so the pointer had to be an int as well.
(2) don't forget the asterisk. That is what is letting the compiler know this is a pointer.
(3) either point the pointer to an address of a variable, define a new variable for the pointer, or POINT IT AT NULL (... int * pMyVariable = 0; ...). If you have the pointer "dangling" or "wild" or "stray" it could cause some rather unpleasant bugs in your program that will be a nightmare to find in the future.
What's the Point
To understand why you need to be able to use pointers in the first place, you need to understand the basic structure of how computers store information in the RAM. Go get a book, as that is a topic and a half. You don't need to know the mechanical function of RAM or the technical specs, but you do need to know how the RAM houses information in different "areas" and what happens to each area in C++ while the program functions. I have said it before, and I will say it again... if you want to actually learn C++, go beyond just reading me review what I have learned... buy a book or enroll in a class. The benefits of being taught correctly the first time through is muy bien.
Pointers can access the data too!
So, pointers also can access the data of the variable they point to. It could be slightly confusing I guess, but with practice it doesn't seem to bother me anymore. The example we wrote before... let's change the variable data and see how it works eh?
...
int myVariable = 5;
int * pMyVariable = &myVariable;
cout << "myVariable, located at " << pMyVariable
*pMyVariable = 10;
cout << "myVariable, located at " << pMyVariable
...
Look confusing at all? The asterisk has dual function. When you are defining the pointer, it informs the compiler that this variable contains an address. When you are using it any other time in your code, it says that you are accessing the VALUE stored at the address the pointer points to.
So, in the above example:
pMyVariable would equal some hexidecimal address
*pMyVariable starts out as 5, and then we change it to 10. *pMyVariable is identical to myVariable in this example. When one changes, the other changes as well. They are just different labels for the same data.
Practical Uses of pointers
I don't have a full grasp on what the usages are yet, that will come tomorrow and in the following days, but I can give an overview.
pointers store data on the "free store" part of the RAM, which means after the function is over, the data is still there (unless you explicitely delete it). Of course, you have to make sure you can still ACCESS that data... otherwise you have a memory leak. The data will still reside in the address the pointer points to, BUT the pointer variable itself is stored in the "stack" of the function, so after it is done it will be GONE, unless you have the pointer somewhere other than the function and you are passing it into the function in the first place.
The one example the book gave (and the author didn't have me make this so I am not sure how exactly it works yet), is a word processor. A dialog box needs to have access to the document, but once the dialog box finishes it's task... we don't want the document disappearing. Makes sense... but why not just use an object for the document? I don't know, I'm sure there are reasons.
pointers can also CREATE a variable. The example we listed before, here is yet another version of it:
...
int * pMyVariable = new int (5);
cout << "pMyVariable points to: " << pMyVariable
cout << "*pMyVariable is: " << *pMyVariable << "\n";
...
This should demonstrate what I am talking about. You can of course use pointers for objects as well. It would be the same as before, but instead of type int, you would use your class name. To access functions from the pointer for that object... you would do something like this:
...
fish * pGoldfish = new fish;
cout << "pGoldfish is a fish with the default weight of " << pGoldfish->GetWeight() << " lbs.\n";
pGoldfish->SetWeight(10);
cout << "pGoldfish is now 10 lbs, see: " << pGoldfish->GetWeight() << " lbs.\n";
...
Obviously, you would need to have a class named "fish" with the functions "GetWeight()" and "SetWeight( int weight )". And having a default weight in the constructor would be a very good thing if you are going to copy and paste that little snippet above. The "->" accesses functions of an object through a pointer. Notice that you don't need an asterisk to get into the functions.
Summary and Workshop
That is just about all they covered. The end of the chapter demonstrated a program that seperates a string into words. The program obviously used pointers to do a lot of brunt work and showed how flexible they are when traversing functions. The exercises were very, very easy. Just making sure you understood how to use pointers really.
Saturday, December 13, 2008
Week 1, in review
So, I wrapped up one week of the book... a third of the way there. I treated myself to a couple of days of light duty. The book had a few pages of review and I covered those. Basically, it was a single program that did stuff with rectangles. It could draw them out in ASCII-test, calculate area or perimeter, allows you to change the height and width, and of course quit the program.
Nothing too difficult. Although there was an error in the code in the book that took me a bit to figure out. I still don't know why it was in there... I knew it didn't belong there and so I removed it and the program worked like a champ. Had a few typos here or there, but the compiler helped me hunt those little guys down.
Now I can make a very simple, text-only program that runs in the terminal in linux. I could write a program for an early 90's library computer now! Score! [/sarcasm]
Nothing too difficult. Although there was an error in the code in the book that took me a bit to figure out. I still don't know why it was in there... I knew it didn't belong there and so I removed it and the program worked like a champ. Had a few typos here or there, but the compiler helped me hunt those little guys down.
Now I can make a very simple, text-only program that runs in the terminal in linux. I could write a program for an early 90's library computer now! Score! [/sarcasm]
Thursday, December 11, 2008
Week 1, Day 7
goto, while loops, for loops, do...while loops, and switch expressions. Pretty basic program flow parts of basically any programming language (ok, so the last two are not in all the languages I have learned so far... but still).
Today's "Class"
Today's chapter was all about controlling the flow of the program with loops and another condition branch tool.
goto
Basically... after showing how to do it, the book said to never use it. It makes code rather confusing. It has it's very rare uses, but apparently the author has only had to use it ONCE in his entire C++ career. If you feel you absolutely need to use it, you should be more capable at C++ than this blog will probably EVER teach you... so I won't bother going any further on it.
while loops
Very basic and straight forward. While the condition is true, the code is executed. Example:
int i = 0;
while( i < 10 )
{
i++;
cout << "i: " << i << endl;
}
That little code will just print "1", "2", "3", ... all the way down to "10". Nothing too complicated. Obviously, if the condition is false to begin with, the code is not ran at all. Be careful not to make a never ending loop unless you have a way out of it.
Speaking of a way out of a never ending loop... the break; command will exit a loop. The continue; command will restart the current loop at the beginning of the loop (skipping whatever is after the command). Apparently, those are suppose to be used sparingly... so as to make your code more easily read. Good to know that you can do that in your code if you come across something that needs it.
do...while loops
If you would like to create a while loop that for sure executes at least once, use a do...while. It is identical to a while loop, with the simple exception that it tests for while AFTER the code block is ran and not before it is ran. Example:
int tooLarge = 15;
do
{
tooLarge++;
cout << "tooLarge: " << tooLarge << endl;
} while(tooLarge < 10);
If this was a while loop, it would never print anything, but the do...while loop executes once and then figures out that it shouldn't loop because the condition is not met. Obviously has some usefulness in it... I can't think of a good example at the moment though. Sorry. Moving on then...
for loops
While loops are fantastic... but sometimes they are not really the right tool for the job. For instance, the code I gave as an example earlier could be done much better with a for loop. Like this:
for( i=0; i<10; i++ )
cout << "i: " << i << endl;
Nice and tidy eh? for loops have an expression, a condition, and another expression. Usually you initialize your variable in the first expression and the last expression does some sort of incrementing or decrementing. However, the chapter had me write several different programs that used all sorts of expressions in there. You can even just write a condition... but then it is just a differently labeled while loop. One of the last examples was a "forever" loop, which is a identical to a while loop that had a condition of "true". However, I love what some programmers put down to create a "forever" loop:
#define EVER ;;
....
for(EVER)
{
....
}
That has a degree of elegance and readability I love. #define EVER ;; tells the compiler that whenever it sees "EVER" in the code, replace it with the text ";;". Thus, the "forever" loop is correctly written for( ; ; ){ ... } as in a blank expression, a blank condition, and a blank expression... or a loop that can never end.
switch expressions
Instead of doing massive if...else branches, you could use switch... case.... default... expressions. You provide a variable, and then write down the different "cases" (if the value equals this... do this...). You can (and should) specify a default, if for no other reason than to debug. Here is a sample switch expression:
cin >> choice;
switch( choice )
{
case (1): cout << "you chose option one.";
break;
case (2): cout << "you chose option two.";
break;
default: cout << "that wasn't an option!";
break;
}
The break; commands are important. If you don't have them in there it will execute several commands (give it a try, it was interesting to see what happens). Obviously this is not as powerful as an if...else, but if you can use it, it is much tidier.
Summary and Workshop
Thus ended another chapter with the common summary. Exercises where ok, but I had the concept down WELL before I even finished the chapter. The chapter really seemed to drag on for me. But, that is simply because I have been using these sort of program flow loops and expressions since I was a sophmore in high school (used to program those graphic calculators for math class... surprisingly that became rather a useful primer as I went through life...).
Today's "Class"
Today's chapter was all about controlling the flow of the program with loops and another condition branch tool.
goto
Basically... after showing how to do it, the book said to never use it. It makes code rather confusing. It has it's very rare uses, but apparently the author has only had to use it ONCE in his entire C++ career. If you feel you absolutely need to use it, you should be more capable at C++ than this blog will probably EVER teach you... so I won't bother going any further on it.
while loops
Very basic and straight forward. While the condition is true, the code is executed. Example:
int i = 0;
while( i < 10 )
{
i++;
cout << "i: " << i << endl;
}
That little code will just print "1", "2", "3", ... all the way down to "10". Nothing too complicated. Obviously, if the condition is false to begin with, the code is not ran at all. Be careful not to make a never ending loop unless you have a way out of it.
Speaking of a way out of a never ending loop... the break; command will exit a loop. The continue; command will restart the current loop at the beginning of the loop (skipping whatever is after the command). Apparently, those are suppose to be used sparingly... so as to make your code more easily read. Good to know that you can do that in your code if you come across something that needs it.
do...while loops
If you would like to create a while loop that for sure executes at least once, use a do...while. It is identical to a while loop, with the simple exception that it tests for while AFTER the code block is ran and not before it is ran. Example:
int tooLarge = 15;
do
{
tooLarge++;
cout << "tooLarge: " << tooLarge << endl;
} while(tooLarge < 10);
If this was a while loop, it would never print anything, but the do...while loop executes once and then figures out that it shouldn't loop because the condition is not met. Obviously has some usefulness in it... I can't think of a good example at the moment though. Sorry. Moving on then...
for loops
While loops are fantastic... but sometimes they are not really the right tool for the job. For instance, the code I gave as an example earlier could be done much better with a for loop. Like this:
for( i=0; i<10; i++ )
cout << "i: " << i << endl;
Nice and tidy eh? for loops have an expression, a condition, and another expression. Usually you initialize your variable in the first expression and the last expression does some sort of incrementing or decrementing. However, the chapter had me write several different programs that used all sorts of expressions in there. You can even just write a condition... but then it is just a differently labeled while loop. One of the last examples was a "forever" loop, which is a identical to a while loop that had a condition of "true". However, I love what some programmers put down to create a "forever" loop:
#define EVER ;;
....
for(EVER)
{
....
}
That has a degree of elegance and readability I love. #define EVER ;; tells the compiler that whenever it sees "EVER" in the code, replace it with the text ";;". Thus, the "forever" loop is correctly written for( ; ; ){ ... } as in a blank expression, a blank condition, and a blank expression... or a loop that can never end.
switch expressions
Instead of doing massive if...else branches, you could use switch... case.... default... expressions. You provide a variable, and then write down the different "cases" (if the value equals this... do this...). You can (and should) specify a default, if for no other reason than to debug. Here is a sample switch expression:
cin >> choice;
switch( choice )
{
case (1): cout << "you chose option one.";
break;
case (2): cout << "you chose option two.";
break;
default: cout << "that wasn't an option!";
break;
}
The break; commands are important. If you don't have them in there it will execute several commands (give it a try, it was interesting to see what happens). Obviously this is not as powerful as an if...else, but if you can use it, it is much tidier.
Summary and Workshop
Thus ended another chapter with the common summary. Exercises where ok, but I had the concept down WELL before I even finished the chapter. The chapter really seemed to drag on for me. But, that is simply because I have been using these sort of program flow loops and expressions since I was a sophmore in high school (used to program those graphic calculators for math class... surprisingly that became rather a useful primer as I went through life...).
Labels:
C++,
do...while,
for,
goto,
program flow,
switch,
while
Wednesday, December 10, 2008
Week 1, Day 6
Hey, something completely new to me! Score. Finally I get into some meat.
Today's "Class"
Today was all about Classes and objects.
I have never done object oriented programming (in the strictest sense at least). This chapter really made the five slow days behind it pay off (well... start to pay off). I actually learned some new concepts and my brain is busy absorbing the new information and what it means for my code in other languages. So, I am pretty happy obviously.
Classes are the new "types" of variables the book talked about in the past. I think this is a misnomer personally. You still are restricted to the limitations of the pre-existing variable types in C++. I was hoping (and still hold out some hope that this is included in C++) that you could have an array, or even better, a hash. How I have become so dependent on arrays and hashes.... PHP has spoiled me I tell you! So, while not able to create a new type of variable in the sense I wanted, classes are still very useful. Classes (and the individual objects of that class) are organized groups of data and functions. Here is a for instance:
Class: Car
Properties: Wheel Size, transmission speed, engine size, gas tank size, number of passengers, ...
Functions: Throttle engine, shift gear up, shift gear down, engage brakes, open door, ....
With all of the variables set up and the functions defined, you could easily create an instance of a "Car" by typing:
Car mySubaruImpreza;
Or, if you have the class constructor built, you could assign all of the specifications of the car right when you create it.
Honestly, writing a review of the chapter today would be like typing the entire thing. I'm not going to do that. So, instead, buy a book, check one out from the library, google C++ Classes, or whatever you usually do. This is one of those things you should learn from something more substantial than a blog designed to review one person's efforts toward learning how to program on Linux.
Summary and Workshop
The exercises again made me think. Probably because the whole Class thing is fairly new to me. The closest thing I have come to this is doing some OOP stuff in JavaScript. Oddly enough, it is actually pretty similar... but C++ takes it above and beyond the little I learned in JavaScript.
I made a few mistakes in my code as I worked on the proposed problem. I'm glad too... because every bug I made taught me a little about Class declarations and definitions that I had apparently not paid attention to during the chapter.
Today's "Class"
Today was all about Classes and objects.
I have never done object oriented programming (in the strictest sense at least). This chapter really made the five slow days behind it pay off (well... start to pay off). I actually learned some new concepts and my brain is busy absorbing the new information and what it means for my code in other languages. So, I am pretty happy obviously.
Classes are the new "types" of variables the book talked about in the past. I think this is a misnomer personally. You still are restricted to the limitations of the pre-existing variable types in C++. I was hoping (and still hold out some hope that this is included in C++) that you could have an array, or even better, a hash. How I have become so dependent on arrays and hashes.... PHP has spoiled me I tell you! So, while not able to create a new type of variable in the sense I wanted, classes are still very useful. Classes (and the individual objects of that class) are organized groups of data and functions. Here is a for instance:
Class: Car
Properties: Wheel Size, transmission speed, engine size, gas tank size, number of passengers, ...
Functions: Throttle engine, shift gear up, shift gear down, engage brakes, open door, ....
With all of the variables set up and the functions defined, you could easily create an instance of a "Car" by typing:
Car mySubaruImpreza;
Or, if you have the class constructor built, you could assign all of the specifications of the car right when you create it.
Honestly, writing a review of the chapter today would be like typing the entire thing. I'm not going to do that. So, instead, buy a book, check one out from the library, google C++ Classes, or whatever you usually do. This is one of those things you should learn from something more substantial than a blog designed to review one person's efforts toward learning how to program on Linux.
Summary and Workshop
The exercises again made me think. Probably because the whole Class thing is fairly new to me. The closest thing I have come to this is doing some OOP stuff in JavaScript. Oddly enough, it is actually pretty similar... but C++ takes it above and beyond the little I learned in JavaScript.
I made a few mistakes in my code as I worked on the proposed problem. I'm glad too... because every bug I made taught me a little about Class declarations and definitions that I had apparently not paid attention to during the chapter.
Labels:
C++,
Classes,
Object Oriented Programming,
Objects,
OOP
Tuesday, December 9, 2008
Week 1, Day 5
I had to fight paint fumes last night, so I escaped my workspace and let the headache subside. Finished the lesson up this morning.
Today's "Class"
Functions are pretty straight forward in C++. The one new piece of code, for me, is that you have to declare a "prototype" of the function. The prototype is just a quick line of code that tells the compiler what value your function returns, the function name, and the type of variables you pass into your function (as well as any default values FOR those variables).
Sample function prototype:
int rectangleArea ( int length, int width );
You could also write it:
int rectangleArea ( int, int );
Adding the extra values of variable names though helps in the debugging process (or so the book says). You can use different parameter names than the actual defined function though... so just label it something useful if you are going to add parameter names. To add default values you would have the prototype look something like this:
int rectangleArea ( int length, int width = 10 );
This means you COULD pass one argument when you call this function and it would assume the width was 10 in that case. Obviously, you cannot have a default value for length if it (a) comes before width in the prototype and definition and (b) width does not have a default value also.
You define a function in a fairly straightforward manner. You have a header to the function with the return type, function name, and parameter list (almost identical to the prototype, except that the parameters MUST have names in the function header and you can't add default values in the header).
Here is the rectangleArea function definition:
int rectangleArea ( int rectangleLength, int rectangleWidth )
{
return (rectangleLength * rectangleWidth);
}
Super easy right? Now the chapter went into some interesting details about what you can do with functions in C++.
Function Overloading
Because you have to define the return value and the variable types of the parameters... what happens when you pass in a float instead of an int. Yep, it creates an error. However, in C++ you CAN use identical function names as long as they return and / or accept different variable types. Like this:
int Double ( int );
float Double ( float );
int Double ( int x )
{
return (x * 2);
}
float Double ( float x )
{
return (x * 2);
}
This way, if you call the function Double with either an integer or a float it still returns the value you want it to. You don't need to have two differently named functions and then have to remember which function you needed to call. You can "overload" a function many, many times (say you want to double short, long, float, double, and maybe even some signed and unsigned variables as well...). The book did mention you have to be careful with doing something like this:
int Divide ( int x, int y = 2 );
int Divide ( int x );
int Divide ( int x, int y )
{
return (x / y);
}
int Divide ( int x )
{
return (x / 3);
}
As you can imagine, if you DO make this function call "Divide(myVariable)", the compiler won't know which function you actually want to call and will throw up an error.
Inline Functions
Another feature in C++ is the ability to add a function "inline" the code when it is called. This can give you some performance gain... but at a cost. Every time you call that function, it will add the entire function body into your code and thus take up more storage on the hard drive, CD, etc. The book recommended you only use inline functions when the function is one or two lines as larger functions tend to bloat your code and the performance is probably negated by the extra code size. How much of a benefit you get by doing inline functions... I know not. Maybe I should play around and do some benchmarking... as soon as I know how to benchmark C++ code at least.
Here is how you have a function inline. In the function prototype declaration, you simply add the keyword "inline" before the return type. Here is an example:
inline int Divide ( int x, int y );
Recursive Functions
You can have a function call itself... or you can have a function call another function, which happens to call the first funciton again...
Confusing? Perhaps, that wasn't the best explanation. The danger is if you don't have a condition to the function that is being recurringly called. If the function just continues to call itself indefinitely it will bloat your program in your RAM until it crashes.
Why would you want to use Recursive functions? The book had a couple of examples of ways to use recursive functions, but I don't see why you would need to do them with those examples. In PHP or PERL you could easily do a FOR loop and take care of their examples without adding more and more to the RAM. However the book did mention there are the occasional problems that recursive functions are an elegant answer for. When I find one, I will no doubt be happy I know how to do recursive functions (or at least know where to look up so I can remember... although the implementation is exactly what you think it is... you just call the function from within the same function or another called function...).
Summary and Workshop
In the exercises there were some "bug finding" programs where you had to figure out what was wrong. Some of the bugs are things I'm sure programmers do ALL THE TIME. Like accidentally typing a semicolon after function header (in the definition... not after the prototype, which is required). I caught myself doing that a few times... but I caught it write after I typed it, so it wasn't a big deal. If I wasn't paying too much attention though I could see that causing some irritation. One of the exercises was to do a recursive function that solved x to the power of n sort of math problems (like x squared... x ^ 8... etc). That one made me think. Took me back to middle school when we had to do that sort of stuff all the time. Or was that High School... *shrug* whatever, either way it was a while ago. I ended up getting it first try though, so at least I had that going for me.
Today's "Class"
Functions are pretty straight forward in C++. The one new piece of code, for me, is that you have to declare a "prototype" of the function. The prototype is just a quick line of code that tells the compiler what value your function returns, the function name, and the type of variables you pass into your function (as well as any default values FOR those variables).
Sample function prototype:
int rectangleArea ( int length, int width );
You could also write it:
int rectangleArea ( int, int );
Adding the extra values of variable names though helps in the debugging process (or so the book says). You can use different parameter names than the actual defined function though... so just label it something useful if you are going to add parameter names. To add default values you would have the prototype look something like this:
int rectangleArea ( int length, int width = 10 );
This means you COULD pass one argument when you call this function and it would assume the width was 10 in that case. Obviously, you cannot have a default value for length if it (a) comes before width in the prototype and definition and (b) width does not have a default value also.
You define a function in a fairly straightforward manner. You have a header to the function with the return type, function name, and parameter list (almost identical to the prototype, except that the parameters MUST have names in the function header and you can't add default values in the header).
Here is the rectangleArea function definition:
int rectangleArea ( int rectangleLength, int rectangleWidth )
{
return (rectangleLength * rectangleWidth);
}
Super easy right? Now the chapter went into some interesting details about what you can do with functions in C++.
Function Overloading
Because you have to define the return value and the variable types of the parameters... what happens when you pass in a float instead of an int. Yep, it creates an error. However, in C++ you CAN use identical function names as long as they return and / or accept different variable types. Like this:
int Double ( int );
float Double ( float );
int Double ( int x )
{
return (x * 2);
}
float Double ( float x )
{
return (x * 2);
}
This way, if you call the function Double with either an integer or a float it still returns the value you want it to. You don't need to have two differently named functions and then have to remember which function you needed to call. You can "overload" a function many, many times (say you want to double short, long, float, double, and maybe even some signed and unsigned variables as well...). The book did mention you have to be careful with doing something like this:
int Divide ( int x, int y = 2 );
int Divide ( int x );
int Divide ( int x, int y )
{
return (x / y);
}
int Divide ( int x )
{
return (x / 3);
}
As you can imagine, if you DO make this function call "Divide(myVariable)", the compiler won't know which function you actually want to call and will throw up an error.
Inline Functions
Another feature in C++ is the ability to add a function "inline" the code when it is called. This can give you some performance gain... but at a cost. Every time you call that function, it will add the entire function body into your code and thus take up more storage on the hard drive, CD, etc. The book recommended you only use inline functions when the function is one or two lines as larger functions tend to bloat your code and the performance is probably negated by the extra code size. How much of a benefit you get by doing inline functions... I know not. Maybe I should play around and do some benchmarking... as soon as I know how to benchmark C++ code at least.
Here is how you have a function inline. In the function prototype declaration, you simply add the keyword "inline" before the return type. Here is an example:
inline int Divide ( int x, int y );
Recursive Functions
You can have a function call itself... or you can have a function call another function, which happens to call the first funciton again...
Confusing? Perhaps, that wasn't the best explanation. The danger is if you don't have a condition to the function that is being recurringly called. If the function just continues to call itself indefinitely it will bloat your program in your RAM until it crashes.
Why would you want to use Recursive functions? The book had a couple of examples of ways to use recursive functions, but I don't see why you would need to do them with those examples. In PHP or PERL you could easily do a FOR loop and take care of their examples without adding more and more to the RAM. However the book did mention there are the occasional problems that recursive functions are an elegant answer for. When I find one, I will no doubt be happy I know how to do recursive functions (or at least know where to look up so I can remember... although the implementation is exactly what you think it is... you just call the function from within the same function or another called function...).
Summary and Workshop
In the exercises there were some "bug finding" programs where you had to figure out what was wrong. Some of the bugs are things I'm sure programmers do ALL THE TIME. Like accidentally typing a semicolon after function header (in the definition... not after the prototype, which is required). I caught myself doing that a few times... but I caught it write after I typed it, so it wasn't a big deal. If I wasn't paying too much attention though I could see that causing some irritation. One of the exercises was to do a recursive function that solved x to the power of n sort of math problems (like x squared... x ^ 8... etc). That one made me think. Took me back to middle school when we had to do that sort of stuff all the time. Or was that High School... *shrug* whatever, either way it was a while ago. I ended up getting it first try though, so at least I had that going for me.
Sunday, December 7, 2008
Week 1, Day 4
Another super easy lesson. Covered statements, expressions, operators, etc. Nothing groundbreaking. It is identical to PERL and PHP, and since I have programmed in both I could have easily passed up THIS chapter as well.
The only thing unique about this chapter was the exercises at the end... two of the tripped me up and made me think.
Today's "Class"
Not interested in putting down the majority of the chapter, as it is super basic for anyone with a background in either C, PERL, PHP, even Visual Basic to some extent.
One thing to note though, is the "static_cast( variableName ) " from the lesson. This lets you convert an integer into a float (or whatever you have instead of "float", I would assume). This does NO good unless you actually do it BEFORE you need a decimal value though, otherwise it will just be an integer stored as a float.
The only thing unique about this chapter was the exercises at the end... two of the tripped me up and made me think.
Today's "Class"
Not interested in putting down the majority of the chapter, as it is super basic for anyone with a background in either C, PERL, PHP, even Visual Basic to some extent.
One thing to note though, is the "static_cast
Saturday, December 6, 2008
Week 1, Day 3
Today's "Class"
Today's chapter covered variables and constants in a bit more depth. Including the different types of variables and what their limitations were and appropriate uses.
Variable Types
According to the book, there are 3 basic types of variables: integers, characters, and floating-point variables. The chapter mentioned that you can define your own variable types as well, but we won't discuss that until the end of the week looks like.
bool
bool (no doubt short for boolean) variables are true or false, 0 or 1. They take up 1 byte on your the RAM.
unsigned short {int}
short integers are 2 bytes on the RAM. unsigned short integers have a range of 0 to 65,535. The book had a few example programs of what would happen if you assign a value that exceeds this max. As it turns out, the value is "wrapped" around like an odometer in a car. Good to know in case you get some strange values in a program in the future.
{signed} short {int}
short integers default to signed and have a range of -32,768 to 32,767. You can explicitly define a variable as a signed short, or just type short. When the values "wrap" on a signed integer, it goes from the highest positive value (32,767) to the highest negative value (-32,768) and then keeps adding to the value (which would bring the negative number down toward 0, and then once 0 is passed it would add the numbers to the positive until we reach the max again). If this is confusing... try to write a program that adds to a maxed out variable and see what happens.
unsigned long {int}
long integers set aside 4 bytes of space on RAM. unsigned long integers have a range of 0 to 4,294,967,295. If you exceed the max, it wraps around... look at the unsigned short type for specifics.
{signed} long {int}
signed long integers have a range of -2,147,483,648 to 2,147,483,647.
int
int can be either a short or a long, depending on the machine running it. It's good practice to specify the variable as either short or long instead of using int because of this very detail. I think on any new computer or OS, it would be a long. It is on my computer and it was on the author's computer as well. int can be either signed or unsigned, and it defaults on signed.
char
1 byte and it is any of the ASCII default text characters. The book had an interesting little program that cycled through the characters that are assigned to the numbers 32 - 128. A char would be "a", or "A", "0", "8", etc. Any character on your keyboard is an example. It is a "character". Hopefully that is clear enough for you. I understand it fine, but don't know how else to describe it.
float
float variables are interesting. They let you go to some extremely large values and they take up 4 bytes. But, there is a definite problem with them. They are NOT accurate. If you need a decimal number, use a float (or a double). Range is from 1.2e-38 to 3.4 e38. If my scientific notation is remembered correctly, that is from 0.000000000000000000000000000000000000012 to 340,000,000,000,000,000,000,000,000,000,000,000,000. But you better not quote me on that, High School was a long time ago. The accuracy is NOT perfect for float variables. It remembers the first 7 digits and the rest are "lost".
double
double variables are "double" the size of float variables. They are basically the same as float variables. They have a range of 2.2e-308 to 1.8e308. I will not type out what that translates into. It is a ridiculously huge range though. Instead of remembering 7 digits, double remembers 19 digits.
Constants
Constants are variables that NEVER change in the program (well, never change until you recompile the program at least). There are two ways in the book on how to define constants, one is labeled as obsolete, so I am going to ignore that it exists. So, then the one way you define a constant goes something like this:
const unsigned short daysInWeek = 7;
You can obviously define the constant as something other than an unsigned short. This sets up a "symbolic constant", which is basically saying that "daysInWeek" is synonymous with the value "7". Incidentally, a numerical value like "7" is also a constant of sorts. Those sort of constants are called "literal constants" (since they are LITERALLY constants).
Summary and Workshop
The lesson wrapped up by doing a brief overview of what was covered. The author brought out that C++ doesn't REQUIRE you to know how the physical components in a computer handles data, but that knowledge would come in handy. C++ has a lot of power and has a ton of control over your computer.
The workshop mostly dealt with trying to decide which type of variable you need in order to store certain values provided in the book. Wasn't that difficult to me and I was able to fly through it.
Today's chapter covered variables and constants in a bit more depth. Including the different types of variables and what their limitations were and appropriate uses.
Variable Types
According to the book, there are 3 basic types of variables: integers, characters, and floating-point variables. The chapter mentioned that you can define your own variable types as well, but we won't discuss that until the end of the week looks like.
bool
bool (no doubt short for boolean) variables are true or false, 0 or 1. They take up 1 byte on your the RAM.
unsigned short {int}
short integers are 2 bytes on the RAM. unsigned short integers have a range of 0 to 65,535. The book had a few example programs of what would happen if you assign a value that exceeds this max. As it turns out, the value is "wrapped" around like an odometer in a car. Good to know in case you get some strange values in a program in the future.
{signed} short {int}
short integers default to signed and have a range of -32,768 to 32,767. You can explicitly define a variable as a signed short, or just type short. When the values "wrap" on a signed integer, it goes from the highest positive value (32,767) to the highest negative value (-32,768) and then keeps adding to the value (which would bring the negative number down toward 0, and then once 0 is passed it would add the numbers to the positive until we reach the max again). If this is confusing... try to write a program that adds to a maxed out variable and see what happens.
unsigned long {int}
long integers set aside 4 bytes of space on RAM. unsigned long integers have a range of 0 to 4,294,967,295. If you exceed the max, it wraps around... look at the unsigned short type for specifics.
{signed} long {int}
signed long integers have a range of -2,147,483,648 to 2,147,483,647.
int
int can be either a short or a long, depending on the machine running it. It's good practice to specify the variable as either short or long instead of using int because of this very detail. I think on any new computer or OS, it would be a long. It is on my computer and it was on the author's computer as well. int can be either signed or unsigned, and it defaults on signed.
char
1 byte and it is any of the ASCII default text characters. The book had an interesting little program that cycled through the characters that are assigned to the numbers 32 - 128. A char would be "a", or "A", "0", "8", etc. Any character on your keyboard is an example. It is a "character". Hopefully that is clear enough for you. I understand it fine, but don't know how else to describe it.
float
float variables are interesting. They let you go to some extremely large values and they take up 4 bytes. But, there is a definite problem with them. They are NOT accurate. If you need a decimal number, use a float (or a double). Range is from 1.2e-38 to 3.4 e38. If my scientific notation is remembered correctly, that is from 0.000000000000000000000000000000000000012 to 340,000,000,000,000,000,000,000,000,000,000,000,000. But you better not quote me on that, High School was a long time ago. The accuracy is NOT perfect for float variables. It remembers the first 7 digits and the rest are "lost".
double
double variables are "double" the size of float variables. They are basically the same as float variables. They have a range of 2.2e-308 to 1.8e308. I will not type out what that translates into. It is a ridiculously huge range though. Instead of remembering 7 digits, double remembers 19 digits.
Constants
Constants are variables that NEVER change in the program (well, never change until you recompile the program at least). There are two ways in the book on how to define constants, one is labeled as obsolete, so I am going to ignore that it exists. So, then the one way you define a constant goes something like this:
const unsigned short daysInWeek = 7;
You can obviously define the constant as something other than an unsigned short. This sets up a "symbolic constant", which is basically saying that "daysInWeek" is synonymous with the value "7". Incidentally, a numerical value like "7" is also a constant of sorts. Those sort of constants are called "literal constants" (since they are LITERALLY constants).
Summary and Workshop
The lesson wrapped up by doing a brief overview of what was covered. The author brought out that C++ doesn't REQUIRE you to know how the physical components in a computer handles data, but that knowledge would come in handy. C++ has a lot of power and has a ton of control over your computer.
The workshop mostly dealt with trying to decide which type of variable you need in order to store certain values provided in the book. Wasn't that difficult to me and I was able to fly through it.
Week 1, Day 2
Super easy lesson today. I could have easily skipped the entire chapter. It introduced nothing too new for me, coming from PERL and PHP. Even JavaScript coders could probably skip this entire chapter.
I did learn something new though. I have used comments in the past to basically detail everything the function that follows does... and apparently so did the author. He wrote in this book that he had a change of mind from previous editions of this book. I like his new approach and will have to apply it. He says that now he uses comments to explain WHY the code is being called. WHAT the code is doing should be sufficiently evident from the actual code. It is the programmers responsibility to write his code in a way that is understandable as to what it is doing, without the lazy approach of comments.
Today's "Class"
Today discussed the basic components of a C++ program. These include objects, functions, variables, and other components. The chapter does a quick introduction to functions, throws in some variables in a few sample programs you recreate, and shows you how to add comments (and when to use them).
I have no desire to go into any depth into this chapter... So, if you are trying to learn from my blogs for some reason... just google the basics of C++ functions, variables, and comments and you should be good.
I did learn something new though. I have used comments in the past to basically detail everything the function that follows does... and apparently so did the author. He wrote in this book that he had a change of mind from previous editions of this book. I like his new approach and will have to apply it. He says that now he uses comments to explain WHY the code is being called. WHAT the code is doing should be sufficiently evident from the actual code. It is the programmers responsibility to write his code in a way that is understandable as to what it is doing, without the lazy approach of comments.
Today's "Class"
Today discussed the basic components of a C++ program. These include objects, functions, variables, and other components. The chapter does a quick introduction to functions, throws in some variables in a few sample programs you recreate, and shows you how to add comments (and when to use them).
I have no desire to go into any depth into this chapter... So, if you are trying to learn from my blogs for some reason... just google the basics of C++ functions, variables, and comments and you should be good.
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...".
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...".
Subscribe to:
Posts (Atom)