Class structure

This is a general tutorial about the class structure of programming languages.

The class is basically an object that allows public methods (accessed outside that class object) and private methods (the internal class logic) and protected with is the same as private apart from an inherited class can access the protected objects. Basically the 3 main types

Public : Access from outside the class, and internal of course, with inherited/friends etc.
Protected : Access from inherited/friends of the class, but not called from the class object.
Private : Access from only the class and friends of the class.

For example within C# coding …

class example1
{
       private int value1;
       private int value2;
       protected int value3;
 
       public int addtwo(int a, int b)
       {
              value1 = a;
              value2 = b;
              value3 = (a+b);              // setup the local variables
              return value3;
       }
 
       public int returnValue()
       {
              return value1;
       }
}
 
class example2 : example1
{
       public int returnValue2()
       {
              return value2;       // will error due to value2 is private in the inherited class
              //return 0;              // to be able to compile
       }
 
       public int returnValue3()
       {
              return value3;       // fine since a protected part of the inherited class
       }
}
 
class examples 
{
       static public void Main()
       {
              example1 ex = new example1();
              example2 ex2 = new example2();
              // add the two numbers and set the local internal variables
              System.Console.WriteLine("Two numbers 3 + 4 = " + ex.addtwo(3,4));
              // output the value1 from the internal example1 class
              System.Console.WriteLine("Value1 = " + ex.returnValue());
 
              // Just for demo'ing, since the example2 is not linked to example1, just inherites it.
              System.Console.WriteLine(ex2.returnValue2());       // will not compile the class above.
              System.Console.WriteLine(ex2.returnValue3());
              return;
       }
}

save as classexample.cs. The output will be

Two numbers 3 + 4 = 7
Value1 = 3
0
0

The two zero’s are because class example2 only inherits the example class and not does not link to it. I have added the line within the example2 returnValue2 method to be able to compile the source code otherwise you will get the error similar to

classexample.cs(25,10): error CS0122: example1.value2 is inaccessible due to its protection level

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.