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).

Array

Arrays are ways of having a block of memory allocated to a single variable type, this allows for holding the national lottery numbers within an array of 6 instead of actually having 6 different variables.

e.g.

int value1 = 1;
int value2 = 2;
int value3 = 3;
int value4 = 4;
int value5 = 5;
int value6 = 6;

and instead you could just have

int values[6] = {1,2,3,4,5,6};

this is a example of how to manipulate the array structure.

#include <iostream>
 
int main()
{
       // array structure
       int values[5];       // set asside 5 blocks of int(eger) size of continuous memory blocks
                     // the actual points into the array are 0-4 (e.g. 5 points but start at 0)
 
       int values2[6] = {0,1,2,3,4,5}; // exmaple of settings the values on define time.
 
       // direct assigning the values.
       values[0] = 5;
       values[1] = 4;
       values[2] = 3;
       values[3] = 2;
       values[4] = 1;
 
       //could use a position pointer
       for (int i = 0; i < 5; i++)
       {
              // display the values
              std::cout << "Value " << i << " : " << values[i] << "\n";
       }
 
       int *int1p = &values[0];
       std::cout << "Pointer value = " << *int1p << "\n";
       int1p++;              // increament the pointer to the next part of the array
       std::cout << "Pointer value = " << *int1p << "\n";
       return 0;
}

save as arrays.cpp and then compile the program, the result once executed will be

Value 0 : 5
Value 1 : 4
Value 2 : 3
Value 3 : 2
Value 4 : 1
Pointer value = 5
Pointer value = 4

and include three different ways to access the values within the array.

Templates

Templates, Templates, Templates. A template is a way to declassify the type of variable used, e.g. if there was a function to add up two integer values, but you had two double values, then the int function would not function correctly, so there would be a need to function overload the function to allow for the double increment as well. But just envisage that the same function would be required for characters, floats, personally created ones etc, then there would be more functions per type of variable than the actual work done.

A template negates this by allowing any class type passed to the function, as long as the type has the standard increment process defined, it will work with any type.

The syntax for a template is

template <class (template variable name) > (return type) function name (parameters)

Hopefully the code will help in the explanation.

#include <iostream>
 
// the definition is similar to a function, (return type) (function name) (parameters...) instead
// it has a template <class (name of template)>
template <class T> T TemplateAdd(T a, T b)
{
              return (a+b);              // use the internal adding for the variable of type T
              // e.g if defined as a int, then use int add internal, or if a double, use a double etc.
}
 
// template class, same as a function apart from a class 
template<class T> class TemplateClass
{
private:
       T value;       // the internal value
public : 
       TemplateClass(T va)       // set the default internal value in the constructor
       {
              value = va;
       }
 
       T ReturnValue()              // return the default value, all using the template class
       {
              return value;
       }
};
 
int main()
{
       // standard template for a function, pass in two integer values, and also two doubles, it will both work the same
       std::cout << "Adding " << TemplateAdd(3,1) << " " <<  TemplateAdd(3.4, 5.4) << "\n";
 
       // create a int class with the default value of 3
       TemplateClass<int> intTemplate(3);
       std::cout << intTemplate.ReturnValue() << "\n";
 
       // using the same constructor, create a char class with the default value 'b'
       TemplateClass<char> charTemplate('b');
       std::cout << charTemplate.ReturnValue() << "\n";
 
       return 0;
}

Save as templates.cpp, then compile the program. The result displayed will be
Adding 4 8.8
3
b

So using the templates will cut down on the standard function/class types but you do need to be careful to not use any of the non internal class calls within the template, e.g. if you create a new type and do not proved a function to increment the values this will not work.

Just a note that you could add in more different types of templates by
template (return value) (function name) (parameters , e.g. S input1, T input2)

Friends

A friend of a class allow access to the private members of that class, because there are some cases where this is required when the whole of the created class is required to be viewable, e.g. when a private member is not being able to set with the class methods.

To declare a friend within a class, for example to declare a function / class as a friend.

friend int FuncToFriendClass(int a);              // friend to the function.
friend class ClassToClassFriend;              // friend to a class

This will allow the function FuncToFriendClass to gain access to all objects within the class, and the class ClassToClassFriend as well.

The source code

int FuncToFriendClass(int a);              // declare the function, for the class to make a friend.
 
class FriendlyClass 
{
private:
       int private_data;
 
       friend int FuncToFriendClass(int a);              // friend to the function.
       friend class ClassToClassFriend;              // friend to a class
public:
       FriendlyClass()
       {
              private_data = 5;
       }
};
 
int FuncToFriendClass(int a)
{
       FriendlyClass friendClass;                            // the friend to this funciton.
       return friendClass.private_data + a;
}
 
class ClassToClassFriend
{
public:
       int FriendClassPrivateDataMinus(int b)
       {
              FriendlyClass classFriend;
              return classFriend.private_data - b;
       }
};
 
int main()
{
       ClassToClassFriend classFriend;
       std::cout << "Friendly function to class =" << FuncToFriendClass(3) << "\n";
       std::cout << "Friendly class to class = " << classFriend.FriendClassPrivateDataMinus(2) << "\n";
       return 0;
}

If you save that as friend.cpp and then compile, the output will be
Friendly function to class = 8
Friendly class to class = 3

Pointers

Pointers are pointers to memory, the variable that is defined as a pointer still holds the same area in memory as the type of the pointer e.g. char and int are of different size in memory, just do a

cout << sizeof(char) << sizeof(int);

to find out.

The actually pointer holds the address of the variable it is pointing to, e.g.

The value variable

Value = 10, address within the memory for value = 4

The pointer

ValuePointer = 4 (the address of the value), address within the memory = 200

The valuepointer actually equals 4, the memory of the value variable, if you use the * (pointer) then you will get the value pointed to, 10, and if you use the & (address) you will get 200 which is the place in memory where the valuepointer is placed in memory.

For example

int main()
{
int value1 = 10;
int *valp = &value1;       // equals the memory address of value1
 
std::cout << "Value 1 = " << value1 << " address = " << &value1 << "\n";
std::cout << "Value p = " << *valp << " address = " << &valp << " pointed to memory " << valp << "\n";
std::cout << "The value p pointerd to memory is the same as the memory address as value 1\n";
std::cout << "The value p = the pointerd memory value that is assoicated with the value that is in valp (address of memory)\n";
 
(*valp)++;              // correct increament the value that is pointed to by the address held by vapl
std::cout << "Value 1 = " << value1 << " address = " << &value1 << "\n";
std::cout << "Value p = " << *valp << " address = " << &valp << " pointed to memory " << valp << "\n";
 
*valp++;              // incorrect will increament the address that is held valp by sizeof(int) (usually 4)
std::cout << "Value 1 = " << value1 << " address = " << &value1 << "\n";
std::cout << "Value p = " << *valp << " address = " << &valp << " pointed to memory " << valp << "\n";
 
return 0;
}