Local variables

This is a tutorial about variable scope, a variable is a means to store data within the context of the program. For example, if you wish to store a numerical value then you would not store that number in a string, but within a integer or floating point number.

There are different types of variables, integer / string etc, but the scope of a variable is the area in which the variable is defined within block of code that it is situated.

This is some c# code to demonstrate the difference between local variables and global.

class localvariable
{
       private static int value1 = 1;
 
       private static void global()
       {
              System.Console.WriteLine("Global : " + value1);
       }
 
       public static void Main()
       {
              int value1 = 0;
              System.Console.WriteLine("Local : "  + value1);
              global();
              return;
       }
}

save this as localvariable.cs and then compile up the code and run. The output should be.

Local : 0
Global : 1

and as you can tell there are two value1 within the source code whilst one is the global variable and the other is local to the Main method.

Read and Write to files

This tutorial is about how to read and write from/to a file (textual file).

First of all define the files that are for the input and output filenames.

NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;

string fileName = "country.txt";
string outFileName = "sqlcountry.txt";
 
//make sure that the file is present
if (File.Exists(fileName))
{
 
//Setup the output file object, using a StreamWriter to write to files.
       using (StreamWriter SWriter = new StreamWriter(outFileName))
       {
 
//Setup the input file object, 
       using (StreamReader SReader = new StreamReader(fileName))
       {
 
//The line is the input line from the input file.
       String line;
 
//While assigning the line from the input file is valid
       while ((line = SReader.ReadLine()) != null){
 
//Write to the output file some different text.
       SWriter.WriteLine("insert into country(place) values(\"" + line + "\");");
}

I have attached all of the required files to be able to test this tutorial.

Hello world

This is a classic Hello World, if you are using Windows then there should be included with it .net, which has the .net runtime environment (this is stored in C:\WINDOWS\Microsoft.NET\Framework\v*). If you are using another OS or wish to try out Mono (Try Mono), which is a open source .net compiler and run-time environment.

The language is similar to c++ and also Java. The main aspect is that it envolves from class and because of that System is a class which in-turn has the Console sub class and then the method WriteLine.

The helloworld code example

public class helloworld
{
       public static void Main(string[] args)
       {
              System.Console.WriteLine("Hello world!");
       }
}

save as helloworld.cs

To compile with MS .net

csc helloworld.cs

which creates a executable file to execute.
helloworld.exe
with the output

Hello world!

or within mono

mcs helloworld.cs

and then to run the program you can either just type in helloworld.exe as like MS .net or use the JIT (Just In Time) environment, mint.

mint helloworld.exe

Define – the marco that can save time

The define part of the c/c++ language is able to save on coding time. The define is only really good for a one line of code that a function would be far to much of a over kill.

Here are three examples of the define,

#define MAXVALUE 20

this will allow for a hard coded value to be used within the coding

#define PrintHello cout << "Hello" << endl;

this will print hello when you call the marco as

void main()
{
       PrintHello;
}

and last is passing parameters to the marco

#define PrintWord(word) cout << word << endl;

will change the word parameter within the code to the passed value for example both will work

void main()
{
       PrintWord("hi there");
       PrintWord(3);
}

since the marco will convert the passed parameter to the value.

Operators

An operator syntax is part of the standard C/C++ syntax that allows you to use the +/-/* etc aspects of the objects that are assoicated with them, for example, for two values of an integer you can add these two values together with value1 + value2, the adding part is calculated within the operator and the value is returned.

To code a operator you just need to use the operator () function. Within the code below there are 3 types of operators for the operate test class, to add another integer to the internal value, to increament the internal value and also to logically add the internal value with another integer value.

The code

#include <iostream>
 
using namespace std;
 
class optest
{
public :
       int i ;
       optest() { i = 0; }
       optest(int i2) { i = i2;}
 
       virtual int out() { return i;}
 
       int operator +(int addi)
       {
              return i + addi;
       }
 
       int operator++(int)
       {
              return ++i;
       }
 
       int operator &(int addi)
       {
              return i&addi;
       }
};
 
class optest2 : public optest       // inhert the base class for showing how to use operators within base class and inherited classes
{
public:
       int out() { return i + 10;}
       optest2() : optest() {;}
       optest2(int i) : optest(i) {;}
};
 
int main(int argc, char* argv[])
{
       // setup and demo the results of the operators
       optest op(3);
       optest2 op2(2);
       cout << op.out() << endl; 
       cout << op + 3 << endl;
       cout << op.out() << endl;
       cout << op++ << endl;
       cout << op.out() << endl;
       cout << op2.out() << endl;
       return 0;
}

I have included Microsoft Visual Studio 2005 downloadable edition (here) and also Linux code for this tutorial as a download.

Function Pointers

Function pointers are just like any other type of pointer within C/C++, in that they point to a reference in memory to something that is the reference to the object (a pointer to a integer type that is holding the value 2 is the value of 2 and the pointer just points to this object). Since a pointer can point to any type of object that is similar to the object type, you can also have pointers to functions. This type of pointer is good for having a single function pointer to list of functions that is chosen from a action, for example if there was a list of function for the edit action within the standard document editors (Cut/Copy/Paste/Undo/Redo) and to just point to within every function was required was to use the drop down list item number, then the code is very small and also re-usable.

For example of code with using function pointers.

#include <iostream>
 
using namespace std;
 
void f()
{
       cout << "hi" << endl;       
}
 
void g()
{
       cout << "bye" << endl;
}
 
// always set the function pointer to NULL.
void (*func)() = NULL;
 
// to pass a function pointer to a function.
void callFunc( void (*function)())
{
       function();
}
 
int main(int argc, char* argv[])
{
       // set the func object (as created above – void (*func)() = NULL; - to the function address f)
       func=&f;
       // call the function that is assoicated with the func pointer
       func();
       // reset to another function.
       func=&g;
       func();
 
       // call the function pointed to within another function.
       callFunc(func);
       return 0;
}

I have included Microsoft Visual Studio 2005 downloadable edition (here) and also Linux code for this tutorial as a download.

Vector

An vector is very similar to a array, in that it is able to store data within one variable name. The main difference is that a vector is more of a class structure that allows you do to different tasks to it and also it acts similar to a stack FIFO (First In First Out) theory.

Here is SGI vector functions (SGI Vectors)

The vector allows for a dynamic approach of creating and inserting data onto a seemless endless array of data. When a new element is placed onto the vector (push_back), this creates a new block of memory associated with the vector and the value is attached (you can only attached the value the same as the initial setup of the vector, an vector is able to use classes, variables etc to push/pop of the vector/stack).

This is the example code

#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
       // create a vector of intergers
       vector<int> vectorInt;
       // push_back, inserts onto the top of the list values
       vectorInt.push_back(2);              // insert 2
       vectorInt.push_back(3);              // insert 3
 
       // a iterator is basically a type of object that can reference part of the vector
       vector<int>::iterator iteratorInt;
 
       // display all of the emplements inside the vector
       for (iteratorInt = vectorInt.begin(); iteratorInt != vectorInt.end(); iteratorInt++)
       {
              cout << *iteratorInt << "\n";              // display what the iterator pointers to 
       }
       // display the size of a vector 
       cout << "size = " << vectorInt.size() << "\n";
 
       // the capacity = how big the vector is, since there has been no pull/remove/erase from the vector
       // then it is the same size.
       cout << "capacity = " << vectorInt.capacity() << "\n";
 
       cout << "Last element " << (int)vectorInt.back() << "\n";              // the last element
       vectorInt.pop_back();                            // delete the last element
       cout << "Last element " << (int)vectorInt.back() << "\n";              // the last element now
 
       cout << "capacity = " << vectorInt.capacity() << "\n";       // capacity is still 2
       cout << "size = " << vectorInt.size() << "\n";              // but the size is 1
 
       cout << "max size = " << vectorInt.max_size() << "\n";       
       return 0;
}

if you save the code as vectorcpp.cpp, then compile and run, the output will be

2
3
size = 2
capacity = 2
Last element 3
Last element 2
capacity = 2
size = 1
max size = 1073741823

and as you can see the with removing the last element from the list (number 3), the size of the vector is reduced, but the capacity is the same since that is how much had been assigned to the vector.

Note : the (int) in front of the vectorInt.back(), will decast the return value into a int(eger) value for the standard console output (std::cout).