[Upgrading, Modifying and Unlocking] donhashem.dh:
LESSON 11

Lesson 11 - Typecasting

Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.

For example:

Code: [Select]

#include <iostream>

using namespace std;

int main()

{

cout<< (char)65 <<"\n";

// The (char) is a typecast, telling the computer to interpret the 65 as a

// character, not as a number. It is going to give the character output of

// the equivalent of the number 65 (It should be the letter A for ASCII).

cin.get();

}

One use for typecasting for is when you want to use the ASCII characters. For example, what if you want to create your own chart of all 256 ASCII characters. To do this, you will need to use to typecast to allow you to print out the integer as its character equivalent.

Code: [Select]

#include <iostream>

using namespace std;

int main()

{

for ( int x = 0; x < 256; x++ ) {

cout<< x <<". "<< (char)x <<" ";

//Note the use of the int version of x to

// output a number and the use of (char) to

// typecast the x into a character

// which outputs the ASCII character that

// corresponds to the current number

}

cin.get();

}

The typecast described above is a C-style cast, C++ supports two other types. First is the function-style cast:

Code: [Select]

int main()

{

cout<< char ( 65 ) <<"\n";

cin.get();

}

This is more like a function call than a cast as the type to be cast to is like the name of the function and the value to be cast is like the argument to the function. Next is the named cast, of which there are four:

Code: [Select]

int main()

{

cout<< static_cast<char> ( 65 ) <<"\n";

cin.get();

}

static_cast is similar in function to the other casts described above, but the name makes it easier to spot and less tempting to use since it tends to be ugly. Typecasting should be avoided whenever possible. The other three types of named casts are const_cast, reinterpret_cast, and dynamic_cast. They are of no use to us at this time.

LESSON 12

Lesson 12 - Classes

C++ is a bunch of small additions to C, and one major addition. This one addition is the object-oriented approach. As its name suggests, this deals with objects. Of course, these are not real-life objects. Instead, these objects are the essential definitions of real world objects. Classes are collections of data related to a single object type. Classes not only include information regarding the real world object, but also functions to access the data, and classes possess the ability to inherit from other classes. Inheritance is covered in a later lesson.

What is this mysterious beast? Well, it is not only a collection of variables under one heading, but it is a collection of functions under that same heading. If a class is a house, then the functions will be the doors and the variables will be the items inside the house. They usually will be the only way to modify the variables in this structure, and they are usually the only to access the variables in this structure. The idea to make programs more modular - encapsulation. A section of code will have its own functions and variables that control what it can do, and it does not require anything outside of itself to function. While the class may require initialization with data, it does not require outside variables or functions to manipulate the data. That allows programs to reuse the same code more easily.

The syntax for these classes is simple. First, you put the keyword 'class' then the name of the class. Our example will use the name computer. Then you put an open bracket. Before putting down the different variables, it is necessary to put the degree of restriction on the variable. There are three levels of restriction. The first is public, the second protected, and the third private. For now, all you need to know is that the public restriction allows any part of the program, including that which is not part of the class, access the variables specified as public. The protected restriction prevents functions outside the class to access the variable. The syntax for that is merely the restriction keyword (public, private, protected) and then a colon. Finally, you put the different variables and functions (You usually will only put the function prototypes) you want to be part of the class. Then you put a closing bracket and semicolon. Keep in mind that you still must end the function prototype(s) with a semi-colon.

Classes should always contain two functions: the constructor and destructor. The syntax for them is simple, the class name denotes a constructor, a ~ before the class name is a destructor. The basic idea is to have the constructor initialize variables, and to have the destructor clean up after the class, which includes freeing any memory allocated. The only time the constructor is called is when the programmer declares an instance of the class, which will automatically call the constructor. The only time the destructor is called is when the instance of the class is no longer needed. When the program ends, or when its memory is deallocated (if you do not understand the deallocation part, do not worry). Keeps in mind this: NEITHER constructors NOR destructors RETURN AN ARGUMENT! This means you do not want to try to return a value in them.

The syntax for defining a function that is a member of a class outside of the actual class definition is to put the return type, then put the class name, two colons, and then the function name. This tells the compiler that the function is a member of that class.

For example:

Code: [Select]

#include <iostream>

using namespace std;

class Computer // Standard way of defining the class

{

public:

// This means that all of the functions below this(and any variables)

// are accessible to the rest of the program.

// NOTE: That is a colon, NOT a semicolon...

Computer();

// Constructor

~Computer();

// Destructor

void setspeed ( int p );

int readspeed();

protected:

// This means that all the variables under this, until a new type of

// restriction is placed, will only be accessible to other functions in the

// class. NOTE: That is a colon, NOT a semicolon...

int processorspeed;

};

// Do Not forget the trailing semi-colon

Computer::Computer()

{

//Constructors can accept arguments, but this one does not

processorspeed = 0;

}

Computer::~Computer()

{

//Destructors do not accept arguments

}

void Computer::setspeed ( int p )

{

// To define a function outside put the name of the class

// after the return type and then two colons, and then the name

// of the function.

processorspeed = p;

}

int Computer::readspeed()

{

// The two colons simply tell the compiler that the function is part

// of the clas

return processorspeed;

}

int main()

{

Computer compute;

// To create an 'instance' of the class, simply treat it like you would

// a structure. (An instance is simply when you create an actual object

// from the class, as opposed to having the definition of the class)

compute.setspeed ( 100 );

// To call functions in the class, you put the name of the instance,

// a period, and then the function name.

cout<< compute.readspeed();

// See above note.

}

This introduction is far from exhaustive and recommends practices that are not always the best option. This was done for simplicity. For more detail, I suggest using google.

LESSON 13

Lesson 13 - Functions Continued

In lesson 4 you were given the basic information on functions. However, I left out one item of interest. That item is the inline function. Inline functions are not very important, but it is good to understand them. The basic idea is to save time at a cost in space. Inline functions are a lot like a placeholder. Once you define an inline function, using the 'inline' keyword, whenever you call that function the compiler will replace the function call with the actual code from the function.

How does this make the program go faster? Simple, function calls are simply more time consuming than writing all of the code without functions. To go through your program and replace a function you have used 100 times with the code from the function would be time consuming not too bright. Of course, by using the inline function to replace the function calls with code you will also greatly increase the size of your program.

Using the inline keyword is simple, just put it before the name of a function. Then, when you use that function, pretend it is a non-inline function.

For example:

Code: [Select]

#include <iostream>

using namespace std;

inline void hello()

{

cout<<"hello";

}

int main()

{

hello(); //Call it like a normal function...

cin.get();

}

However, once the program is compiled, the call to hello(); will be replaced by the code making up the function.

A WORD OF WARNING: Inline functions are very good for saving time, but if you use them too often or with large functions you will have a tremendously large program. Sometimes large programs are actually less efficient, and therefore they will run more slowly than before. Inline functions are best for small functions that are called often.

In the future, we will discuss inline functions in terms of C++ classes. However, now that you understand the concept I will feel comfortable using inline functions in later tutorials.

LESSON 14

Lesson 14 - Accepting Command Lines

In C++ it is possible to accept command line arguments. To do so, you must first understand the full definition of int main(). It actually accepts two arguments, one is number of command line arguments, the other is a listing of the command line arguments.

It looks like this:

Code: [Select]

int main ( int argc, char *argv[] )

The integer, argc is the ARGument Count (hence argc). It is the number of arguments passed into the program from the command line, including the name of the program.

The array of character pointers is the listing of all the arguments. argv[0] is the name of the program, or an empty string if the name is not available. After that, every element number less than argc are command line arguments. You can use each argv element just like a string, or use argv as a two dimensional array. argv[argc] is a null pointer.

How could this be used? Almost any program that wants its parameters to be set when it is executed would use this. One common use is to write a function that takes the name of a file and outputs the entire text of it onto the screen.

Code: [Select]

#include <fstream>

#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )

{

if ( argc != 2 ) // argc should be 2 for correct execution

// We print argv[0] assuming it is the program name

cout<<"usage: "<< argv[0] <<" <filename>\n";

else {

// We assume argv[1] is a filename to open

ifstream the_file ( argv[1] );

// Always check to see if file opening succeeded

if ( !the_file.is_open() )

cout<<"Could not open file\n";

else {

char x;

// the_file.get ( x ) returns false if the end of the file

// is reached or an error occurs

while ( the_file.get ( x ) )

cout<< x;

}

// the_file is closed implicitly here

}

}

This program is fairly simple. It incorporates the full version of main. Then it first checks to ensure the user added the second argument, theoretically a file name. The program then checks to see if the file is valid by trying to open it. This is a standard operation that is effective and easy. If the file is valid, it gets opened in the process. The code is self-explanatory, but is littered with comments, you should have no trouble understanding its operation this far into the tutorial.