18
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

Embed Size (px)

Citation preview

Page 1: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

C++ / G4MICE Course

Session 3

Introduction to ClassesPointers and References

MakefilesStandard Template Library

Page 2: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard 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

Page 3: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 4: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 5: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 6: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 7: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 8: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 9: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 10: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 11: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 12: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 13: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 14: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 15: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 16: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 17: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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

Page 18: C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library

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