C++ 090 Overloading
C++ 090 Overloading
Impl_Tripdistance_A UDI C
Impl_Tripdistance
Program a
Data_Adapter l
c
calc_v alue() : v oid u
display () : v oid Imp_Triptime_Alarm l
ignition_on() : v oid Impl_Triptime_A larm_A UDI a
ignition_off() : void t
i
reset() : v oid Impl_Triptime o
<<?>> eeprom_changed() : ... n
need_recalc() : bool
Impl_Mean_C onsumption_A U DI S
Impl_Mean_C onsumption e
Impl_Average_Speed
clear()
Impl_Range
write_str()
<<draft>> w rite_sy mbol()
<<ctor>> init()
D
i
s
Display _Triptime_A larm_A U DI p
Display_Triptime_A larm l
Display_V alue a
O SEK-VGC y
(from SYS) show(v alue : dword) : v oid Display _Mean_C onsumtion_A U DI
Display _Mean_C onsumption S
e
c
t
i
O SEK-win32 Display_C urrent_C onsumption_A U DIo
Display _C urrent_C onsumption
(from SYS) n
"classes " of lefthand coloumns in the middel build up the trip computer core. The classes on the right side all
coloumn act as inerface to A ll code has to be independent of manufacturer. belong to the A UDI package
hareware and O S. They and are not part of the main class
are declared abstract in the The Impl_XXX and Display_XXX classes describe what flav ours of program hierarchy.
main class hierarchy and functionality are expected
implemented in several They are included to show where
SYS packages. the main class hierarchy has to be
expanded for manufactorer's
requirements
Content
Method Overloading
Understand operator overloading
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 2
Wrap-Up
Class hierarchies
Aggregation
Composition
Inheritance
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 3
C++ Name Spaces
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 4
Top level namespace
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 5
Definition and usage of namespace
Definition
namespace myNameSpace {
// Declarations
}
Using names from different namespaces
Permanently
using namespace myNameSpace;
Only for some names using namespace as prefix
myNameSpace::CCoordinate
“using” shouldn't be used in header files because all files including
the header file will (involuntarily) be affected
“using” may be used in a block (makes names available within
block)
Explains “preamble” for using I/O streams
cin, cout defined in namespace std
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 6
C++ Name Spaces
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 7
Extended CCoordinate Class
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 8
Extended Requirement 1 : Initialization
Q2(-1,1) Q1(1,1)
x 1 x
x P(x,y)
1
x x
Q3(-1,-1) Q4(1,-1)
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 9
C++ Method Overloading
Typical workarounds:
Naming conventions
Manual namespace (e.g. CComplex_set1, CComplex_set2)
hard to maintain and difficult to use
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 10
C++ Method Overloading
namespace::class::method(parameterset)
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 11
C++ Method Overloading
Declaration
//Alternative parameter for initialization
enum t_quadrant{Q1, Q2, Q3, Q4};
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 12
C++ Method Overloading
Implementation
CCoordinate::CCoordinate(float x, float y)
{
m_x = x;
m_y = y;
cout << "Generated object at adress " << this <<" with the values: "
<< m_x << " , " << m_y << endl;
}
CCoordinate::CCoordinate(t_quadrant quadrant)
{
switch (quadrant)
{
case Q1: m_x = 1; m_y = 1; break;
case Q2: m_x = -1; m_y = 1; break;
case Q3: m_x = -1; m_y = -1; break;
case Q4: m_x = 1; m_y = -1; break;
}
cout << "Generated object at adress " << this <<" with the values: "
<< m_x << " , " << m_y << endl;
}
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 13
C++ Method Overloading
Call
//Old convention with default parameters
CCoordinate c1(2,2);
CCoordinate c2;
CCoordinate c4(5);
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 14
C++ Method Overloading
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 15
C++ Operator Overloading
CCoordinate c1(3,4);
CCoordinate c2(4,5);
CCoordinate c3;
c3 = c1+c2
../myCode/main.cpp:23:10: error: no match for ‘operator+’ in ‘c1 +
c2’
../myCode/main.cpp:23:10: note: candidates are:
/usr/include/c++/4.6/bits/stl_iterator.h:327:5: note:
template<class _Iterator> std::reverse_iterator<_Iterator>
std::operator+(typename
std::reverse_iterator<_Iterator>::difference_type, const
std::reverse_iterator<_Iterator>&)
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 16
C++ Operator Overloading
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 17
C++ Operator Overloading
Declaration
right operant
left operant
overwritten operator
return type
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 18
C++ Operator Overloading
Implementation
CCoordinate operator+(const CCoordinate& c1, const CCoordinate& c2)
{
CCoordinate result;
result.m_x = c1.m_x + c2.m_x;
result.m_y = c1.m_y + c2.m_y;
return result;
}
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 19
C++ Operator Overloading
Declaration
right operant
left operant
overwritten operator
return type
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 20
C++ Operator Overloading
Alternative format – member function
right operant
overwritten operator
return type
Left operand: this pointer
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 21
C++ Operator Overloading
How about:
c6 = c1 + 4;
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 22
C++ Operator Overloading
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 23
Combining Operator Overloading and
Namespace
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 24
Rules for C++ Operator Overloading
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 25
List of overloadable binary operators
Binary:
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 26
Overloading using methods
When called, the left operand becomes the object the method is
invoked for, the right operand is passed as the parameter
c1 + c2 → c1.operator+(c2)
When called, the left operand becomes the object the method is
invoked for, the right operand is passed as the parameter
c1 + c2 → c1.operator+(c2)
How about
CCoordinate c1(1,2);
c1++;
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 29
Unary Operators
Example
public:
// Some unary operators - implemented as member, no friend needed
CCoordinate& operator++(); // Prefix increment operator.
CCoordinate operator++(int); // Postfix increment operator.
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 30
List of overloadable unary operators
Unary:
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 31
<< Operator
CCoordinate c1(3,4);
cout << c1 << endl;
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 32
<< Operator
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 33
Overloading of operators and methods
completeness
intuitive use
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 34
Operator overloading „must“
Life demo
Add assignment operator to CMatrix
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 35
Implementation of Copy Constructor and
Assignment Operator
Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 36
Copy constructor, destructor, assignment
operator
Rule
Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 37
Wrap-Up
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 38
Proposal for own studies
Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 39