Sei sulla pagina 1di 23

Classes

7. OO Revisited June 15, 2010

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 1 of 14

Classes

Create a Struct Vector


Create a struct Vector with three doubles and an additional double holding the

length of the vector.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 2 of 14

Classes

Create a Struct Vector


Create a struct Vector with three doubles and an additional double holding the

length of the vector.


s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; };

Create three vectors a = (0.2, 0.4), b = (1.0, 0.0), and c = (1.2, 0.0) in your

main routine. The rst two instances shall be variables, the latter instance shall be created on the heap.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 2 of 14

Classes

Create a Struct Vector


Create a struct Vector with three doubles and an additional double holding the

length of the vector.


s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; };

Create three vectors a = (0.2, 0.4), b = (1.0, 0.0), and c = (1.2, 0.0) in your

main routine. The rst two instances shall be variables, the latter instance shall be created on the heap.
Vector a, b; V e c t o r c = new V e c t o r ; / / a s s i g n v a l u e s now

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 2 of 14

Classes

Compute the Length


Write an operation void computeLength( Vector & vector) that computes the

length of the passed vector and sets it accordingly.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 3 of 14

Classes

Compute the Length


Write an operation void computeLength( Vector & vector) that computes the

length of the passed vector and sets it accordingly.


# i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; }; v o i d computeLength ( V e c t o r& v e c t o r ) { vector . length = std : : sqrt ( vector . x [0] vector . x [ 0 ] + vector . x [ }

Call this operation in your main for a and c .

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 3 of 14

Classes

Compute the Length


Write an operation void computeLength( Vector & vector) that computes the

length of the passed vector and sets it accordingly.


# i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; }; v o i d computeLength ( V e c t o r& v e c t o r ) { vector . length = std : : sqrt ( vector . x [0] vector . x [ 0 ] + vector . x [ }

Call this operation in your main for a and c . computeLength ( a ) ; computeLength ( c ) ;

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 3 of 14

Classes

Methods Instead of Functions


Make void computeLength( Vector & vector) a method of Vector.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 4 of 14

Classes

Methods Instead of Functions


Make void computeLength( Vector & vector) a method of Vector. # i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; v o i d computeLength ( ) ; }; v o i d V e c t o r : : computeLength ( ) { length = std : : sqrt ( x [0] x [ 0 ] + } a . computeLength ( ) ; c >computeLength ( ) ; Add an operation toTerminal() writing the vector to the terminal. x [1] . . . ) ;

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 4 of 14

Classes

Methods Instead of Functions


Make void computeLength( Vector & vector) a method of Vector. # i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; v o i d computeLength ( ) ; }; v o i d V e c t o r : : computeLength ( ) { length = std : : sqrt ( x [0] x [ 0 ] + } a . computeLength ( ) ; c >computeLength ( ) ; Add an operation toTerminal() writing the vector to the terminal. void Vector : : toTerminal ( ) { std : : cout < < ( < < x [0] < < , < < ...; } x [1] . . . ) ;

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 4 of 14

Classes

Scalar Product
Add the operation double getAngle( const Vector & rhs) to Vector. This

operation shall do two things: Print object for which it is called to terminal. Print argument rhs to terminal. Return 0.0. Invoke this operation for the variables a and c and print the result to the terminal.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 5 of 14

Classes

Scalar Product
Add the operation double getAngle( const Vector & rhs) to Vector. This

operation shall do two things: Print object for which it is called to terminal. Print argument rhs to terminal. Return 0.0. Invoke this operation for the variables a and c and print the result to the terminal.
# i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; double getAngle ( c o n s t V e c t o r & r h s ) ; }; double V e c t o r : : getAngle ( c o n s t V e c t o r & r h s ) { toTerminal ( ) ; rhs . toTerminal ( ) ; return 0.0; } ... a . getAngle ( c ) ; / / or c >getAngle ( a ) ;
page 5 of 14

7. OO Revisited

Introduction to C/C++, Tobias Weinzierl

Classes

Scalar Product
Add the operation double getAngle( const Vector & rhs) to Vector. This

operation shall do three things:


Print object for which it is called to terminal. Print argument rhs to terminal. Compute angle between object and rhs due to the value of length and return it.

Invoke the operation for a and b.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 6 of 14

Classes

Scalar Product
Add the operation double getAngle( const Vector & rhs) to Vector. This

operation shall do three things:


Print object for which it is called to terminal. Print argument rhs to terminal. Compute angle between object and rhs due to the value of length and return it.

Invoke the operation for a and b.


# i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double l e n g t h ; double getAngle ( c o n s t V e c t o r & r h s ) ; }; double V e c t o r : : getAngle ( c o n s t V e c t o r & r h s ) { toTerminal ( ) ; rhs . toTerminal ( ) ; r e t u r n x [ 0 ] rhs . x [ 0 ] + . . . }

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 6 of 14

Classes

Scalar Product
Add a constructor to Vector which accepts three doubles and automatically

ensures that length holds the right value. Do not use initialisation lists (makes it a little bit simpler).

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 7 of 14

Classes

Scalar Product
Add a constructor to Vector which accepts three doubles and automatically

ensures that length holds the right value. Do not use initialisation lists (makes it a little bit simpler).
# i n c l u d e <cmath> s t r u c t Vector { . .. V e c t o r ( double x1 , double x2 , double x3 ) ; }; V e c t o r : : V e c t o r ( double x1 , double x2 , double x3 ) { x [ 0 ] = x1 ; . . . computeLength ( ) ; }

Make your struct a class and try to manipulate the attribute length of a manually

within the main function.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 7 of 14

Classes

7.1. Classes
With the new technique at hand, we can encapsulate data and operations as

these two things go hand in hand.


We can write a couple of setter and getter operations to allow the user to

manipulate our brand new data structure.


However, the user still can reset attributes manually. We can not forbid this. Consequently, we need an alterantive, a new technique to forbid this. Furthermore, it would be nice if the user doesnt even see the attributes, as it might be reasonable that the user cant even read attributes if we dont provide

the corresponding getters (next pointer in our list example, e.g.).

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 8 of 14

Classes

Our Beloved Colleagues


/ Represents a date . Each month s h a l l have 30 days . / s t r u c t Date { i n t month , day ; v o i d switchToNextDay ; };

/ / t h i s i s t h e code our c o l l e a g u e wrote Date myDate ( . . . ) ; myDate . day ++;

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 9 of 14

Classes

Encapsulation
/ Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; public : v o i d switchToNextDay ( ) ; };

In principle, class is an alias for struct, i.e. we can do all the things we can do with structs with classes, too. However, for classes we can create public and private sections. Only add them

in the header.
Private attributes and operations are not available from outside, but only within the

operations of the class (encapsulation).

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 10 of 14

Classes

Encapsulation at Work
/ Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; public : v o i d switchToNextDay ( ) ; };

... v o i d Date : : switchToNextDay ( ) { day ++; / / o . k . } Date myDate ; myDate . day ++; / / doesn t work .

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 11 of 14

Classes

Encapsulation and the Object Lifecycle


/ Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; Date ( ) ; public : v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; };

We can make constructors private and, thus, forbid everybody to create an

instance of our object.


We could make the destructor private, too. However, this never makes sense.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 12 of 14

Classes

Remark: Classes and Namespaces


namespace c a l e n d a r { / Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; Date ( ) ; public : v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; }; } v o i d c a l e n d a r : : Date : : switchToNextDay ( ) { ... } / / f u l l y q u a l i f i e d argument here n o t necessary / / we can access p r i v a t e arguments o f o t h e r i n s t a n c e s v o i d c a l e n d a r : : Date : : copyFromOtherDate ( c o n s t c a l e n d a r : : Date& date ) { ... }

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 13 of 14

Classes

Remark: Classes and Recursion


class IntegerEntry { private : int value ; I n t e g e r E n t r y next ; public : v o i d append ( I n t e g e r E n t r y newEntry ) ; }; v o i d I n t e g e r E n t r y : : append ( I n t e g e r E n t r y newEntry ) { i f ( n e x t ==0) { n e x t = newEntry ; } else { next >append ( newEntry ) ; } }

This is not recursion, as the operation is invoked on another object. However, recursion and object-based programming work together.

7. OO Revisited Introduction to C/C++, Tobias Weinzierl page 14 of 14

Potrebbero piacerti anche