Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Classes
Classes
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.
Classes
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
Classes
Classes
Classes
Classes
Classes
Classes
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.
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
Classes
Scalar Product
Add the operation double getAngle( const Vector & rhs) to Vector. This
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.
Classes
Scalar Product
Add the operation double getAngle( const Vector & rhs) to Vector. This
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.
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).
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
Classes
7.1. Classes
With the new technique at hand, we can encapsulate data and operations as
Classes
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
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 .
Classes
Classes
Classes
This is not recursion, as the operation is invoked on another object. However, recursion and object-based programming work together.