0% found this document useful (0 votes)
6 views

C++ 030 Objects

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C++ 030 Objects

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Advanced Programming Techniques

Basics of Classes, Objects and Attributes and Operations


State_C ontrol
Signal_A dapter C
State_C ontrol_A UDI o
on_ select_prev () n
init() on_select_next() t
raise_tick() on_tick() on_select_next() r
raise_select_prev () on_triptime_alarm() on_tick() o
raise_select_next() 1 on_mem_lev el_dow n() on_triptime_alarm() l
raise_triptime_alarm() on_mem_lev el_up() on_ select_prev ()
raise_ignition_on() on_mem_lev el_dow n() S
on_ignition_on() e
raise_ignition_off() on_ignition_off() on_mem_lev el_up() c
raise_reset_program() on_reset_program() on_ignition_off() t
raise_reset_all_programs() on_reset_all_programs() on_ignition_on() i
raise_eeprom_changed() on_eeprom_changed() reset_program() o
raise_memlev el_down() reset_all_programs() n
<<ctor>> init()
raise_memlev el_up() Create()

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

Prof. Dr.-Ing. Peter Fromm


c
t
i
Impl_C urrent_C onsumption Impl_C urrent_C onsumption_AUDIo
n

Impl_Average_Speed

Prof. Dr.-Ing. Michael Lipp Display _A dapter

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

Display_Tripdistance Display_Tripdistance_A UDI


console-win32
(from SYS)

"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

 Get familiar with the Use Case


diagram
 A closer look at Together
 From C structs to C++ Classes
 Learn about classes and objects
 Learn the differences between
private, protected and public
scope
 Implement some attributes and
methods

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

 Difference between functional and object oriented programming


paradigm

Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 3
Example: Coordinate Class

Task:

 Develop a Use Case Diagram for a class representing coordinates.

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

Let’s imagine the following Use Case

 You want to implement a program which represents different


geometrical (2D) shapes (variable number)
 A shape can be represented by a name, a color and a set of
coordinates
 A shape can be shifted

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

We have the requirements (at least part of them)

Next steps
 Design the library
 Generate the code framework
 Start implementing the methods
 Start implementing some testcases

How would you implement this in C?

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

// Struct containing a // Class containing a coordinate


coordinate class CCoordinate
struct coordinate {
{ public:
int x; // x-Coordiante int m_x; // x-Coordiante
int y; // y-Coordinate int m_y; // y-Coordinate
} }

// 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

 The C++ programming language allows programmers to define


program-specific datatypes through the use of structures and
classes.
 Instances of these datatypes are known as objects and can contain
member variables, constants, member functions, and overloaded
operators defined by the programmer.
 Syntactically, structures and classes are extensions of the C struct
datatype, which however cannot contain functions or overloaded
operators.

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

#ifndef CCLASS_H #include “CClass.h”


#define CCLASS_H
void CClass::set(int x, int y)
class CClass {
{ m_x = x;
private: m_y = y;
int m_x; }
int m_y;
public:
set(int x, int y);
};

#endif

Header file Source file


CClass.h CClass.cpp
Class Definition (often: declaration) Method Definition (a.k.a.
Method Declaration Implementation)
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 11
Header and Source File Format

#ifndef CCLASS_H #include “CClass.h”


#define CCLASS_H
CClass::set(int x, int y)
class CClass {
{ m_x = x;
private: m_y = y;
int m_x; }
int m_y;
public:
set(int x, int y);
};

#endif

Header file Source file


CClass.h CClass.cpp
Class Definition (often: declaration) Method Definition (a.k.a.
Method Declaration Implementation)
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 12
Header and Source files

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

It is good programming practice however to


 separate declaration and implementation in 2 different files
 have only one class per file
 to give the class and the file the same name

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

 Which attributes do we need?


 Which operations do we need?

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();
};

UML representation, independent from programming language.


Order of type and variable name different, details missing
Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 16
Using Together – Summary

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

Adding a diagram or class


 Right click on the package where you want to add the diagram and
select New Diagram or New Class
 Note: Diagrams should be stored in the Model folder, C++ classes in
the project root (= myCode)
 Select the required diagram element from the diagram elements tool
bar and drag/drop them to the diagram
 Classes should be added as Shortcuts only

Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 17
Using Together – Summary

Adding Attributes and Methods to a class


 Right Click on the class and select new attribute, operation, ….
 Add a description to the added element
 Alternative: Add the attribute or operation directly in the code (H-file)

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)

 Provide self explaining names for attributes and operations


 Provide comments
 Use indentation
 Use brackets

 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

Class: Type Information, Container


Object: Instantiation of a class, Variable

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

Conventions for file inclusion


 System libraries provided by the compiler are included using <>
 Project libraries are included using “”
 CPP libs are included without .h
 C libs are included with .h
 For CPP libraries, the corresponding namespace must be activated

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

 Declare the class in the header file


 Include the required system and project headers in all files

 Add all methods as empty bodies to the cpp file


 Provide a constant return value where needed
 Implement the constructor in the cpp file

 Add a first testcase to the main.cpp


 Run and test the program

 Add method by method


 Test method by method

Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 26
Non-Functional Requirements

In addition to the functional requirements which can be described using


e.g. Use Case diagrams, non-functional requirement have to be
considered.

Non functional requirements very often describe quality constraints,


e.g.
 The memory consumption must be low
 The performance must be fast
 The code must be portable
 The functionality must be maintainable

Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 27
Non-Functional Requirements

These non-functional requirements must be specified in more detail to


become testable
 The memory of the class xyz must be below 128Byte ROM and
16Bytes RAM
 The runtime of the method keypress() must be below 10ms under all
conditions

Important: Non functional requirements very often have a huge impact


on the design!

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

Let’s consider the following non-functional requirements for our


CCoordinate class

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

Design a class student and implement the following attributes


 Name
 Address
 Matr. Number
 Current Semester
 Planned Semesters
Provide the following operations
 Provide setter and getter functions for all attributes
 Provide a function to enter one complete record by keyboard
 Print the data (individually, all together)
 Calculate the number of semesters the study will take (planned –
current semesters)
Write a main program which instantiates two student objects.

Advanced Programming Techniques and Engineering Processes, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 14/10/15 31

You might also like