07 oo exercise

Upload: anidcohen9058

Post on 04-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 07 Oo Exercise

    1/23

    Classes

    7. OO Revisited

    June 15, 2010

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 1 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    2/23

    Classes

    Create a Struct Vector

    Create a structVectorwith 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

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    3/23

    Classes

    Create a Struct Vector

    Create a structVectorwith three doubles and an additional double holding the

    length of the vector.

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

    } ;

    Create three vectorsa= (0.2, 0.4), b= (1.0, 0.0), andc= (1.2, 0.0)in yourmain routine. The first two instances shall be variables, the latter instance shall becreated on the heap.

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 2 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    4/23

    Classes

    Create a Struct Vector

    Create a structVectorwith three doubles and an additional double holding the

    length of the vector.

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

    } ;

    Create three vectorsa= (0.2, 0.4), b= (1.0, 0.0), andc= (1.2, 0.0)in yourmain routine. The first two instances shall be variables, the latter instance shall becreated on the heap.

    V e ct o r a , b ;Ve cto r c = new V e c t o r ;/ / a s s i g n v a l u es now

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 2 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    5/23

    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

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    6/23

    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

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

    } ;

    v o i d co mp ut eL en gt h ( V e c t o r& v e c t o r ) {v e c to r . l e n g t h = s t d : : s q r t ( v e ct o r . x [ 0 ]v e ct o r . x [ 0 ] + v e ct o r . x [

    }

    Call this operation in your main for aand c.

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 3 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    7/23

    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

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

    } ;

    v o i d co mp ut eL en gt h ( V e c t o r& v e c t o r ) {v e c to r . l e n g t h = s t d : : s q r t ( v e ct o r . x [ 0 ]v e ct o r . x [ 0 ] + v e ct o r . x [

    }

    Call this operation in your main for aand c.

    compu teL en gth (a ) ;computeLength(c ) ;

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 3 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    8/23

    Classes

    Methods Instead of Functions

    Makevoid computeLength( Vector & vector)a method ofVector.

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 4 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    9/23

    Classes

    Methods Instead of Functions

    Makevoid computeLength( Vector & vector)a method ofVector.

    # i n c l u d e

    s t r u c t Ve cto r {double x [ 3 ] ;double l e n g t h ;v o i d computeLength ( ) ;

    } ;

    v o i d Vect or : : computeLength ( ) {l e ng t h = s td : : s q r t ( x [ 0 ] x [ 0 ] + x [ 1 ] . . . ) ;

    }

    a . computeLength ( ) ;c>computeLength ( ) ;

    Add an operation toTerminal()writing the vector to the terminal.

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 4 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    10/23

    Classes

    Methods Instead of Functions

    Makevoid computeLength( Vector & vector)a method ofVector.

    # i n c l u d e

    s t r u c t Ve cto r {double x [ 3 ] ;double l e n g t h ;v o i d computeLength ( ) ;

    } ;

    v o i d Vect or : : computeLength ( ) {l e ng t h = s td : : s q r t ( x [ 0 ] x [ 0 ] + x [ 1 ] . . . ) ;

    }

    a . computeLength ( ) ;c>computeLength ( ) ;

    Add an operation toTerminal()writing the vector to the terminal.

    v o i d V e c t o r : : t o T e r m i n a l ( ) {s t d : : c ou t

  • 8/13/2019 07 Oo Exercise

    11/23

    Classes

    Scalar Product

    Add the operation double getAngle( const Vector & rhs)to Vector. Thisoperation shall do two things:

    Print object for which it is called to terminal. Print argumentrhs to terminal. Return 0.0.

    Invoke this operation for the variablesaand cand print the result to the terminal.

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 5 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    12/23

    Classes

    Scalar Product

    Add the operation double getAngle( const Vector & rhs)to Vector. Thisoperation shall do two things:

    Print object for which it is called to terminal. Print argumentrhs to terminal. Return 0.0.

    Invoke this operation for the variablesaand cand print the result to the terminal.

    # i n c l u d e

    s t r u c t Ve cto r {double x [ 3 ] ;double l e n g t h ;double getAngle ( co n st V ec t or & r h s ) ;

    } ;

    double Ve cto r : : g e tAn g le ( co n st V e ct o r & r h s ) {

    t o T er m i n al ( ) ;r h s . t o T e r mi n a l ( ) ;r e t u r n 0 . 0 ;

    }

    . . .a . ge t A n gl e (c ) ;/ / o r

    c>g e t A ng l e ( a ) ;7. OO RevisitedIntroduction to C/C++,Tobias Weinzierl page 5 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    13/23

    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 argumentrhs to terminal. Compute angle between object andrhs due to the value oflengthand

    return it.

    Invoke the operation foraand b.

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 6 of 14

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    14/23

    Classes

  • 8/13/2019 07 Oo Exercise

    15/23

    Classes

    Scalar Product

    Add a constructor to Vectorwhich accepts three doubles and automatically

    ensures thatlengthholds the right value. Do not use initialisation lists (makes it alittle bit simpler).

    7. OO Revisited

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

    Classes

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    16/23

    Classes

    Scalar Product

    Add a constructor to Vectorwhich accepts three doubles and automatically

    ensures thatlengthholds the right value. Do not use initialisation lists (makes it alittle bit simpler).

    # i n c l u d e

    s t r u c t Ve cto r {. . .Vect or ( double x1 , double x2 , double x3 ) ;

    } ;

    Ve cto r : : Ve cto r ( double x1 , double x2 , double x3 ) {x [ 0 ] = x1 ; . . .computeLength ( ) ;

    }

    Make your struct a class and try to manipulate the attribute length of amanuallywithin the main function.

    7. OO Revisited

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

    Classes

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    17/23

    Classes

    7.1.Classes

    With the new technique at hand, we can encapsulate data and operations asthese two things go hand in hand.

    We can write a couple of setter and getter operations to allow the user tomanipulate 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 providethe corresponding getters (next pointer in our list example, e.g.).

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 8 of 14

    Classes

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    18/23

    Classes

    Our Beloved Colleagues

    / R e p r es e n ts a d a t e .

    Each m onth s h a l l ha ve 30 d ay s ./

    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 co de our c o l l e a g u e wroteDa te myDate ( . . . ) ;myDate. day++;

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 9 of 14

    Classes

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    19/23

    Encapsulation

    / R e p r es e n ts a d a t e .

    Each m onth s h a l l ha ve 30 d ay s ./

    c l a s s Da te {p r i v a t e :

    i n t month , day ;p u b l i c :

    v o i d switchToNextDay ( ) ;} ;

    In principle, classis an alias forstruct,

    i.e. we can do all the things we can do with structs with classes, too.

    However, for classes we can create publicand privatesections. Only add themin the header.

    Private attributes and operations are not available from outside, but only within theoperations of the class (encapsulation).

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 10 of 14

    Classes

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    20/23

    Encapsulation at Work

    / R e p r es e n ts a d a t e .

    Each m onth s h a l l ha ve 30 d ay s ./

    c l a s s Da te {p r i v a t e :

    i n t m on th , d a y ;p u b l i c :

    v o i d switchToNextDay ( ) ;} ;

    . . .v o i d Date : : switchToNextDay ( ) {

    d a y++; / / o . k .}

    Date myDate;

    myDate. day ++; / / doe sn t w or k .

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 11 of 14

    Classes

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    21/23

    Encapsulation and the Object Lifecycle

    / R e p r es e n ts a d a t e .

    Each m onth s h a l l ha ve 30 d ay s ./

    c l a s s Da te {p r i v a t e :

    i n t m on th , d a y ;D at e ( ) ;

    p u b l i c :v o i d switchToNextDay ( ) ;

    Da te ( i n t month , i n t day ) ;} ;

    We can make constructors private and, thus, forbid everybody to create aninstance 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

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    22/23

    Remark: Classes and Namespaces

    namespace cal end ar {

    / R e p r es e n ts a d a t e . Each m onth s h a l l ha ve 30 d ay s ./

    c l a s s D at e {p r i v a t e :

    i n t m on th , d a y ;D at e ( ) ;

    p u b l i c :

    v o i d switchToNextDay ( ) ;Da te ( i n t month , i n t day ) ;

    } ;}

    v o i d cal enda r : : Date : : switchToNextDay ( ) {. . .

    }

    / / f u l l y q u a l i f i e d ar gum ent here n o t necessary/ / we ca n access p r i v a t e arguments o f o t h e r i n s t a n c e sv o i d cal enda r : : Date : : copyFromOtherDate (co n st ca le n d a r : : Date& d a te ) {

    . . .}

    7. OO Revisited

    Introduction to C/C++,Tobias Weinzierl page 13 of 14

    Classes

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 07 Oo Exercise

    23/23

    Remark: Classes and Recursion

    c l as s I n t e g e rE n t r y {

    p r i v a t e :i n t value ;I n t e g e r E n t r y next ;

    p u b l i c :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 ( next ==0) {n e xt = n e wEn try ;

    }e l s e {

    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

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl