08 Object Based Programming
08 Object Based Programming
Const Methods
Classes
An OO Case Study
Operations on Structs
Const Methods
Classes
An OO Case Study
Outline
Recapitulation Operations on Structs: Methods Constructors and Destructors The Const Modier Encapsulation & Information Hiding Case Study: Types for Linear Algebra Case Study: A Binary Tree &
Depth-rst Traversal
Operations on Structs
Const Methods
Classes
An OO Case Study
Recapitulation
int a; int b; a = 3; b = &a ; b = 4; b = 5 ;
Explain/sketch what the individual lines do. What does b++; What happens if we write int a, b;
Operations on Structs
Const Methods
Classes
An OO Case Study
/ 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 ( Date& date ) { ... }
Operations on Structs
Const Methods
Classes
An OO Case Study
structs as attributes.
Remember: Structs can hold pointers
to other structs.
Operations on Structs
Const Methods
Classes
An OO Case Study
/ 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 ; }; / / Declaration v o i d switchToNextDay ( Date& date ) { . . . }
/ 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 ; / / 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 ( ) ; };
Operations on Structs
Const Methods
Classes
An OO Case Study
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; }; / / Date . cpp # i n c l u d e Date . 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 data
from another object, it should use this objects functions (operations, methods).
Internally, it is still your straightforward C realisation.
8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 35
Operations on Structs
Const Methods
Classes
An OO Case Study
/ / Date . h s 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 ( ) ;
Create a variable: instantiate. Variable: object. Variables of a struct: attributes. Call a structs operation: invoke a method.
Operations on Structs
Const Methods
Classes
An OO Case Study
/ / Date . h s 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 ( ) ;
initialisation operation.
Add a method init(int month, int
day);.
Operations on Structs
Const Methods
Classes
An OO Case Study
Constructors
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; }; / / Date . cpp Date : : Date ( i n t month , i n t day ) : month ( month ) , day ( day ) { / / some checks }
operation.
Its name equals the struct name. It has no return type. It uses initialisation lists.
Operations on Structs
Const Methods
Classes
An OO Case Study
Creating an Instance
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( c o n s t char s t r i n g R e p r e s e n t a t i o n ) ; }; / / Another f i l e Date myDate1 ( 7 ,5 ) ; Date myDate2 ( ) ; / / doesn t work Date myDate3 ; Date myDate4 ( 5 J u l y ) ;
We can overload constructors. The default constructor is invoked without brackets. There is no need for a default constructor. If you dont provide a constructor at all, C++ automatically (in the background)
Operations on Structs
Const Methods
Classes
An OO Case Study
Object Destruction
... w h i l e ( something ) { Date 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?
Operations on Structs
Const Methods
Classes
An OO Case Study
Destructor Syntax
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( ) ; }; / / Date . cpp Date : : 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 dene a destructor, C++ automatically generates one.
Operations on Structs
Const Methods
Classes
An OO Case Study
For our algorithms, it is important that the last list element points to 0, i.e. whenever we create a list element, we manually have to ensure that is points
to 0.
With the constructors, we can ensure that each single element equals a list of
length one.
Operations on Structs
Const Methods
Classes
An OO Case Study
Operations on Structs
Const Methods
Classes
An OO Case Study
So far, a delete always induced a memory leak. With the destructors, we can ensure that all connected list elements are deleted
as well.
ListElement : : ListElement ( ) { i f ( nextElement ! = 0 ) d e l e t e }
nextElement ;
Operations on Structs
Const Methods
Classes
An OO Case Study
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 rst list entry, this entry can also free the subsequent entries. It is much easier to enforce the consistency with these new data structures.
page 16 of 35
Operations on Structs
Const Methods
Classes
An OO Case Study
Arrays of Objects
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( ) ; }; Date myDates [ 1 0 ] ; ... myDate[4]> switchToNextDay ( ) ; ...
Arrays of objects are supported by C++. However, such data structures have to have a standard constructor.
Operations on Structs
Const Methods
Classes
An OO Case Study
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.
Operations on Structs
Const Methods
Classes
An OO Case Study
Call-by-value is expensive, as it copies the whole object bit-wise. This lead to a performance breakdown in this code. Thus, we used call-by-const-reference. However, the code now does not compile anymore.
Operations on Structs
Const Methods
Classes
An OO Case Study
Const Keyword
struct IntegerEntry { int 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 . void p r i n t L i s t ( ) const ; / / Make argument n e x t argument . 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 ( c o n s t I n t e g e r E n t r y & e n t r y ) { ... entry . p r i n t L i s t ( ) ; }
Operations on Structs
Const Methods
Classes
An OO Case Study
Semantics of Const
struct IntegerEntry { int 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 . void p r i n t L i s t ( ) const ; }; void I n t e g e r E n t r y : : p r i n t L i s t ( ) const { value = 2; / / e r r o r r e t u r n value ; / / o . k . }
const operations may not alter object state. const operations may not call non-const methods. const operations can be invoked on objects passed by call-by-const-reference. const operations allow the compiler to optimise. const operations allow user to enforce encapsulation and to write safer apps.
Operations on Structs
Const Methods
Classes
An OO Case Study
Const Variants
struct IntegerEntry { int value ; I n t e g e r E n t r y next ; c o n s t i n t getValue ( ) ; c o n s t i n t getValue ( ) c o n s t ; / / variant A i n t getValue ( ) c o n s t ; i n t getValue ( ) ; / / variant B c o n s t i n t & getValue ( ) c o n s t ; c o n s t i n t & getValue ( ) ; };
Operations on Structs
Const Methods
Classes
An OO Case Study
Const Variants
struct IntegerEntry { int value ; I n t e g e r E n t r y next ; c o n s t i n t getValue ( ) ; c o n s t i n t getValue ( ) c o n s t ; / / variant A i n t getValue ( ) c o n s t ; i n t getValue ( ) ; / / variant B c o n s t i n t & getValue ( ) c o n s t ; c o n s t i n t & getValue ( ) ; };
const belongs to the signature, i.e. we can overload with respect to const. const after the operation enforces the operation not to manipulate object state. const before the return argument does not allow programmer to manipulate result. The compiler tries to use const operations before it falls back to non-const
operations.
8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 22 of 35
Operations on Structs
Const Methods
Classes
An OO Case Study
8.4. Classes
Edsger Dijkstra, The Humble Programmer (EWD340), Communications of the ACM: The major cause of the software crisis is that the machines have become several orders of magnitude more powerful! To put it quite bluntly: as long as there were no machines, programming was no problem at all; when we had a few weak computers, programming became a mild problem, and now we have gigantic computers, programming has become an equally gigantic problem.
Projects running over-budget. Projects running over-time. Software was very inefcient. Software was of low quality. Software often did not meet requirements. Projects were unmanageable and code difcult to maintain. Software was never delivered.
Operations on Structs
Const Methods
Classes
An OO Case Study
Resume
With the new technique at hand, we can encapsulate data and operations as
Operations on Structs
Const Methods
Classes
An OO Case Study
Operations on Structs
Const Methods
Classes
An OO Case Study
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 on Structs
Const Methods
Classes
An OO Case Study
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 .
Operations on Structs
Const Methods
Classes
An OO Case Study
Operations on Structs
Const Methods
Classes
An OO Case Study
Operations on Structs
Const Methods
Classes
An OO Case Study
This is not recursion, as the operation is invoked on another object. However, recursion and object-based programming work together.
Operations on Structs
Const Methods
Classes
An OO Case Study