07 object based programming
TRANSCRIPT
-
8/13/2019 07 Object Based Programming
1/29
Operations on Structs Constructors and Destructors Const Methods Classes
7. Object-based Programming
June 14, 2010
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 1 of 28
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 Object Based Programming
2/29
Operations on Structs Constructors and Destructors Const Methods Classes
Outline
Recapitulation
Operations on Structs: Methods
Constructors and Destructors
The Const Modifier
Encapsulation & Information Hiding
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 2 of 28
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 Object Based Programming
3/29
Operations on Structs Constructors and Destructors Const Methods Classes
Recapitulation
i n t a ;i n t b ;a = 3 ;b = & a ;b = 4 ;b = 5 ;
Explain/sketch what the invidial lines do. What does b++;
What happens if we write
i n t a , b ;
What happens if we call a functionfoo(int*)withb.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 3 of 28
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 Object Based Programming
4/29
Operations on Structs Constructors and Destructors Const Methods Classes
7.1.Operations on Structs
/ R e p r es e nt s a d a t e . Each m onth s h a l l hav e 30 d ay s ./
s t r u c t Date {i n t month , day ;
} ;
v o i d switchToNextDay (Date& date
) {. . .
}
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 4 of 28
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 Object Based Programming
5/29
Operations on Structs Constructors and Destructors Const Methods Classes
Class Diagrams with the Unified Modelling Language
The Unified Modelling Language is agraphical representation of yoursystem design.
Operation and the data here gohand-in-hand. UML illustrates this fact.
Object-based paradigm: Model wholesystem in terms of structs andoperations acting on these structs.
Remember: Structs can hold otherstructs asattributes.
Remember: Structs can hold pointersto other structs.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 5 of 28
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 Object Based Programming
6/29
Operations on Structs Constructors and Destructors Const Methods Classes
Aggregation and Composition
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 6 of 28
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 Object Based Programming
7/29
Operations on Structs Constructors and Destructors Const Methods Classes
Operations on Structs Rewritten
/ R e pr es en t s a d a te . Each month s h a l l h ave 30 d ay s /s t r u c t Date {
i n t month , day ;} ;
/ / D e c l a r a t i o n
v o i d s w i tc hT oN ex t Da y ( D at e& d a t e ) { . . . }
/ R e pr es en t s a d a te . Each month s h a l l h ave 30 d ay s /s t r u c t Date {
i n t month , day ;/ / Belongs t o t h e s t r u c t i t i s embedded i n t o
v o i d switchToNextDay ( ) ;} ;
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 7 of 28
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 Object Based Programming
8/29
Operations on Structs Constructors and Destructors Const Methods Classes
Operations on Structs Rewritten
/ / Da te . hs t r u c t Date {
i n t month , day ;v o i d switchToNextDay ( ) ;
} ;
/ / Da te . cp p# i n c l u d e D a t e . h
v o i d Date : : switchToNextDay ( ) {day++;. . .
}
Syntax is similar to namespaces.
It is now clear, how operations and data belong together.
A good object always works solely on data of its own. If it has to manipulate datafrom another object, it should use this objects functions (operations,methods).
Internally, it is still your straightforward C realisation.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 8 of 28
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 Object Based Programming
9/29
-
8/13/2019 07 Object Based Programming
10/29
Operations on Structs Constructors and Destructors Const Methods Classes
7.2.Constructors and Destructors
/ / Da te . hs t r u c t Date {
i n t month , day ;v o i d switchToNextDay ( ) ;
} ;
. . .
Date myDate;myDate. month = 7;myDate . day = 5 ;myDate. switchToNextDay ( ) ;
The two set operations also belong to
the struct itself. It might be good design to write an
initialisation operation.
Add a methodinit(int month, intday);.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 10 of 28
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 Object Based Programming
11/29
Operations on Structs Constructors and Destructors Const Methods Classes
Constructors
/ / Da te . h
s t r u c t Date {i n t m on th , d a y ;v o i d switchToNextDay ( ) ;Date( i n t month , i n t day ) ;
} ;
/ / Da te . cp pDate : : Date ( i n t month , i n t day ) :
month ( month ) ,d a y ( d a y ) {
/ / some checks}
A constructor is a special type ofoperation.
Its name equals the struct name.
It has no return type.
It uses initialisation lists.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 11 of 28
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 Object Based Programming
12/29
Operations on Structs Constructors and Destructors Const Methods Classes
Creating an Instance
/ / Da te . h
s t r u c t Date {i n t m on th , d a y ;v o i d switchToNextDay ( ) ;Da te ( i n t month , i n t day ) ;D at e ( ) ;Da te ( c o n st c ha r s t r i n gR e p r e se n t a t i on ) ;
} ;
/ / Another f i l eD at e myDate1 ( 7 , 5 ) ;Date myDate2 ( ) ; / / doe sn t w or k Date myDate3 ;Date myDate4 ( 5 J u l y ) ;
We can overload constructors. Thedefault constructoris invoked without brackets.
There is no need for a default constructor.
If you dont provide a constructor at all, C++ automatically (in the background)generates a default constructor.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 12 of 28
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 Object Based Programming
13/29
Operations on Structs Constructors and Destructors Const Methods Classes
Object Destruction
. . .
w h i l e ( s o m e t h i n g ) {D at e myDate ( . . . ) ;
/ / we do something
}
Instance of Date is destroyed at the end of the scope.
If there is a constructor, why isnt there a counterpart?
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 13 of 28
O ti St t C t t d D t t C t M th d Cl
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 Object Based Programming
14/29
Operations on Structs Constructors and Destructors Const Methods Classes
Destructor Syntax
/ / Da te . h
s t r u c t Date {i n t m on th , d a y ;v o i d switchToNextDay ( ) ;Da te ( i n t month , i n t day ) ;D at e ( ) ; D at e ( ) ;
} ;
/ / Da te . cp pDate : : Date ( ) {}
Destructor is called always at the end of the scope containing the object.
We cannot overload destructors.
Destructors dont have a return value.
A destructor is never called explicitly.
If you dont define a destructor, C++ automatically generates one.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 14 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
15/29
Operations on Structs Constructors and Destructors Const Methods Classes
Objects on the Heap
/ / Da te . h
s t r u c t Date {i n t m on th , d a y ;v o i d switchToNextDay ( ) ;Da te ( i n t month , i n t day ) ;D at e ( ) ; D at e ( ) ;
} ;
. . .Date myDate = new D at e ( 7 , 5 ) ;. . .
myDate>switchToNextDay ( ) ;. . .d e l e l t e myDate ;
new reserves memory (as it does in C), and new invokes the constructor. delete invokes the destructor, and it frees the memory (as it does in C). Remember of linked list: If we delete the first list entry, this entry can also free the
subsequent entries. It is much easier to enforce the consistency with these newdata structures.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 15 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
16/29
Operations on Structs Constructors and Destructors Const Methods Classes
Arrays of Objects
/ / Da te . h
s t r u c t Date {i n t m on th , d a y ;v o i d switchToNextDay ( ) ;Da te ( i n t month , i n t day ) ;D at e ( ) ; D at e ( ) ;
} ;
Date myDates [ 1 0 ] ;. . .
myDate[4]>switchToNextDay ( ) ;. . .
Arrays of objects are supported by C++.
However, such data structures have to have a standard constructor.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 16 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
17/29
Operations on Structs Constructors and Destructors Const Methods Classes
Explicit Constructor Calls
/ / Da te . h
s t r u c t Date {i n t m on th , d a y ;v o i d switchToNextDay ( ) ;Da te ( i n t month , i n t day ) ;D at e ( ) ; D at e ( ) ;
} ;
D at e m yDate1 = D at e ( 7 , 5 ) ;
Date myDate2 = Date ( ) ;. . .
Here, we need the parentheses.
Obviously, the constructor is kind of a function which returns a struct. This is bad style, as it induces a bit-wise copy.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 17 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
18/29
Operations on Structs Constructors and Destructors Const Methods Classes
7.3.Const Methods
. . . why does this snippet not work?
s t r u c t I n t e g e r E n t r y {i n t value ;I n t e g e r E n t r y next ;/ / P r i n t t h i s e n t r y t o t e r m i n a l and c o n t i n u e w i t h n e x t e n t r y .v o i d p r i n t L i s t ( ) ;/ / Make argu ment n e x t ar gum ent .v o i d append ( I n t e g e r E n t r y n e x t E n t r y ) ;
} ;
. . .
v o i d doSomething ( co n st I n t e g er E n tr y& e nt r y ) {. . .e n tr y . p r i n t L i s t ( ) ;
}
Call-by-value is expensive, as it copies the whole object bit-wise. This lead to aperformance breakdown in this code. Thus, we used call-by-const-reference. However,the code now does not compile anymore.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 18 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
19/29
p
Const Keyword
s t r u c t I n t e g e r E n t r y {
i n t value ;I n t e g e r E n t r y next ;/ / P r i n t t h i s e n t r y t o t e r m i n a l and c o n t i n u e w i t h n e x t e n t r y .v o i d p r i n t L i s t ( ) co n st;/ / Make argu ment n e x t ar gum ent .v o i d append ( I n t e g e r E n t r y n e x t E n t r y ) ;
} ;
. . .
v o i d doSomething ( co n st I n t e g er E n tr y& e nt r y ) {. . .e n tr y . p r i n t L i s t ( ) ;
}
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 19 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
20/29
p
Semantics of Const
s t r u c t I n t e g e r E n t r y {
i n t value ;I n t e g e r E n t r y next ;/ / P r i n t t h i s e n t r y t o t e r m i n a l and c o n t i n u e w i t h n e x t e n t r y .v o i d p r i n t L i s t ( ) co n st;
} ;
v o i d I n t eg e rE n t ry : : p r i n t L i s t ( ) co n st {v a l u e = 2 ; / / e r r or
r e t u r n value ; / / o . k .}
constoperations may not alter object state.
constoperations may not call non-const methods.
constoperatoins can be invoked on objects passed by call-by-const-reference. constoperations allow the compiler to optimise.
constoperations allow user to enforce encapsulation and to write safter apps.
use theconstmodifier whenever possible.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 20 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
21/29
Const Variants
s t r u c t I n t e g e r E n t r y {
i n t value ;I n t e g e r E n t r y next ;
c on st i n t g e tV a lu e ( ) ;c on st i n t g e tVa lu e ( ) co n st;
/ / v a r i a n t Ai n t g e tVa lu e ( ) co n st;
i n t g e tV a lu e ( ) ;
/ / v a r i a n t Bc on st i n t & g e t V al u e ( ) co n st;c on st i n t & g e tV a lu e ( ) ;
} ;
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 21 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
22/29
Const Variants
s t r u c t I n t e g e r E n t r y {
i n t value ;I n t e g e r E n t r y next ;
c on st i n t g e tV a lu e ( ) ;c on st i n t g e tVa lu e ( ) co n st;
/ / v a r i a n t Ai n t g e tVa lu e ( ) co n st;
i n t g e tV a lu e ( ) ;
/ / v a r i a n t Bc on st i n t & g e t V al u e ( ) co n st;c on st i n t & g e tV a lu e ( ) ;
} ;
constbelongs to the signature, i.e. we can overload with respect to const. constafter the operation enforces the operation not to manipulate object state.
constbefore the return argument does not allow programmer to manipulate result.
The compiler tries to use constoperations before it falls back to non-constoperations.
7. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 21 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
23/29
7.4.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 use still can reset attributes manually. We can not forbid this.
Consequently, we need an alterantive, 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. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 22 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
24/29
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. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 23 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
25/29
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. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 24 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
26/29
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. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 25 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
27/29
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. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 26 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
28/29
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. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 27 of 28
Operations on Structs Constructors and Destructors Const Methods 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 Object Based Programming
29/29
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. Object-based Programming
Introduction to C/C++,Tobias Weinzierl page 28 of 28
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