Topic : Programming in C/C++
Author : Alexander Allain
Page : << Previous 7  Next >>
Go to page :


   //Do Not forget the trailing semi-colon

Computer::Computer()
{ //Constructors can accept arguments, but this one does not
  processorspeed = 0;
  //Initializes it to zero
}

Computer::~Computer()
{  //Destructors do not accept arguments
}
//The destructor does not need to do anything.

void Computer::setspeed(int p)
{       //To define a function outside put the name of the function
 //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.
  return 0;
}


As you can see, this is a rather simple concept. However, it is very powerful. It makes it easy to prevent variables that are contained (or owned) by the class being overwritten accidentally. It also allows a totally different way of thinking about programming. I want to end this tutorial as an introduction, however.


Lesson 13: More on Functions


In lesson 4 you were given the basic information on functions. However, I left out two items of interest. First, when you declare a function you do not have to prototype it! You must give the function definition physically before you call the function. You simply type in the entire definition of the function where you would normally put the prototype.

For example:
#include <iostream.h>
void function(void)  
{      //Normally this would be the prototype. Do not include a semicolon
  //Only prototypes have semicolons
  cout<<"HA!  NO PROTOTYPE!";
}

int main()
{
  function();
//It works like a normal function now.
return 0;
}

The other programming concept 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. 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:
#include <iostream.h>
inline void hello(void)
{  //Just use the inline keyword before the function    
   cout<<"hello";
}
int main()
{
hello();
   //Call it like a normal function...
  return 0;
}


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: Accepting command line arguments


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:

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

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

The array of character pointers is the listing of all the arguments. argv[0] is entire path to the program including its name. 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.

How could this be used? Almost any program that wants it 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.

#include <fstream.h> //Needed to manipulate files
#include <iostream.h>
#include <io.h>      //Used to check file existence
int main(int argc, char * argv[])
{
  if(argc!=2)
  {
    cout<<"Correct input is: filename";
    return 0;  
  }            
  if(access(argv[1], 00)) //access returns 0 if the file can be accessed
  {     //under the specified method (00)
    cout<<"File does not exist";  //because it checks file existence
    return 0;
  }
  ifstream the_file;  //ifstream is used for file input
  the_file.open(argv[1]); //argv[1] is the second argument passed in
    //presumably the file name
  char x;
  the_file.get(x);  
  while(x!=EOF)  //EOF is defined as the end of the file
  {
    cout<<x;
    the_file.get(x);//Notice we always let the loop check x for the end of  
  }  //file to avoid bad output when it is reached
  the_file.close();  //Always clean up
  return 0;
}


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. It checks this, using the access function, which accepts a file name and an access type, with 00 being a check for existence. This is not a standard C++ function. It may not work on your compiler Then it creates an instance of the file input class,and it opens the second argument passed into main. If you have not seen get before, it is a standard function used in input and output that is used to input a single character into the character passed to it, or by returning the character. EOF is simply the end of file marker, and x is checked to make sure that the next output is not bad.


Lesson 15: Singly linked lists


Linked lists are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary. Specifically, the programmer writes a struct or class definition that contains variables holding information about something, and then has a pointer to a struct of its type. Each of these individual struct or classes in the list is known as a node.

Think of it like a train. The programmer always stores the first node of the list. This would be the engine of the train. The pointer is the connector between cars of the train. Every time the train ads a car, it uses the connectors to add a new car. This is like a programmer using the keyword new to create a pointer to a new struct or class.
In memory it is often described as looking like this:

----------        ----------
- Data   -       >- Data   -    
----------     -  ----------  
- Pointer- - -    - Pointer-  
----------        ----------

Each of the big blocks is a struct (or class) that has a pointer to another one. Remember that the pointer only stores the memory location of something, it is not that thing, so the arrow goes to the next one. At the end, there is nothing for the pointer to point to, so it does not point to anything, it should be set to "NULL" to prevent it from accidentally pointing to a totally arbitrary and random location in memory (which is very bad).

So far we know what the node struct should look like:
struct node
{
  int x;
  node *next;
};
int main()
{
  node *root;  //This will be the unchanging first node
  root=new node; //Now root points to a node struct
  root->next=NULL; //The


Page : << Previous 7  Next >>