Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | anidcohen9058 |
View: | 220 times |
Download: | 0 times |
of 52
8/13/2019 10 Object Oriented Programming
1/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
10. Object-oriented Programming
7. Juli 2011
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 1 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
2/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Outline
Object Case Study
Brain Teaser
Copy Constructor & Operators
Object-oriented Programming,i.e. Inheritance
Dynamic and Static Polymorphism
The Keyword Static
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 2 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
3/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
10.1. An OO Case Study
Create a Struct Vector
Create a struct Vector with three doubles and an additional double holding thelength of the vector.
s t r u c t Vector {double x [ 3 ] ;double l eng th ;
} ;
Create three vectors a = ( 0 . 2 , 0 . 4) , b = ( 1 . 0 , 0 . 0), and c = ( 1 . 2 , 0 . 0) in yourmain routine. The rst two instances shall be variables, the latter instance shall becreated on the heap.
Vector a , b ;Vector c = new Vector ; / / ass ign va lues now
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
4/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Compute the Length
Assume there is 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 eng th ;
} ;
vo id computeLength( Vector& vecto 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 c t o r . x [ 0 ]
+ v e c to r . x [ 1 ] . . . ) ;}
. . .computeLength (a ) ;computeLength( c ) ;
Make void computeLength( Vector & vector) a method of Vector , i.e. changethis piece of code into its object-based variant.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
5/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Methods Instead of Functions
# 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 ;void computeLength ( ) ;
} ;
void Vecto r : : computeLength ( ) {l e n g th = 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.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 5 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
6/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Methods Instead of Functions
# 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 ;void computeLength ( ) ;
} ;
void Vecto r : : computeLength ( ) {l e n g th = 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.
vo id Vector : : toTerminal ( ) {s td : : cout
8/13/2019 10 Object Oriented Programming
7/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
A Constructor
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 alittle bit simpler).
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
8/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
A Constructor
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 alittle bit simpler).
# i n c l u d e < cmath >
s t r u c t Vector {. . .Vect or ( double x1 , double x2 , double x3 ) ;
} ;
Vector : : Vector ( double x1 , double x2 , double x3 ) {x [ 0 ] = x1 ; . . .computeLength ( ) ;
}
Make your struct a class and try to manipulate the attribute length of a manuallywithin the main function.
Add a function scale(double value) that scales the vector and automaticallyalso updates the length.
Make computeLength() private, too. Does your code still work? Why?
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
9/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Setters and Getters
Write an operation getLength() .
Add a function void getAngle( const Vector& a, const Vector& b) . It is notdened within a class/object but is a plain (old-fashioned) function.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
10/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Setters and Getters
Write an operation getLength() .
Add a function void getAngle( const Vector& a, const Vector& b) . It is notdened within a class/object but is a plain (old-fashioned) function.
Make the operation getLength() a const operation.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
11/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Brain Teaser
c l as s S t r i n g {p r i v a t e :
/ / a i n t a 0 t er mi na t ed s t r i n g char data ; / / l e n g t h o f s t r i n g i n t l e n g t h ;
p u b l i c :S t r i n g ( ) ;void copyFromOther String ( const S t r in g& s t r i n g ) ;
} ;
. . .
S t r i n g : : S t r i n g ( ) :data (0) ,l e n g t h ( 0 ) {}
void St r i ng : : copyFromOtherStr ing ( const S t r i ng& s t r i n g ) {da ta = s t r i n g . data ;l e ng t h = s t r i n g . l e ng t h ;
}
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 8 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
12/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
10.2. Copy Constructor and Operators
C++ generates a couple of default operations for any class/struct to allow you touse these datatypes as you use a built-in datatype.
i n t a ( 1 0 ) ;Date moated( anotherDate ) ;
Sometimes, we have to rewrite these default operations. For the copy constructor, it is very simple.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 9 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
13/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Copy Constructor for a String Class
c l as s S t r i n g {p r i v a t e :
/ / a i n t a 0 t er mi na t ed s t r i n g char data ; / / l e n g t h o f s t r i n g i n t l e n g t h ;
p u b l i c :S t r i n g ( ) ;S t r i n g ( const S t ri ng& s t r i n g ) ;
} ;
. . .
S t r i n g : : S t r i n g ( ) :data (0) ,l e n g t h ( 0 ) {}
S t r i n g : : S t r i n g ( const S t ri ng& s t r i n g ) :d at a ( 0 ) ,l e ng t h ( s t r i n g . l e ng t h ) {data = new . . . ;
f o r ( . . . ) {. . .
}}
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 47
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
14/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Excursus: C++ Strings
Good news: You now know how to write a copy constructor for your personal string
class. Bad news: Such a class does already exist.
# i n c l u d e < s t r i n g >
s t d : : s t r i n g m yS tr in g0 ;s td : : s t r i ng mySt ring1 ( h e l l o w or l d ) ;
s td : : s t r i ng myStr ing2 ( myStr ing1 ) ;s t d : : s t r i n g m yS tr in g3 = h e l l o MPG ;s td : : s t r i ng mySt ring4 = mySt ring3 ;myString . leng th ( ) ;myString4 += myString1 ;
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 11 of 47
A OO C S d C C d O I h i i l O i i C J
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
15/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Default Operations
The following operations are generated automatically as public operations if we
dont provide our own implementation: Default constructor (attributes contain garbage). Destructor (does nothing). Copy constructor (bit-wise copying). Assignment (bit-wise copying).
However, theres still another pitfall/missing thing:
MyStr ing a ; / / do something w i t h a ; M yS tr in g b ( a ) ; / / works M yS tr i ng c ;i f (c==a) { / / does no t work c o r r e c t l y
s td : : cout
8/13/2019 10 Object Oriented Programming
16/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Operators
In C++, operators such as +,-,+=,=,
8/13/2019 10 Object Oriented Programming
17/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Operators RedenedPart I
c l a s s Vec t or {p u b l i c :
/ / copy c o n s t r u c t o r Vecto r ( const Vec to r& v e c t o r ) ;
Vec to r& opera to r=( const Vec to r& v e c to r ) ; / / unary o p e ra to r s Vecto r& opera to r +=( const Vec to r& vec to r ) ;Vecto r& opera to r =( cons t double & v al u e ) ;
void toS tream( s td : : o st ream& ou t ) const ;} ;
/ / b i n a r y o p e ra to r s Vector& opera to r +( const Vector& lhs , const Vec to r& rhs ) ;s td : : o st ream& operato r
8/13/2019 10 Object Oriented Programming
18/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Operators RedenedPart II
c l a s s Vec t or {p u b l i c :
/ / copy c o n s t r u c t o r Vecto r ( const Vec to r& v e c t o r ) ;
Vec to r& opera to r=( const Vec to r& v e c to r ) ; / / unary o p e ra to r s Vecto r& opera to r +=( const Vec to r& vec to r ) ;Vecto r& opera to r =( cons t double & v al u e ) ;
void toS tream( s td : : o st ream& ou t ) const ;} ;
/ / b i n a r y o p e ra to r s Vector& opera to r +( const Vector& lhs , const Vec to r& rhs ) ;s td : : o st ream& operato r
8/13/2019 10 Object Oriented Programming
19/52
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C vs. Java
10.3. Inheritance
A Case Study I
Image you write a galaxy simulation program, i.e. Gadget-2 does not exist. You have Planet s and Sun s. Due to your brilliant C++ course, you decide to model both as classes. Both of them have a mass, a diameter, and the sun additionally has a temperature.
The planet holds a boolean ag that indicates whether theres life on this planet.
class Sun {p r i v a t e :
double mass ;double d iameter ;double temperature ;
} ;
c l a s s P l a ne t {p r i v a t e :double mass ;double d iameter ;bool l i f e ;
} ;
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 16 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
20/52
y py p p
A Case Study II
class Sun {
p r i v a t e :double mass , d iamete r ;double temperature ;
} ;
c l a s s P l a ne t {p r i v a t e :
double mass , d iamete r ;
bool l i f e ;} ;
Write a member function (method)getWeight() const , and
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
21/52
A Case Study II
class Sun {
p r i v a t e :double mass , d iamete r ;double temperature ;
} ;
c l a s s P l a ne t {p r i v a t e :
double mass , d iamete r ;
bool l i f e ;} ;
Write a member function (method)getWeight() const , and
write a function doublegetForce(xxx, xxx) with either a sunor a planet as input parameters. Keepin mind that these parameters shouldbe passed as const references andthat you can use overloading.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
22/52
A Case Study II
class Sun {
p r i v a t e :double mass , d iamete r ;double temperature ;
} ;
c l a s s P l a ne t {p r i v a t e :
double mass , d iamete r ;
bool l i f e ;} ;
Write a member function (method)getWeight() const , and
write a function doublegetForce(xxx, xxx) with either a sunor a planet as input parameters. Keepin mind that these parameters shouldbe passed as const references andthat you can use overloading.
Is it good code (code duplication)?
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
23/52
Case Study Rewritten
The planet and the sun have something in common. They both are celestialbodies.
Imagine there is something like a celestial body in C. A Planet then is an extension (specialisation) of CelestialBody . A S then is an extension (specialisation) of CelestialBody , too. Sun and Planet are two different things. Both have in common that they have a weight and a diameter.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 18 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
24/52
is aRelations
Planet has all operations and attributes CelestialBody has. Sun has all operations and attributes CelestialBody has.
Every operation expecting an instance of CelestialBody could also be passed aninstance of Sun . Every operation expecting an instance of CelestialBody could also be passed an
instance of Planet . Again, we could image this to be an extension of a cut-n-paste mechanism.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 19 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
25/52
is aRelations in Source Code (Part I)
c l a s s C e l e s t ia l B o d y {p r i v a t e :
double mass , d iamete r ;p u b l i c :
double getMass () const ;} ;
c l as s Sun : p u b l i c C e le s ti a l Bo d y {p r i v a t e :
double temperature ;} ;
c l as s P l an et : p u b l i c C e le s ti a l Bo d y {p r i v a t e :
bool l i f e ;} ;
. . .
P l a ne t e a r t h ;ear th . getMass ( ) ; / / t h at s f i n e
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 20 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
26/52
is aRelations in Source Code (Part II)
c l a s s C e l e s t ia l B o d y {
p r i v a t e :double mass , d iamete r ;p u b l i c :
double getMass () const ;} ;
c l as s Sun : p u b l i c C e le s ti a l Bo d y {p r i v a t e :
double temperature ;p u b l i c :void explode ( ) ;
} ;
c l as s P l an et : p u b l i c C e le s ti a l Bo d y {p r i v a t e :
bool l i f e ;
} ;void foo ( const C e l e s t i a l B od y& a ) {
a . getMass ( ) ; / / t h at s f i n e a . explode ( ) ; / / a r rg h
}
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 21 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
27/52
Object-oriented Programming
is-a relations are mapped to inheritance .
Object-based programming becomes object-oriented programming due to thisinheritance concept.
Whenever an object of type A is expected somewhere, we can also pass an objectwith a subtype of B . This concept is called polymorphism .
Theres a new visibility mode protected : These attributes and operations behavelike private , i.e. they are not visible from outside, but they are visible within
subclasses.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 22 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
28/52
Super Constructors
c l a s s A {
p u b l i c :A( i n t v ar i ab le ) ;} ;
c la ss B : p u bl i c A {p u b l i c :
B ( ) ;} ;
. . .
B : : B ( ) :A ( 2 3 ) {
}
This is the reason, C++ introduced initialisation lists, and treats both attributes andsuper types the same way. Please take care of the order: First, you have to call thesupertypes constructor, then you have to initialise your attributes in the right order.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 23 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
29/52
Case Study RevisitedWrite Your First Inheritance
c l a s s C e l e s t ia l B o d y {
p r i v a t e :double mass , d iamete r ;p u b l i c :
Celes t ia lBody ( double mass);double getMass ( ) const ;
} ;c l as s Sun : p u b l i c C e l es t i al B o dy {
p r i v a t e :
double temperature ;p u b l i c :Sun( double mass);
} ;c l as s P l an et : p u b l i c C e l es t i al B o dy {
p r i v a t e :bool l i f e ;
p u b l i c :
Planet ( double mass);} ;double computeForce (
const C e l e s t i a l B o d y & a ,const C e l e s t i a l B od y& b ) { / / your jo b
}
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 24 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
30/52
Inheritance Summarised
Inheritance represents is-a relations.
It enables us to represent/model the real world more accurately than just plainstructs and functions/methods. It helps us to avoid code duplication. We can (without overloading) write functions for a whole set of different structs. We can extend existing structs without toughing them due to an additional
subclass.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 25 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
31/52
10.4. virtual Operations
c l a s s A {p u b l i c :
void f oo ( ) ;} ;
c la ss B : p u bl i c A {p u b l i c :
void f oo ( ) ;} ;
c la ss C : p u bl i c B {p u b l i c :void f oo ( ) ;
} ;
A o bj ec t1 ;B o bj ec t2 ;C o bj ec t3 ;
A o b j e c t4 = new A ( ) ;A o b j e c t5 = new B ( ) ;B o b j e c t6 = new C ( ) ;
o b j ec t 1 . f oo ( ) ; o b je c t2 . f oo ( ) ; o b je c t 3 . f oo ( ) ;object4 > f oo ( ) ; o bj ec t5 > f oo ( ) ; o bj ec t6 > f oo ( ) ;
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
32/52
Static Polymorphism
c l a s s A {
p u b l i c :void f oo ( ) ;} ;
c la ss B : p u bl i c A {p u b l i c :
void f oo ( ) ;} ;
A o b j e c t4 = new A ( ) ;
object4 > f oo ( ) ;
C++ implements a static polymorphism by default.
It is called static, as the (known) object type determines which operation is to beinvoked. There is a variant called dynamic polymorphism , too. Java, C# support only dynamic polymorphism .
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 27 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
33/52
Dynamic Polymorphism
c l a s s A {
p u b l i c :void f oo ( ) ;v i r t u a l void b ar ( ) ;
} ;
c la ss B : p u bl i c A {p u b l i c :
void f oo ( ) ;
v i r t u a l void b ar ( ) ;} ;
A o b j e ct 1 = new A ( ) ;B o b j e ct 2 = new B ( ) ;A o b j e ct 3 = new B ( ) ;
object1 > f o o ( ) ;
object2 > f o o ( ) ;object3 > f o o ( ) ;object1 > b ar ( ) ;object2 > b ar ( ) ;object3 > b ar ( ) ;
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 28 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
34/52
Dynamic Destructors
If you use inheritance, always make your destructor virtual .
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 29 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
35/52
Abstract Operations
c l a s s A {
} ;c la ss B : p u bl i c A {} ;
c la ss C : p u bl i c A {} ;
Let B and C be classes that belong together logically. An example: B writesmeasurements into a database, C plots data to Matlab.
However, B and C have to implementation in common. They share solely thesignature.
Consequently, all operations (in A) have to be virtual.
However, it does not make sense to provide any default implementation of anoperation in A.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 30 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
36/52
Abstract Operations
c l a s s A {
v i r t u a l void f oo ( ) = 0 ;} ;
c la ss B : p u bl i c A {v i r t u a l void f oo ( ) ;
} ;
c la ss C : p u bl i c A {
v i r t u a l void f oo ( ) ;} ;
virtual operations can be made abstract . A class with at least one abstract method is an abstract class . We cannot instantiate abstract types. This way, we enforce subclasses to implement them. Classes with solely abstract operations are called interface .
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 31 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
37/52
Case Study RevisitedAbstract Operations
c l a s s C e l e s t ia l B o d y {
. . .p u b l i c :v i r t u a l void plotToCout ( ) const = 0 ;
} ;c l as s Sun : p u b l i c C e le s ti a l Bo d y {
. . .p u b l i c :
v i r t u a l void plotToCout ( ) const ;
} ;c l as s P l an et : p u b l i c C e le s ti a l Bo d y {. . .
p u b l i c :v i r t u a l void plotToCout ( ) const ;
} ;
Ce les t i a lBody a = new C e l e st i a l B o d y ( ) ;
Ce les t i a lBody b = new Sun ( . . . ) ;a> p lo tToCout ( ) ;b> p lo tToCout ( ) ;
Implement plotToCout() for both subclasses. Does the code snippet from abovework?
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 32 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
38/52
10.5. static
Variable Lifecycle
void foo ( ) {i n t myInt ;. . .
}
void bar ( ) {s t a t i c i n t myInt ;
. . .}
Variables belong to a scope. Whenever we encounter a variable denition, we reserve memory. At the end of the scope, the variable is destroyed (invoke destructor, free memory).
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
39/52
Static Variables
void foo ( ) {
i n t myInt ;. . .}
void bar ( ) {s t a t i c i n t myInt ;. . .
}
Variables belong to a scope. Whenever we encounter a static variable denition for the rst time , we reserve
memory. A static variable is freed when the application terminates (invoke destructor, free
memory).
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 34 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
l f bl
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
40/52
An Example for Static Variables
void bar ( ) {
s t a t i c i n t m y In t = 2 0;myInt++;
}
. . .b ar ( ) ; / / f i r s t c a l l o f b ar ( ) e ve r b ar ( ) ;
b ar ( ) ;
Static variables are bind to the application, not to the scope. However, they are still accessible only within the scope. Usually, (good?) procedural programmers do not use static.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 35 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Obj V i bl
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
41/52
Object Variables
c l a s s A {
i n t f i e l d ;void f o o ( ) ;} ;
void A : : f o o ( ) { f i e l d + +; }
A ins tance1 , in s tance2 ;ins tance1 . foo ( ) ;
ins tance2 . foo ( ) ;
Both instances work on different instantiations of field .
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 36 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Cl V i bl (St ti )
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
42/52
Class Variables (Static)
c l a s s A {
s t a t i c i n t f i e l d ;void f o o ( ) ;} ;
void A : : f o o ( ) { f i e l d + +; }
A ins tance1 , in s tance2 ;ins tance1 . foo ( ) ;
ins tance2 . foo ( ) ;A : : f i e l d ++;
Here, static binds the attribute to the class. It is a class attribute . Depending on thevisibility, one can either access the static attribute within any operation of A, or evenoutside of the class (not recommended as it violates the encapsulation idea). If the eld
is public, the class acts (from a syntax point of view) similar to a namespace.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 37 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
E l I t C t
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
43/52
Example: Instance Counter
c l a s s A {
p r i v a t e :s t a t i c i n t ins tance s ;p u b l i c :
A ( ) ;} ;
void A : : A ( ) {in s tances++;
}
This code keeps track how many instances of A are created.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 38 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Static Operations
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
44/52
Static Operations
c l a s s A {
p u b l i c :void f oo ( ) ;s t a t i c voi d b ar ( ) ;
} ;
void A : : f o o ( ) { . . . }void A : : bar ( ) { . . . }
We can also declare methods as static . Static methods (class methods) belong to the class, not to an object. Static methods may work on class attributes only.
10. Object-oriented ProgrammingEinf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 39 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Case Study Revisited Static Operations
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
45/52
Case Study RevisitedStatic Operations
c l a s s C e l e s t ia l B o d y {
. . .p u b l i c :s t a t i c i n t getNumberOfCallsToGetMass ( ) ;double getMass () const ;
} ;c l as s Sun : p u b l i c C e le s ti a l Bo d y {
. . .} ;
c l as s P l an et : p u b l i c C e le s ti a l Bo d y {. . .} ;
Ce les t i a lBody a = new P l an e t ( ) ;Ce les t i a lBody b = new Sun ( . . . ) ;s td : : cout
8/13/2019 10 Object Oriented Programming
46/52
10.6. C++ vs. Java
Multiple Inheritance
c l a s s A {p u b l i c :
void f oo ( ) ;} ;c l a s s B {
p u b l i c :void b ar ( ) ;
} ;
c l as s C: p u b l i c A , B {} ;
C myC;myC. foo ( ) ; / / works myC. bar ( ) ; / / works
Multiple inheritance is supported by C++. Java does not have such a concept. Often, it is considered to be a bad smell , while inheriting from multiple interfaces is
not a bad smell. However, it is sometimes useful; in particular if it reects ideas of (AOP).
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 41 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Private Inheritance
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
47/52
Private Inheritance
c l a s s A {p u b l i c :
void f oo ( ) ;} ;
c la ss B : p r i v at e A {p u b l i c :
void b ar ( ) ;} ;
void B : : bar ( ) { f oo ( ) ; } / / works
B myB;myB. bar ( ) ; / / works myB. foo ( ) ; / / doesn t work
Private inheritance is supported by C++. Private downgrades the visibility of the super class. Here, the is-a relationship does not hold anymore, i.e. it is a pure implementation
inheritance, not a behavioural. However, today it is considered to be a bad smell . Java only supports public inheritance.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 42 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Explicit Call of Supertype
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
48/52
Explicit Call of Supertype
c l a s s A {p u b l i c :
v i r t u a l void f oo ( ) ;} ;
c la ss B : p r i v at e A {p u b l i c :
v i r t u a l void f oo ( ) ;} ;
void A : : f o o ( ) { / / do something }
void B : : f o o ( ) { A : : f oo ( ) ; }
B myB;myB. foo ( ) ;
We still can access the supertypes implementation, but only inside the subclass not from the outside. This is sometimes useful if we extend a function, i.e. add behaviour to an already
existing function (such as tracing).
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 43 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Ambiguity I
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
49/52
Ambiguity I
c l a s s B {i n t m y A t t r i b u t e ;void foo ( ) {
/ / wo rk s w i t h m y A t t r i b u t e }
} ;c l a s s C {
i n t m y A t t r i b u t e ;void bar ( ) {
/ / wo rk s w i t h m y A t t r i b u t e }
} ;c l as s D: p u b l i c B ,C {
void t a r ( ) {myAt t r i bu te ++; / / doesn t work
B : : m y A t t r i b u t e + +; C : : m y A t t r i b u t e + +; / / sh ou ld work }
} ;
D myD;myD. foo ( ) ;myD. bar ( ) ;
Class D holds two myAttribute instances.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 44 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Ambiguity III
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
50/52
Ambiguity III
c l a s s A {i n t m y A t t r i b u t e ;
} ;c la ss B : p u bl i c A {
void foo ( ) { / / wo rk s w i t h m y A t t r i b u t e
}} ;c la ss C : p u bl i c A {
void bar ( ) { / / wo rk s w i t h m y A t t r i b u t e
}} ;c l as s D: p u b l i c B ,C {} ;
D myD;myD. foo ( ) ;myD. bar ( ) ;
Question of the day: How many myAttribute instances does an object of type D have?
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 45 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Virtual Inheritance
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
51/52
c l a s s A {i n t m y A t t r i b u t e ;
} ;c l as s B : v i r t u a l p ub li c A {
void foo ( ) { / / wo rk s w i t h m y A t t r i b u t e
}} ;c l as s C: v i r t u a l p ub li c A {
void bar ( ) { / / wo rk s w i t h m y A t t r i b u t e
}} ;c las s D: v i r t u a l p ub l i c B ,C {} ;
D myD;myD. foo ( ) ;myD. bar ( ) ;
Theres also a concept called virtual inheritance , i.e. at runtime the system identies which attributes do exist.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 46 of 47
An OO Case Study Copy Constructor and Operators Inheritance virtual Operations static C++ vs. Java
Missing C++ Topics
http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl8/13/2019 10 Object Oriented Programming
52/52
g p
Generic programming Idioms of the standard library (iterators, e.g.) Standard library RTTI Exception handling (Design) patterns Outlook: Parallel programming
And then theres due to the course open issues, questions, and feedback.
10. Object-oriented Programming
Einf uhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 47 of 47
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