Post on 30-Mar-2020
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
8. Object-oriented Programming
June 15, 2010
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 1 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Outline
• Recapitulation
• Copy Constructor & Operators
• Object-oriented Programming
• Dynamic and Static Polymorphism
• The Keyword Static
• The Singleton Pattern
• Generic Programming
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 2 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
—8.1. Brain Teaser—
c lass S t r i n g {p r i v a t e :
/ / a in ’ t a 0−te rminated s t r i n gchar∗ data ;/ / l eng th o f s t r i n gi n t l en g t h ;
p u b l i c :S t r i n g ( ) ;vo id copyFromOtherStr ing ( const S t r i n g& s t r i n g ) ;
} ;
. . .
S t r i n g : : S t r i n g ( ) :data ( 0 ) ,l e ng th ( 0 ) {}
vo id S t r i n g : : copyFromOtherStr ing ( const S t r i n g& s t r i n g ) {data = s t r i n g . data ;l e ng th = s t r i n g . l e ng th ; s
}
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 3 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
—8.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 myDate ( anotherDate ) ;
• Sometimes, we have to rewrite these default operations.• For the copy constructor, it is very simple.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 4 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Copy Constructor for a String Class
c lass S t r i n g {p r i v a t e :
/ / a in ’ t a 0−te rminated s t r i n gchar∗ data ;/ / l eng th o f s t r i n gi 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 r i n g& s t r i n g ) ;
} ;
. . .
S t r i n g : : S t r i n g ( ) :data ( 0 ) ,l en g t h ( 0 ) {}
S t r i n g : : S t r i n g ( const S t r i n g& s t r i n g ) :data ( 0 ) ,l en g t h ( s t r i n g . l e ng th ) {data = new . . . ;
f o r ( . . . ) {. . .
}}
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 5 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Excursus: C++ Strings
• Good news: You now know how to write a copy constructor for your personal stringclass.
• Bad news: Such a class does already exist.
# inc lude <s t r i n g>
std : : s t r i n g myString0 ;s td : : s t r i n g myString1 ( ” h e l l o wor ld ” ) ;s td : : s t r i n g myString2 ( myString1 ) ;s td : : s t r i n g myString3 = ” h e l l o MPG” ;s td : : s t r i n g myString4 = myString3 ;myStr ing . leng th ( ) ;myString4 += myString1 ;
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 6 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Default Operations
• The following operations are generated automatically as public operations if wedon’t provide our own implementation:
• Default constructor.• Destructor.• Copy constructor.
• However, there’s still another pitfall:
MyStr ing a ;/ / do something wi th a ;MyStr ing b ( a ) ; / / worksMyStr ing c ;c = a ; / / does not work
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 7 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Operators
• In C++, operators such as +,-,+=,=,¡¡ are just methods, too.• The = operators (assignment operator) is generates automatically, if we don’t
overwrite it.• It then is a bit-wise copy of the object.• We can redefine the operators.• We now know, what
std : : cout << ” s tup id ” << s td : : endl
means.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 8 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Operators Redefined—Part I
c lass Vector {p u b l i c :
/ / copy cons t ruc to rVector ( const Vector& vec to r ) ;
Vector& opera tor =( const Vector& vec to r ) ;/ / unary opera torsVector& opera tor +=( const Vector& vec to r ) ;Vector& opera tor ∗=( const double& value ) ;
vo id toStream ( std : : ostream& out ) const ;} ;
/ / b ina ry opera torsVector& opera tor +( const Vector& lhs , const Vector& rhs ) ;s td : : ostream& operator<<(s td : : ostream& out , const Vector& vec to r ) ;
. . .Vector& opera tor +( const Vector& lhs , const Vector& rhs ) {
Vector r e s u l t ( l hs ) ;r e s u l t += rhs ;r e t u r n r e s u l t ;
}
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 9 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Operators Redefined—Part II
c lass Vector {p u b l i c :
/ / copy cons t ruc to rVector ( const Vector& vec to r ) ;
Vector& opera tor =( const Vector& vec to r ) ;/ / unary opera torsVector& opera tor +=( const Vector& vec to r ) ;Vector& opera tor ∗=( const double& value ) ;
vo id toStream ( std : : ostream& out ) const ;} ;
/ / b ina ry opera torsVector& opera tor +( const Vector& lhs , const Vector& rhs ) ;s td : : ostream& operator<<(s td : : ostream& out , const Vector& vec to r ) ;
. . .s td : : ostream& operator<<(s td : : ostream& out , const Vector& vec to r ) {
vec to r . toStream ( out ) ;r e t u r n out ;
}
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 10 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
—8.3. Inheritance—
• Sometimes, someone hands you over an object, but this object lacks somefunctionality.
• Now, it is up to you to add this functionality.• However, what happens if there is different variants of this functionality?
• Image you write a researcher management application.• Let Employee be a multi-purpose class for bookkeeping.• Task: Add a function setSalary() and getTaxesPaid .• However: There are also people getting a stipend. These guys do not pay taxes.• Writing two classes is not a variant, as all employees have to have a getWeight()
operation.
Challenge: If we duplicate the code, then multiple operations have to be rewritten:
bool isBMIAboveThreshold ( ) {. . .
}
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 11 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Challenge Rewritten
• A Stipend is an extension of Employee.• A PostDoc is an extension of Employee, too.• Stipdend and PostDoc are two different things.• Whatever we do with an Employee, we can do with a Stipend and PostDoc, too.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 12 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
“is a”—Relations
AbstractDB
RealDatabase HardcodedTestDB
getEntry()
• Student has all operations and attributes, Employee has.• Every operation expecting an instance of Employee could also be passed an
instance of Student.• Again, we could image this to be an extension of a cut-n-paste mechanism.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 13 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
“is a”—Relations in Source Code (Part I)
c lass Employee {p r i v a t e :
double weight ; ;p u b l i c :
bool isBMIAboveThreshold ( ) ;} ;
c lass St ipend : p u b l i c Employee {p r i v a t e :p u b l i c :
double getSt ipdend ( ) const ;} ;
. . .St ipend myStipend ;myStipend . isBMIAboveThreshold ( ) ; / / t h a t ’ s f i n e
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 14 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
“is a”—Relations in Source Code (Part II)
c lass Employee {. . .
} ;
c lass St ipend : p u b l i c Employee {. . .
} ;
vo id foo ( const Employee& employee ) { . . . }
Stipend myStipend ;foo ( myStipend ) ;
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 15 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Object-oriented Programming
• is-a relations are mapped to inheritance.• Object-based programming becomes object-oriented programming due to this
inheritance concept.• Whenever an object of type A is expected somewhere, we can also pass an object
with a subtype of B. This concept is called polymorphism.• There’s a new visibility mode protected: These attributes and operations behave
like private, i.e. they are not visible from outside, but they are visible withinsubclasses.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 16 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
—8.4. virtual Operations—
c lass A {p u b l i c :
vo id foo ( ) ;} ;
c lass B : p u b l i c A {p u b l i c :
vo id foo ( ) ;} ;
c lass C: p u b l i c B {p u b l i c :
vo id foo ( ) ;} ;
A ob jec t1 ;B ob jec t2 ;C ob jec t3 ;A∗ ob jec t4 = new A ( ) ;A∗ ob jec t5 = new B ( ) ;B∗ ob jec t6 = new C ( ) ;
ob jec t1 . foo ( ) ; ob jec t2 . foo ( ) ; ob jec t3 . foo ( ) ;ob ject4−>foo ( ) ; ob ject5−>foo ( ) ; ob ject6−>foo ( ) ;
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 17 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Static Polymorphism
c lass A {p u b l i c :
vo id foo ( ) ;} ;
c lass B : p u b l i c A {p u b l i c :
vo id foo ( ) ;} ;
A∗ ob jec t4 = new A ( ) ;
ob ject4−>foo ( ) ;
• C++ implements a static polymorphism by default.• It is called static, as the (known) object type determines which operation is to be
invoked.• There is a variant called dynamic polymorphism, too.• Java, C# support only dynamic polymorphism.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 18 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Dynamic Polymorphism
c lass A {p u b l i c :
vo id foo ( ) ;v i r t u a l vo id bar ( ) ;
} ;
c lass B : p u b l i c A {p u b l i c :
vo id foo ( ) ;v i r t u a l vo id bar ( ) ;
} ;
A∗ ob jec t1 = new A ( ) ;B∗ ob jec t2 = new B ( ) ;A∗ ob jec t3 = new B ( ) ;
ob ject1−>foo ( ) ;ob ject2−>foo ( ) ;ob ject3−>foo ( ) ;ob ject1−>bar ( ) ;ob ject2−>bar ( ) ;ob ject3−>bar ( ) ;
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 19 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Dynamic Destructors
If you use inheritance, always make your destructor virtual.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 20 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Finally: Super Constructor
c lass A {p u b l i c :
A( i n t v a r i a b l e ) ;} ;
c lass B : p u b l i c A {p u b l i c :
B ( ) ;} ;
. . .
B : : B ( ) :A( 23 ) {
}
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 thesupertype’s constructor, then you have to initialise your attributes in the right order.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 21 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Excursus: Multiple Inheritance
c lass A {} ;c lass B {} ;
c lass C: p u b l i c A,B {} ;
• Multiple inheritance is supported by C++.• However, today it is considered to be a bad smell.• There’s also a concept called virtual inheritance which is also considered to be a
bad smell nowadays.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 22 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Excursus: Private Inheritance
c lass A {} ;c lass B {} ;
c lass C: p r i v a t e A,B {} ;
• Private inheritance is supported by C++.• 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. I nevertheless find is useful.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 23 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Abstract Operations
c lass A {} ;
c lass B : p u b l i c A {} ;
c lass C: p u b l 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 an
operation in A.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 24 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Abstract Operations
c lass A {v i r t u a l vo id foo ( ) = 0 ;
} ;
c lass B : p u b l i c A {v i r t u a l vo id foo ( ) ;
} ;
c lass C: p u b l i c A {v i r t u a l vo id foo ( ) ;
} ;
• vitual 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.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 25 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
—8.5. static—
Variable Lifecycle
vo id foo ( ) {i n t myInt ;. . .
}
vo id bar ( ) {s t a t i c i n t myInt ;. . .
}
• Variables belong to a scope.• Whenever we encounter a variable definition, we reserve memory.• At the end of the scope, the variable is destroyed (invoke destructor, free memory).
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 26 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Static Variables
vo id foo ( ) {i n t myInt ;. . .
}
vo id bar ( ) {s t a t i c i n t myInt ;. . .
}
• Variables belong to a scope.• Whenever we encounter a static variable definition for the first time, we reserve
memory.• A static variable is freed when the application terminates (invoke destructor, free
memory).
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 27 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
An Example for Static Variables
vo id bar ( ) {s t a t i c i n t myInt = 20;
myInt ++;}
. . .bar ( ) ; / / f i r s t c a l l o f bar ( ) everbar ( ) ;bar ( ) ;
• Static variables are bind to the application, not to the scope.• However, they are still accessible only within the scope.• Usuall, (good?) procedural programmers do not use static.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 28 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Object Variables
c lass A {i n t f i e l d ;vo id foo ( ) ;
} ;
vo id A : : foo ( ) { f i e l d ++; }
A instance1 , ins tance2 ;ins tance1 . foo ( ) ;ins tance2 . foo ( ) ;
Both instances work on different instantiations of field.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 29 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Class Variables (Static)
c lass A {s t a t i c i n t f i e l d ;vo id foo ( ) ;
} ;
vo id A : : foo ( ) { f i e l d ++; }
A instance1 , ins 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 fieldis public, the class acts (from a syntax point of view) similar to a namespace.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 30 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Example: Instance Counter
c lass A {p r i v a t e :
s t a t i c i n t ins tances ;p u b l i c :
A ( ) ;} ;
vo id A : : A ( ) {i ns tances ++;
}
This code keeps track how many instances of A are created.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 31 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Static Operations
c lass A {p u b l i c :
vo id foo ( ) ;s t a t i c vo id bar ( ) ;
} ;
vo id A : : foo ( ) { . . . }vo id 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.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 32 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
A Factory Method
c lass A {p r i v a t e :
i n t a t t r i b u t e ;p u b l i c :
s t a t i c A createAnAFromTerminal ( ) ;} ;
s t a t i c A createAnAFromTerminal ( ) {A r e s u l t ;s td : : c in >> r e s u l t . a t t r i b u t e ;r e t u r n A ;
}
. . .A myA;myA = A : : createAnAFromTerminal ( ) ;
• Static methods may manipulate any attribute of the class as they are part of theclass.
• Static methods are called similar to methods wrapped by a namespace.• Static methods typically represent operations on sets while methods represent
operations on an element of a set.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 33 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
—8.6. Design Patterns—
Standard book by the gang of four.
• Design Patterns: Stem from reocurringsolutions for certain technicalchallenges.
• More complex than idioms.• Something different than an algorithm:
It describes the technical realisation.• Famous book by the gang of four.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 34 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
A Singleton
/∗∗∗ Implements a connect ion to a∗ database ( b ig f i l e ) w i th l o t s o f∗ mate r i a l constants .∗ /
c lass Mater ia lConstants {} ;
We want to ensure that MaterialConstantsis created only once throughout the applica-tion lifecycle. Here are some hints:• MaterialConstants should not have a
public (default) constructor.• As anyone could need the database, a
static getDatabase() operation mightbe helpful that returns an instance ofMaterialConstants.
• This static operation might hold aninstance of MaterialConstants.However, it should not create it eachtime it is invoked.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 35 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Singleton Implementation - Part I
c lass Mater ia lConstants {p r i v a t e :
/∗∗∗ noone should be able i n s t a n t i a t e the database on h is own∗ /
Mater ia lConstants ( ) ;/∗∗∗ noone should be able to copy a database∗ /
Mater ia lConstants ( const Mater ia lConstants &) ; / / noone should be ablep u b l i c :
i n t ge tEnt ry ( ) const ;vo id addEntry ( . . . ) ;
} ;
. . .Mater ia lConstants myDatabase ; / / fo rb idden
/ / however , we need something l i k emyDatabase . ge tEnt ry ( ) ;
We could make all the operations static. However, then we don’t have control of theopen process. Even worse, this won’t fit to our fancy new object-oriented paradigm.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 36 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Singleton Implementation - Part II
c lass Mater ia lConstants {p r i v a t e :
/∗∗∗ noone should be able i n s t a n t i a t e the database on h is own∗ /
Mater ia lConstants ( ) ;/∗∗∗ noone should be able to copy a database∗ /
Mater ia lConstants ( const Mater ia lConstants &) ; / / noone should be ablep u b l i c :
. . .
/∗∗∗ Provide access to one s i n g l e database ( s i ng l e t on )∗ /
s t a t i c Mater ia lConstants& getDatabase ( ) ;} ;
. . .Mater ia lConstants : : getDatabase ( ) . ge tEnt ry ( ) ; / / f i n eMater ia lConstants myConstants =
Mater ia lConstants : : getDatabase ( ) ; / / fo rb idden
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 37 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Singleton Implementation - Part III
c lass Mater ia lConstants {p r i v a t e :
. . .p u b l i c :
. . .
/∗∗∗ Provide access to one s i n g l e database ( s i ng l e t on )∗ /
s t a t i c Mater ia lConstants& getDatabase ( ) ;} ;
Mater ia lConstants& Mater ia lConstants : : getDatabase ( ) {Mater ia lConstants r e s u l t ;r e t u r n r e s u l t ;
}
This simple realisation of the static operation works. However, it creates asegmentation fault.
If we removed the reference symbol, it would create one instanceand one copy per call. That is not what we want.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 38 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Singleton Implementation - Part III
c lass Mater ia lConstants {p r i v a t e :
. . .p u b l i c :
. . .
/∗∗∗ Provide access to one s i n g l e database ( s i ng l e t on )∗ /
s t a t i c Mater ia lConstants& getDatabase ( ) ;} ;
Mater ia lConstants& Mater ia lConstants : : getDatabase ( ) {Mater ia lConstants r e s u l t ;r e t u r n r e s u l t ;
}
This simple realisation of the static operation works. However, it creates asegmentation fault. If we removed the reference symbol, it would create one instanceand one copy per call. That is not what we want.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 38 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
Singleton Implementation - Part IV
c lass Mater ia lConstants {p r i v a t e :
. . .p u b l i c :
. . .
/∗∗∗ Provide access to one s i n g l e database ( s i ng l e t on )∗ /
s t a t i c Mater ia lConstants& getDatabase ( ) ;} ;
Mater ia lConstants& Mater ia lConstants : : getDatabase ( ) {s t a t i c Mater ia lConstants r e s u l t ;r e t u r n r e s u l t ;
}
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 39 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
A Factory Pattern
• Study our simple MaterialConstantstype.
• This shall be part of a project wherewe are promised to get access to thereal database by the end of theproject. Meanwhile, we have to workwith a small number of artificialmaterial constants.
• Could be switch from one database toanother without changing anything inthe code using the material database?
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 40 of 41
Brain Teaser Copy Constructor and Operators Inheritance virtual Operations static Design Patterns
A Factory Pattern
AbstractDB
RealDatabase HardcodedTestDB
getEntry()
• We define an abstract Database type. It is an interface (see UML diagram).• This type is delivered by our singleton, i.e. codes work only with the interface.• We implement the interface with a stupid class where we hardcode some material
constants (basically a big case statement).• Within the static operation, we then decide whether we deliver a real database or
our artificial one.
8. Object-oriented Programming
Introduction to C/C++, Tobias Weinzierl page 41 of 41