Upload
julius-tucker
View
216
Download
2
Embed Size (px)
Citation preview
C++ / G4MICE Course
Session 3
Introduction to ClassesPointers and References
MakefilesStandard Template Library
Classes• A class is a way of making a new variable
type that is very powerful.
• It does not just contain data, but also comes with functions (methods) that can be called.
• This allows us to define a new type for a 3 vector (for example) have it do more than just store 3 numbers, it can tell you the magnitude, direction, dot product with another vector, etc, etc.
2
Constructor• When you make a new int or double the
compiler just finds sufficient memory and if you have set an initial value in the code, that value is copied to this new memory location.
• A class is more sophisticated and so we have the ability to specify exactly what should happen when an instance of our class is created.
• This code is called the constructor.
3
Destructor
• Similarly, when a simple type is no longer needed that memory is freed up, but nothing else is done.
• In a class, we can define specific code that should happen just as the instance of that class is being destroyed.
• This code is called the destructor.
4
Functions and Variables• In addition to the constructor and
destructor, a class can have any number of functions.
• It can also have variables of either the basic types, or of other classes.
• We typically store internal information in the variables and then provide functions (often called methods) that use this information to perform whatever tasks the class is meant to do.
5
Public and Private
• Methods and variables can be declared as being public or private.
• If something is public, it means that code outside of the class can see it and call it (if it is a function) or use it in an expression (if it is a variable).
• If something is declared to be private, then it is only visible to other code that is in that class.
6
Methods and Variables
• What we usually do is to have the variables that are part of a class private.
• We then make some (or all) of the methods of the class public.
• This means that the code that uses a class can call a well defined method that should perform a certain task without any need to know how that task is performed.
7
A Very Simple Classclass MyClass
{
public :
MyClass() {}; // this is the constructor
~MyClass() {}; // this is the destructor
void increment() { val++; } // this function increments the value
void decrement() { val--; } // this function decrements the value
int value() { return val; } // this function returns the value
private :
int val;
};
8
Inline Functions
• In the previous example, the code for each function was provided in the class declaration.
• This is known as an inline function.
• It is a good idea to do this when the function is very short.
• For more complicated code, it should be compiled separately (we will see this shortly).
9
Simple Class Example
• Download the file simpleClass.cc and compile it.
• Run it and check that you understand what is happening.
• Uncomment the line that attempts to use the variable _val and try to compile.
10
Pointers
• Pointers are effectively a different type and refer to the address in memory of a variable.
• double x = 5;
• double* y = & x;
• y holds the address in memory where the variable x is located.
• You can de-reference a pointer using * e.g. std::cout << *y << std::endl;
11
References
• Are like pointers, except:– They do not need to be dereferenced– They must always refer to something (i.e.
they cannot be NULL, like a pointer!)
• A reference IS whatever it is referring to.
• double x = 5;
• double& y = x;
• y = 9;
12
Pointers & References
• Download the pointers.cc and references.cc files and compile them.
• Run them and check that you understand what is happening.
• In the pointers.cc file, uncomment the code at the end, recompile and see what happens when you run.
13
Makefiles• When we start making G4MICE
applications, we will use a tool that generates Makefiles for us.
• For now, I will provide some Makefiles for the remaining exercises.
• A Makefile allows you to specify what actions are required to compile the code and how one file depends on another.
• Use the example Makefile for this session.
14
STL
• Standard Template Library:– http://www.sgi.com/tech/stl/
• We have already seen one class from the library, string.
• Another useful tool from the STL is the vector template.
• This template allows you to create a vector (array) of any type.
15
vector• std::vector<int> numbers;• Defines a vector (container) of integers
called numbers.• size() – return the number of elements in the
vector• [] – refers to the ith element (numbering
starts at 0, e.g. numbers[10]• push_back( x ) – add an element to the end
of the vector.• http://www.sgi.com/tech/stl/Vector.html
16
Vector Example
• The Makefile should already have built the vector example for you (if you used the tarball).
• If not, download the code and compile it.
• Run the vector example to understand how the vector template works.
17
More STL
• We don’t have enough time to go into the STL in any detail, but it is worth spending some time to learn what it can provide.
• There are many different templates that provide useful ways of storing information as well as algorithms such as sorting routines.
18