Introduction to Object-Oriented Programming in C - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Object-Oriented Programming in C

Description:

double r(); double phi(); double theta(); protected: double _x, _y, _z; Vector.h ... double r() const; double phi() const; double theta() const; ... – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 33
Provided by: lis104
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Object-Oriented Programming in C


1
Introduction to Object-Oriented Programmingin
C
  • Vincenzo Innocente
  • CERN, Geneva, Switzerland

2
Classes and Objects
  • An example of object oriented Programa
    video-game

3
Class 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
4
Class Vector
  • How to use Vector

invoke the constructor
5
Class Vector
  • Data access protection

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

8
Operators
  • It is possible to redefine , -, , , , , .
    . .

9
Operators
  • Example

redefinition of ltlt
10
Operators
  • Example

p
11
Operator overloading
  • It is possible to define functions with the same
    name but different arguments

12
Static Functions and data
  • Some functions can be common to the whole class

13
Templates
  • 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))
14
Inheritance
  • Add methods and attributes to a class

Invoke the constructor of the base class
  • Inheritance allows code re-use

15
Inheritance
  • 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())

16
Abstract Class
  • Classic example Shape
  • All objets in the window have common behaviors
    which could be considered in abstract
  • draw, move, enlarge, etc.

17
Abstract classes and virtual function
  • Shape is an abstract class because it has pure
    virtual methods

Pure virtual methods
18
Abstract classes and virtual functions
  • Shape clients do not have to know which of its
    derived classes actually implement it.

19
Inheritance
  • 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?

20
Multiple Inheritance
  • A class can inherit from more than one class

21
Surfaces 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 . . .
22
Surfaces and trajectories
  • Interface of various Trajectorys

23
Surfaces and trajectories
  • Implementation

24
Surfaces and trajectories
  • Interface of the various Surfaces

distance (with sign) of a point form the surface
25
Surfaces and trajectories
  • Surface is an abstract class

scalar product between two Vectors
26
Surfaces and trajectories
  • Interface of Intersection

forward class declarations
27
Surfaces and trajectories
  • Implementation of the algorithm

28
Surfaces 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!

29
Shapes Visitors
  • Shapes

30
Shapes Visitors
  • Visitors

31
Shapes Visitors
  • Main

32
Open/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
Write a Comment
User Comments (0)
About PowerShow.com