Title: Introduction to Object-Oriented Programming in C
1Introduction to Object-Oriented Programmingin
C
- Vincenzo Innocente
- CERN, Geneva, Switzerland
2Classes and Objects
- An example of object oriented Programa
video-game
3Class Vector
- An example un tri-dimentional vector
- With respect to a structure, a class contains
functions beside data
constructor
functions or methods
data
copy constructor
4Class Vector
invoke the constructor
5Class Vector
6Interface and implementation
- Protected data are not accessible outside the
class
Vector.hh
class Vector public Vector(double x, double
y, double z) double x() const double y()
const double z() const double r() const
double phi() const double theta()
const protected double _x, _y, _z
const
Vector.cc
include Vector.h VectorVector(double x,
double y, double z) _x(x), _y(y),
_z(z) double Vectorx() const return _x
double Vectorr() const return sqrt(xx
yy zz)
7Interface and implementation
- The internal structure of the data (_x, _y, _z)
that represent the objects of the class Vector
are hidden (protected) to the clients of the
class. - The clients do not depend on the internal
structure of the data (as it was the case for
clients of FORTRAN common block) - If the internal structure changes (es. _r,
_theta, _phi), the code using Vector does not
have to be modified.
8Operators
- It is possible to redefine , -, , , , , .
. .
9Operators
redefinition of ltlt
10Operators
p
11Operator overloading
- It is possible to define functions with the same
name but different arguments
12Static Functions and data
- Some functions can be common to the whole class
13Templates
- The same definition can be applied to different
types
templateltclass Tgt T sqr(const T x) return x x
templateltclass Tgt T min(const T a, const T b)
return (a lt b ? a b) templateltclass Tgt T
max(const T a, const T b) return (a gt b ? a
b)
recursive
templateltclass Tgt T fact(const T n) return
(n 0 ? 1 n fact(n -
1))
14Inheritance
- Add methods and attributes to a class
Invoke the constructor of the base class
- Inheritance allows code re-use
15Inheritance
- A derived class inherits all methods fo the base
class
- DchTrack is a Track with more attributes (_hits)
and new methods (DchHit hit(int n), int hits())
16Abstract Class
- All objets in the window have common behaviors
which could be considered in abstract - draw, move, enlarge, etc.
17Abstract classes and virtual function
- Shape is an abstract class because it has pure
virtual methods
Pure virtual methods
18Abstract classes and virtual functions
- Shape clients do not have to know which of its
derived classes actually implement it.
19Inheritance
- Careful choosing the correct inheritance
relations could not be trivial. - Is a square a rectangle?
- both_lx and _ly are redundant in Square
- What happen if scaleX or scaleY is invoked?
20Multiple Inheritance
- A class can inherit from more than one class
21Surfaces and trajectories
- In tracking is often required to compute the
intersections among curves (tracks) and
superfaces (detector elements)
Intersection(Surface, Trajectory)
Trajectory
Surface
Line
Helix
PolyLine
Plane
Cylinder
etc . . .
etc . . .
22Surfaces and trajectories
- Interface of various Trajectorys
23Surfaces and trajectories
24Surfaces and trajectories
- Interface of the various Surfaces
distance (with sign) of a point form the surface
25Surfaces and trajectories
- Surface is an abstract class
scalar product between two Vectors
26Surfaces and trajectories
- Interface of Intersection
forward class declarations
27Surfaces and trajectories
- Implementation of the algorithm
28Surfaces and trajectories
- Intersection uses only
- The methods position and direction of a
Trajectory object - The methods distance and derDist of a Surface
object - It is possible to add a new class that models a
new Trajectory or a new Surface and Intersection
continues to work without modifying a single line
of code!
29Shapes Visitors
30Shapes Visitors
31Shapes Visitors
32Open/Closed principle (R. Martin)
- A good code has to be
- open to extensions
- closed to modifications
- Modifications of a working code can introduce
defects (bugs) - Object Orientation, with the mechanism of virtual
classes, allows to apply this principle