C++ 030 Objects
C++ 030 Objects
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
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 2
Wrap-Up
Eclipse Environment
Setting up a project
Writing some code
Adding some files
Compiling
Debugging
Running
Use cases
First Glance at Together
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 3
Example: Coordinate Class
Task:
P=(x|y)
x
r
ϕ
x
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 4
Requirements
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 5
Adding some complexity
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 6
From Requirements to Design and Code
Next steps
Design the library
Generate the code framework
Start implementing the methods
Start implementing some testcases
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 7
C vs. C++ Implementation
C Implementation
Declares a struct for coordinate
Declares a struct for every shape
Implement functions that use the structures (or pointers to the
structures) as parameters or return values
C++ Implementation
Identify the objects: coordinate, rectangle, triangle etc.
Declare classes that describe the objects' properties
Implement the behaviour
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 8
C-struct versus C++ class
// triangle // triangle
struct triangle { class CTriangle {
struct coordinate corner1, CCoordinate corner1, corner2,
corner2, corner3; corner3;
... ...
} }
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 9
Objects and Classes
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 10
Header and Source File Format
#endif
#endif
Technically it is possible to
have the code for the class declaration and implementation in one
single file
implement several classes in one file
to call the file different from the class
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 13
Our first C++ example
Based on the Use Case Diagram for the Coordinate, we are now going
to implement a corresponding class which implements these Use Cases
Life demo
Using Together
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 14
Class CCoordinate
Name
Attributes
Methods
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 15
UML Class Diagram
class CCoordinate
{
private:
float m_x;
float m_y;
public:
CCoordinate(int x = 0, int y =
void setCartesian(float x, float y);
void setPolar(float r, float phi);
//... Some methods missing
private:
void calcY(float r, float phi);
void calcX(floar r, float phi);
void calcPhi();
float calcR();
};
Starting
Open Together
by doubleclicking on the .tpr file in the myCode folder
by starting Together and opening a project (.tpr file)
Together will look for all C++ classes and diagram files in the parent
and all child folders and display them in the Model Explorer
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 17
Using Together – Summary
Adding properties
All diagram elements have additional properties, which can (and
should) be added in the Property Explorer
This increases readability of the diagram and can be used for
document and code generation
Possible Problems
If your class diagrams have an extra compartment, go to “Tool |
Options | Project Level”, then to “View Management | JavaBeans / C+
+ Properties” and deselect “Recognize C++ properties”
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 18
Some coding rules (for our course)
A class has the prefix C and starts with a capital letter, e.g. CMyClass
One class per header (.h) and source (.cpp) file
Name of the header and source files are equal to the name of the
class (e.g. CMyClass.h)
Member attributes have the prefix m_ to distinguish them from
parameters and local variables. They start with a small letter. Capital
letters are used to separate words (camel case) e.g.
m_myColorAttribute
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 19
UML Class Diagram
Alternative representation
Diagram format:
Implementation
Language
Use
Good for detailed specification
Should be avoided for class
hierarchies representations (i.e.
if you have more than 1-2
classes in the diagram)
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 20
Visibility and Storage of Attributes
class complex {
private:
float mf_imaginary;
static int i_numberElements;
float mf_real;
public:
static int i_printdebug;
…
}
Visibility:
private (-): Only accessible from own class operations
protected (#): Accessible from class operations and derived objects
public (+): Accessible from all objects
Storage:
“member”: Every object gets own instance (=allocated memory)
static (__): Same instance accessed from all objects
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 21
Visibility and Storage of Operations
class complex {
public:
void mv_addComplex();
void mv_switchReIm();
void mv_setComplex();
void mv_divComplex();
};
Visibility:
identical to attributes
Storage:
“member”: Every object gets own instance
static (__): Method without “this” pointer (C style function)
virtual (italic): May be overridden Polymorphism
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 22
Most common conventions for scoping
Attributes
Private access only via getter and setters
Protected direct (fast) access by specialized classes
Public Should be avoided
Operations
Public For all API functions
Private For all internal helper functions
Protected If needed by specialized class
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 23
Objects and Classes
Object
CCoordinate c1(2,2);
CCoordinate c2;
CCoordiante c4(5);
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 24
Frequently used libraries
Example
#include <iostream>
#include <string>
#include <math.h>
#include “myClass.h”
using namespace std;
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 25
Recipe for implementing a class
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 26
Non-Functional Requirements
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 27
Non-Functional Requirements
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 28
Impact of non functional requirements
Scenario A
The memory consumption (RAM) of an object of the class must be
below 4 bytes.
Scenario B
The implementation of the getter routines must be as fast as possible.
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 29
Wrap-Up
UML
Use case diagram
Class diagram (basic)
C++
Classes
Objects
Requirements
Functional Requirements
Non-Functional Requirements
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 30
Proposal for own studies
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 31