Object-oriented Programming in C++

Object-oriented Programming in C++ Object Orientation in C++ Wolfgang Eckhardt, Tobias Neckel March 24, 2015 Wolfgang Eckhardt, Tobias Neckel: Object...
Author: Moses McKenzie
1 downloads 1 Views 343KB Size
Object-oriented Programming in C++ Object Orientation in C++ Wolfgang Eckhardt, Tobias Neckel March 24, 2015

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

1

1 C: Repetition & Best Practices

todo check

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

3

Syntax vs. Semantics • Syntax = rules for the constructing of sentences in the

languages •

The syntax is (more or less) prescribed by C.

• Semantics = meaning of sentences, words, or fragments. •

The semantics is what you had in mind, i.e. the reader has to reconstruct it from your program.

• Best practices: • • •

Use meaningful names! Add documentation, documentation, and documentation! If code is not documented, it does not exist!

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

4

Functions—Some Best Practices 17 18 19

int sign ( int a ) { // return sign }

20 21 22 23

double change_sign ( double b ) { // change sign and return new value }

24 25 26 27

int main ( int argc , char * argv []) { // do something : change signs change_sign (5.3) ;

28

return 0;

29 30

} code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/MPE_2015/src/basics_c/functions3.cpp

• One purpose, one function: Any function should concentrate

solely on one job. • One page, one function: With a less, one should be able to see

the complete function. • A function without documentation does not exist! • Functions should have meaningful names (verbs). Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

5

const Modifier We can mark any argument to be a const argument, i.e. the function is not allowed to modify it. 1 2 3

void foo ( const i n t a ) { ... } • const allows compiler to optimise code aggressively. • const allows you to write bug-safe code (user cannot modify a

variable by accident). • const closes (some of) the performance gap between C / C ++

and FORTRAN.

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

6

1.1 Signature of a Function 1 2 3 4 5

int int int int int

foo ( ) bar ( ) foo ( i n t a ) f o o ( double x ) f o o ( i n t a , c o n s t double b , double x )

• A function is unique

due to its name, and due to the number of arguments, and • due to the type of the arguments, and • due to the const modifiers of its arguments • Everything besides the name: signature. • •

• name + signature ⇒ function unique. • Attention: return value does not come into play! Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

7

Overloading 1 2 3

i n t foo ( ) i n t f o o ( i n t a ) {} i n t f o o ( double a ) {}

4 5

...

6 7 8 9

foo ( ) ; foo ( 3 . 4 ) ; foo ( 4 ) ; • overload a function = offer it with different signatures. • C / C ++ then automatically tries to find the right signature. • Be aware of automatic type conversions!

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

8

C++ const References 1 2 3

void foo ( i n t a , i n t b ) { ... }

4 5 6 7

v o i d bar ( i n t & a , i n t & b ) { ... }

8 9 10 11

void e f f i c i e n t F o o ( const i n t & a , const i n t & b ) { ... } • const references avoid side effects! • (const) references belong to the signature ⇒ overloading!

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

9

Best Practices: Pointers vs. References 1 2 3 4

5

void foo ( i n t * a ) { ( * a ) ++; . .. a ++; / / That c o u l d n o t have happened w i t h Ç references }



• Pointers are difficult to handle (lots of syntactic overhead). • We know how to do it, but do collaborators know? • Initiate pointer always with NULL (avoid 0 due to implementation

dependencies in handling) • Avoid it whenever possible. Use C++ references instead. • References are always initiated (per definition). • References cannot be changed. Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

10

2 The Concept of Object Orientation

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

12

2.1 Definition of Objects What Is an Object? • a car • a cat • a chair • ... • a molecule • a star • a galaxy • ... • anything that represents a real thing • anything that represents an abstract thing • anything which has some properties Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

13

How to Program Object-orientedly

• Change your way of thinking • Change your point of view • It is not just a technical question! • Good designs requires considerable experience!

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

14

2.2 Classes vs. Objects Classes • A class is similiar to the definition of a type • Specifies properties of the objects to be created • Data to be stored once for all objects (class/static variable) • Data to be stored in each object (member variables) • Operations to be done with objects (member functions/methods) • Represents a class of things/objects Instances / Objects • An instance/object is a concrete thing of the type of a class • A class specifies properties, an object has specific values for the specified properties ⇒ state of an object • For each class, there can be an arbitrary number of objects • Each object belongs to exactly one class (exception: polymorphism, day 3) Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

15

Object: Definition

Oracle, The Java™ Tutorials

Definition • State: the set of values of member variables/fields (built-in datatypes and other objects) at a specific point in time • Behaviour: Member functions/methods operate on the internal state of the object, primary mechanism for object-to-object communication Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

16

Object: Example

Oracle, The Java™ Tutorials

Example: Bike • State: Speed, Revolutions per Minute, Gear, .. • Behaviour: Change gears, change cadence, brake, .. Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

17

2.3 Why Object Orientation? Pros • Modularity: Implementation of different objects usually written

independently. • Information hiding: Internal implementation remains hidden

from the outside world (can be changed easily even for heavy used objects) • Code reuse: • Object (re-)use: you only have to understand the member-functions visible to you (interfaces in software projects and/or libraries); data & functions grouped together! • Inheritance (details below) Cons • Performance: Wrong usage of OO-language constructs might

come with a significant overhead. • Code complexity: Be careful: unclever OO design ⇒ problems

in maintenance, extensibility, etc.) Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

18

3 Objects in C ++ Bikes as Objects • Task: Model a bike in C ++ • Goal: Learn the C ++ syntax on the way

Design Gear Shift

• State: current gear, #gears • Behaviour: increase gear, decrease gear, get

current gear Bike

• State: current speed, revolutions per minute, gear

shift • Behaviour: change gear, brake, change cadence

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

20

3.1 Overview: Bikes as Objects Design Gear Shift

• State: current gear, #gears • Behaviour: increase gear, decrease gear, get

current gear Bike

• State: current speed, revolutions per minute, gear

shift • Behaviour: change gear, brake, change cadence

Overview Next Skeleton (header file) of the bike class with member variables, member functions and access control.

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

21

Bike.h - The class Keyword 17 18

# ifndef BIKE_H_ # define BIKE_H_

19 20 21

# include < cmath > # include < iostream >

22 23

# include " GearShift . h "

24 25

class Bike {

72

};

73 74

# endif // BIKE_H_ code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/MPE_2015/src/objects/bike/Bike.h

Keyword class defines a class in C ++ ⇒ frame for OO concepts (member variables, member functions, inheritance, . . . )

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

22

Bike.h - State: Member Variables 25 26 27 28

class Bike { // private : // ! current speed float m_speed ;

29

// ! current rpm float m_revolutions ;

30 31 32

// ! gear shift GearShift m_gearShift ;

33 34

72

}; code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/MPE_2015/src/objects/bike/Bike.h

State Member variables define the state of an object. Member variable can be built-in or custom objects.

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

23

Relationships between Objects: ”Part-Of”

Part-Of releationship Object A is with object B in an part-of releationship, if A is part of B (A is one of the parts the machinery of B is assembled of). Example: Object ”gear shift” can be modeled in a part-of relation to the object ”bike”. Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

24

”Object Member Variables” model ”Part-Of”

Object Member Variables OO-programming: allows objects to be build of other objects not only built-in data-types ⇒ allows for a modular implementation and code reuse Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

25

Bike.h - Behaviour: Member Functions 25

class Bike { public : /* * * Constructor * * @param i_numberOf Ge a rs # gears * */ Bike ( int i_numberO fG ea r s ) ;

36 37 38 39 40 41 42 43

/* * * Destructor * */ ~ Bike () ;

44 45 46 47 48

/* * * Changes the gear to the closest possible gear . * * @param i_gear gear to change to . * */ void changeGear ( int i_gear ) ;

49 50 51 52 53 54

72

}; code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/MPE_2015/src/objects/bike/Bike.h

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

26

Bike.h - Behaviour: Member Functions (2) 25

class Bike { public :

36

/* * * Brakes the bike . * */ void brake () ;

56 57 58 59 60

/* * * Accelerates the bike . * * @param i_rpm revolutions per minute . * */ void changeCadence ( float i_rpm ) ;

61 62 63 64 65 66

72

}; code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/MPE_2015/src/objects/bike/Bike.h

Behaviour Member functions: mutator methods: change internal state of an object (often setXXX()) Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

27

Bike.h - Behaviour: Member Functions (3)

25

class Bike { public :

36

67

/* * * Prints status of our bike . * */ void printInformati o n () const ;

68 69 70 71 72

}; code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/MPE_2015/src/objects/bike/Bike.h

Behaviour Member functions: accessor methods give access (read-only) to the state of an object (often getXXX())

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

28

Bike.h - Access Control 25 26 27 28

36 37 38 39 40 41 42

class Bike { // private : // ! current speed float m_speed ; public : /* * * Constructor * * @param i_numberOf Ge a rs # gears * */ Bike ( int i_numberO fG ea r s ) ;

Accessibility • Restrictive access control: helps to write clean code & uses

compiler to enforce interfaces. ⇒ information hiding! • private: Members • public: Anyone Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

29

Information Hiding

Users/developers have restricted access to objects/classes ⇒ avoid side effects! (you don’t want a user to change the gravity constant of the earth, etc.) ⇒ reduce code complexity in usage (you only need to care about what you can see) ⇒ reduce code complexity in development (accessor methods, e.g.: rename a member variable)

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

30

3.2 Overview: Bikes as Objects Design Gear Shift

• State: current gear, #gears • Behaviour: increase gear, decrease gear, get

current gear Bike

• State: current speed, revolutions per minute, gear

shift • Behaviour: change gear, brake, change cadence

Overview Previous Skeleton (header file) of the Bike class with member variables, member functions and access control. Next Implementation (source file) of the Bike class & runner.

Wolfgang Eckhardt, Tobias Neckel: Object-oriented Programming in C++ Object Orientation in C++, March 24, 2015

31

Bike.cpp - Constructor 17

# include " Bike . h "

18 19 20 21 22 23 24 25

Bike :: Bike ( int i_numbe rO f Ge ar s ) : m_speed (0) , m_revolutions (0) , m_gearShift ( i_numberO f Ge ar s ) { std :: cout