• No results found

Programsystemkonstruktion med C++: ¨Ovning 2

N/A
N/A
Protected

Academic year: 2021

Share "Programsystemkonstruktion med C++: ¨Ovning 2"

Copied!
19
0
0

Loading.... (view fulltext now)

Full text

(1)

Programsystemkonstruktion med C++: ¨Ovning 2

Programsystemkonstruktion med C++: ¨ Ovning 2

Karl Palmskog palmskog@kth.se

september 2010

(2)

Programsystemkonstruktion med C++: ¨Ovning 2 Specalfunktioner in classes

Constructor

constructors of a class initializes class members

initial values can be given in the colon list or the code block default constructor is not used if the constructor is declared default constructor runs members constructor

Copy Constructor takes an instance of the class as an argument

Copy Constructor copies the members

c l a s s V e h i c l e {

p r i v a t e : i n t w e i g h t ; p u b l i c :

V e h i c l e ( i n t w) : w e i g h t (w) {}

} ;

(3)

Programsystemkonstruktion med C++: ¨Ovning 2 Specalfunktioner in classes

Destructor

destructor takes no arguments and does not return anything default destructor is used if not defined

runs members destructor

P e r s o n : : ˜ P e r s o n ( ) {

// f r e e up memory f o r a r r a y o f heap−a l l o c a t e d s k i l l s d e l e t e [ ] s k i l l s ;

}

(4)

Programsystemkonstruktion med C++: ¨Ovning 2 Specalfunktioner in classes

Assignment operators and the Rule of Three

the class instance variable is called assignment operator function operator=

the default operator= copies the members heuristics:

1 if you need to define the destructor you need to define Copy Constructor operator=

2 if you need to define the Copy Constructor you need to define operator=

3 if you need to define the operator= you need to define the copy constructor

destructor, copy and operator= are “associated”

(5)

Programsystemkonstruktion med C++: ¨Ovning 2 Specalfunktioner in classes

An inheritance hierarchy

Animal string name

Donkey Horse

Mule

(6)

Programsystemkonstruktion med C++: ¨Ovning 2 Specalfunktioner in classes

Special functions and inheritance

Objekt Konstruktor Destruktor Animal Animal ˜Animal

Donkey Animal ˜Donkey

Donkey ˜Animal

Horse Animal ˜Horse

Horse ˜Animal

Mule Animal ˜Mule

Donkey ˜Horse

Animal ˜Animal

Horse ˜Donkey

Mule ˜Animal

(7)

Programsystemkonstruktion med C++: ¨Ovning 2 Virtual Functions

Virtual Functions

non-virtual functions are selected statically at compile time, depending on the type of pointer or reference

virtual functions are determined dynamically during execution based on the object’s actual type

#i n c l u d e <i o s t r e a m >

u s i n g namespace s t d ; c l a s s A {

p u b l i c :

v o i d f o o ( ) { c o u t << ”A : : f o o ( ) ” << e n d l ; }

v i r t u a l v o i d b a r ( ) { c o u t << ”A : : b a r ( ) ” << e n d l ; } } ;

c l a s s B : p u b l i c A { p u b l i c :

v o i d f o o ( ) { c o u t << ”B : : f o o ( ) ” << e n d l ; } v o i d b a r ( ) { c o u t << ”B : : b a r ( ) ” << e n d l ; } } ;

(8)

Programsystemkonstruktion med C++: ¨Ovning 2 Virtual Functions

Virtual functions in practice

#i n c l u d e ” ab . h ” i n t main ( ) {

B b ;

B ∗ b P t r = &b ; A ∗ a P t r = b P t r ; b P t r −>f o o ( ) ; b P t r −>b a r ( ) ; a P t r −>f o o ( ) ; a P t r −>b a r ( ) ; r e t u r n 0 ; }

$ . / a . o u t B : : f o o ( ) B : : b a r ( ) A : : f o o ( ) B : : b a r ( )

(9)

Programsystemkonstruktion med C++: ¨Ovning 2 Virtual Functions

Implementation of virtual functions

Mule v-table {...}

V-table

*func1

*func2

(10)

Programsystemkonstruktion med C++: ¨Ovning 2 Virtual Functions

Virtual destructor

What is the problem with this code?

c l a s s A { p u b l i c :

˜A ( ) { s t d : : c o u t << ” ˜A ( ) ” << s t d : : e n d l ; } } ;

c l a s s B : p u b l i c A { p u b l i c :

˜B ( ) { s t d : : c o u t << ” ˜B ( ) ” << s t d : : e n d l ; } } ;

i n t main ( ) {

A∗ a = new A ( ) ; A∗ b = new B ( ) ;

d e l e t e a ; d e l e t e b ; }

(11)

Programsystemkonstruktion med C++: ¨Ovning 2 Virtual Functions

Pure virtual function

if a function is pure virtual, the classes are implemented by inheritance

correspond to abstract methods in Java

c l a s s A { p u b l i c :

v i r t u a l i n t f o o ( ) = 0 ; . . .

} ;

c l a s s B : p u b l i c A { p u b l i c :

v i r t u a l i n t f o o ( ) {

. . . }

. . . } ;

(12)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

Templates

templates (models) used to write type independednt code inheritance is an alternative to shared behaviors

functions and classes parameterized on a given type

compiler replaces occurrences of type parameters and create

parameter free function/class

(13)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

c l a s s S t a c k { p r i v a t e :

s t a t i c c o n s t i n t STK SIZE = 1 0 0 ; i n t m s t k [ STK SIZE ] ;

i n t m top ; p u b l i c :

S t a c k ( ) : m top ( 0 ) {}

b o o l i s E m p t y ( ) { r e t u r n ( m top == 0 ) ; } b o o l i s F u l l ( ) { r e t u r n ( m top >= STK SIZE ) ; } b o o l p u s h ( i n t v a l ) {

i f ( i s F u l l ( ) ) r e t u r n f a l s e ; m s t k [ m top++] = v a l ; r e t u r n t r u e ;

}

b o o l pop ( i n t &v a l ) {

i f ( i s E m p t y ( ) ) r e t u r n f a l s e ; v a l = m s t k [−−m top ] ;

r e t u r n t r u e ; }

} ;

(14)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

t e m p l a t e <typename T>

c l a s s S t a c k { p r i v a t e :

s t a t i c c o n s t i n t STK SIZE = 1 0 0 ; T m s t k [ STK SIZE ] ;

i n t m top ; p u b l i c :

S t a c k ( ) : m top ( 0 ) {}

b o o l i s E m p t y ( ) { r e t u r n ( m top == 0 ) ; } b o o l i s F u l l ( ) { r e t u r n ( m top >= STK SIZE ) ; } b o o l p u s h ( c o n s t T &v a l ) {

i f ( i s F u l l ( ) ) r e t u r n f a l s e ; m s t k [ m top++] = v a l ; r e t u r n t r u e ;

}

b o o l pop (T &v a l ) {

i f ( i s E m p t y ( ) ) r e t u r n f a l s e ; v a l = m s t k [−−m top ] ;

r e t u r n t r u e ; }

} ;

(15)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

Use of template class

#i n c l u d e <i o s t r e a m >

#i n c l u d e ” s t a c k . h ” i n t main ( ) {

S t a c k <i n t > i s t k ; // s t a c k o f i n t

S t a c k <double> f s t k ; // s t a c k o f d o u b l e s i s t k . p u s h ( 4 7 1 1 ) ;

i s t k . p u s h ( 4 7 . 1 1 ) ; f s t k . p u s h ( 4 7 . 1 1 ) ; f s t k . p u s h ( 4 7 1 1 ) ; i n t i t o p ;

i s t k . pop ( i t o p ) ;

s t d : : c o u t << i t o p << s t d : : e n d l ; r e t u r n 0 ;

}

(16)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

A template maxfunction

Exam Assignment: write a template function max that returns the larger of two arguments.

c o n s t & T max ( c o n s t T & x , c o n s t T & y ) { i f ( x < y ) {

r e t u r n y ; } e l s e {

r e t u r n x ; }

}

F¨ oljdfr˚ agor:

what type parameter functionof the arguments is required?

why is the return type of reference?

why can not you give arguments of different types?

(17)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

Meta Programming with templates

#i n c l u d e <i o s t r e a m >

t e m p l a t e<i n t N>

s t r u c t f a c t {

enum { v a l u e = N ∗ f a c t <N−1 >:: v a l u e } ; } ;

t e m p l a t e<> s t r u c t f a c t <0> { enum { v a l u e = 1 } ;

} ;

i n t main ( ) {

s t d : : c o u t << f a c t <5 >:: v a l u e << s t d : : e n d l ; r e t u r n 0 ;

}

(18)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

Compilation of templatiserade classes and functions

template classes/functions has to be defined before use usually occurs because both the declaration and definition of .h-file

compiler need not be given the name of the class file

support for separate production of the definition/declaration

varies between compilers

(19)

Programsystemkonstruktion med C++: ¨Ovning 2 Templates

Templatisering Vs. common super class

Templates common super class

+ code reuse + code reuse

+ static type control + flexibility

+ effective implementation - type control at run time - definition before use - overhead

- slow compilation

- large binary file

References

Related documents

en C++-klass best˚ ar av en deklaration och en definition deklaration vanligtvis i .h (.hh) och definition i .cpp (.cc) private ¨ ar f¨ orvalt f¨ or funktioner och variabler i

undvik enraders if-satser, anv¨ and alltid { och } undvik om m¨ ojligt n¨ astlade if/for satser skriv inte f¨ or l˚ anga funktioner, dela upp. versionshantera all kod med program

Ett f¨ oretag erbjuder en l¨ osning d¨ ar den svenska och kinesiska kalendern ¨ arver fr˚ an en gemensam basklass.. De h¨ avdar att de anv¨ ander sig av polymorfi och det g¨ or

argv[0] är programmets namn (tex /usr/bin/zip eller zip) main() är en global funktion och således inte del av någon klass. main() behöver

Virtuella funktioner bestäms dynamiskt under exekvering baserat på objektets faktiska

Använd std-funktioner, algoritmer etc Optimera där

Thesis Booklet for Diploma Project Karin Andreasson Spring 2017 KTH School of Architecture Studio 7 Supervisors: Elizabeth Hatz and Peter Lynch.. Exterior of Mole

Metal oxide nanostructures are of interest as electrode materials for sensor applications (Fig. 1b) and energy conversion because of; (1) they provide large surface area