46
C++ Coding Style STL - Standard Template Library Martin L¨ umkemann 24. November 2003 WS 2003/04 Martin L¨ umkemann

C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

C++ Coding StyleSTL - Standard Template Library

Martin Lumkemann

24. November 2003

WS 2003/04 Martin Lumkemann

Page 2: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

C++ Coding Style - Ubersicht

1. Motivation

2. Aufbau eines C++ Programms

3. Ungarische Notation

4. Benennung von Klassen und Methoden

5. Methoden-Aufrufe

6. Assertions

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 3: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

7. Makros

8. Code Formatierung und Kommentare

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 4: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Motivation

Warum Coding Styles?

• in C/C++ zuviel ,,erlaubt” (Fehleranfalligkeit)

• robusteren Code schreiben

• kurzere Einarbeitungszeit in fremden Code

• eigener Code muss auch nach langerer Zeit noch nachvollziehbar sein

• leichtere Fehlersuche (Debugging)

• Statistische Auswertung anhand der Programmstruktur

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 5: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Motivation

Aufbau eines C++ Programms

Dateien:

Java : Classname.java

C : name.hname.c

C++ : classname.h (auch *.{hh,hpp,h++})classname.cpp (auch *.{cc,c++})

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 6: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Beispiel: Java und C++

Java

import java.lang.System;public class XY {

public void main(String argv[]) {

System.out.println(‘‘Hello World!’’);

} // main} // class XY

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 7: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Beispiel: Java und C++

C/C++

#include <stdio.h>#include <iostream>using namespace std;

int main(int argc, char *argv[]){

printf(‘‘Hello World!\n’’); // C-Stylecout << ‘‘Hello World!’’ << endl; // C++ Style<

return 0;} // main

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 8: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Grundlegende Unterschiede zwischen Java und C++

Java...

• hat 48 Keywords, davon 2 ungenutzt

• ist sicher (keine Pointerarithmetik)

• ist strikt objektorientiert

• lasst nur einfache Vererbung zu

• hat ein umfassendes Framework

• ist langsam

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 9: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Grundlegende Unterschiede zwischen Java und C++

C++...

• hat 78 Keywords

• lasst Pointer-Arithmetik zu (Gegensatz zur Objektorientierung)

• ermoglicht Mehrfachvererbung

• ermoglicht Uberladen von Operatoren

• hat Low-Level Framework − > STL

• ist schnell - aber unsicher(?)

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 10: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Vererbung in C++

in Java:

public class A {}public class B extends A {}

in C++:

class A {};class B : public A {};

class C {};class AC : public A, protected C {};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 11: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Schnittstellen-Vererbung in C++

• keine Keywords interface oder abstract wie in Java

• Realisierung durch Klassen mit (rein) virtuellen Methoden

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 12: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Schnittstellen-Vererbung in C++

class IFoo {public:

virtual int foo() = 0; // keine Implementation// in dieser Klasse

};

class Foo : public IFoo {public:virtual int foo() { return 1; }

}; // class Foo

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 13: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Typecasts

In C++ gibt es so genannte Typecast-Operatoren:

1. static cast<TypeB>(TypeA) (sicher)

2. dynamic cast<TypeB>(TypeA) (sicher)

3. const cast<TypeB>(TypeA) (gefahrlich)

4. reinterpret cast<TypeB>(TypeA) (gefahrlich, wie C-cast)

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 14: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau eines C++ Programms

Typecasts

Typecast in C:

const long l = 1024;char *c = (char*)(&l); // erlaubt!c[0]=1; // l==1025 (little endian)

Typecast in C++:

const long l = 1024;char *c = const_cast<char*>(

static_cast<const char*>(&l) ));c[0]=1; // l==1025 (little endian)

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 15: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die Ungarische Notation

Variablennamen - Die Ungarische Notation

• eingefuhrt von Charles Simonyi (bis 2002 bei MS)

• Variablen bekommen typabhangige Prafixe

MyClass * pMyClass = new MyClass;unsigned int uiZahl=100;const char * pszName=’’Meier’’;

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 16: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die Ungarische Notation

Prafix Typb boolc chars shorti intl longll long longf floatd doubleu unsignedp Pointera Arrayo Objekt (class/struct)e enumx STL-Container

m Object-Members Class-Member (static)

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 17: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Benennung von Klassen und Methoden

Benennung von Klassen und Methoden

Gemaß Java Code Convention => Vorlesung A & D II

class MyClass {public:long getInternalCounter() const;

};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 18: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Interfaces und Abstrakte Klassen

* Interfaces Bekommen ein I vorangestelltAbstrakte Klassen bekommen ein A

class IEventHandler {...};class AMouseAdapter {...};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 19: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

struct und enum

C++ Structs sind Klassen, bei denen die Member default-maßig publicsind.=> Also Bezeichnung wie bei Klassen.

struct RefCounter {m_lCounter;m_pData;

};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 20: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

struct und enum

Da enum meist dazu eingesetzt wird um bequem Konstanten zu definie-ren, werden sie groß geschrieben.

enum CLASS_TYPE {CT_NONE = 0,CT_SYSTEM = 1,CT_NET = 2,CT_COMPR = 4};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 21: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Methodenaufrufe

Methoden-Modifier

virtual void foo () const throw() = 0;

Umgehung der const-Deklaration einer Methode:Member-Variable wird als mutable delariert.

Beispiel:

class PseudoConst {private:

mutable long m_nCounter;public:

void inc() const { m_nCounter++; }};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 22: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Methodenaufrufe

Parameter-Arten und -Modifier

call-by-reference

void foo (const MyClass & in_oInstanz);

call-by-value

void foo (const char in_cCounter);

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 23: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Methodenaufrufe

Parameter-Arten und -Modifier

call-by-reference

void foo (MyClass * out_pMyClass);void foo (MyClass & out_oMyClass);

void foo (MyClass * inout_pMyClass); // seltenvoid foo (MyClass * inref_pMyClass);

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 24: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Operatoren

Es konnen alle Operatoren uberschrieben werden:<,>,<=, >=,==, ! =, , ... Beispiel:

class StringArray {string operator [] (const size_t in_nIndex);

};

Anwendung:

extern StringArray aNames;cout << aNames[3] << endl;

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 25: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Assertions

assert(Bedingung)Hilfreich beim

• Programmieren

• Testen

• Fehlersuchen

• Code-Verstehen

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 26: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Assertions gibt es in den Varianten

• Precondition

• Invariant

• Poscondition

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 27: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

• Makros immer groß schreiben

• Verarbeitet vom Praprozessor

• Verwendung von Makros is ,,Copy-and-Paste”

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 28: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

Inline-Template

Einem Makro Vorzuziehen − > Typuberprufung durch Compiler.Statt:

#define MAXIMUM(a,b) ((a) > (b) ? (a) : (b))

Immer:

template<class T> inlineconst T& maximum(const T& a,const T& b)

{return ((a) > (b) ? (a) : (b));

};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 29: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

Guards

Einem Makro Vorzuziehen− > Typuberprufung durch Compiler.In Header-Dateien:

#ifndef __MY_CLASS_H__#define __MY_CLASS_H__ 1MyClass{:: (class-definition):}; // MyClass#ifndef // __MY_CLASS_H__

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 30: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

Code Formatierung und Kommentare

Paradigmen beim Code-Schreiben:

• Code auflockern, wenn Komplexitat hoch ist

• Verhaltnis Code : Kommentar = 40:60 bis 60:40

• Kommentare nicht mit Code mischen

• Ausnahme: nach Blocken

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 31: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

Bsp: for-loop

for (int i; i=0; i++) {// Kommentar zum CodedoSomething(i);

} // for

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 32: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

Code Formatierung

Bsp: Methode schlecht:

short complexMethod(const MyClass& in_object){

return doSomething(in_object.getReaderRef().getIterator().iterate(m_lIndex));

} // complexMethod

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 33: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

Code Formatierung

besser:

short complexMethod(const MyClass& in_object){

Reader &rReader = in_object.getReaderRef();

ReaderIterator iter = rReader.getIterator();

long lNewIndex iter.iterate(m_lIndex);

short sRetVal = doSomething(lNewIndex);return sRetVal;

} // complexMethod

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 34: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Makros

Ende Teil 1

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 35: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

STLDie C++ Standard Template Library

Ubersicht

1. Templates in C++

2. die wichtigsten Klassen der STL

3. Iteratoren

4. Anwendung der STL

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 36: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Templates

• Templates sind wiederverwendbarer Code, da sie vom Typ abstra-hieren.

• In Template-Klassen kann man Typen einsetzen, wodurch neueKlassen generiert werden.

• Templates sind wortlichen Sinne Schablonen.

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 37: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Aufbau Template-Klassen

Beispiel - ,,AutoPointer”

template<class Type> class AutoPointer{private:

Type* m_pType;public:

AutoPointer(Type* inref_pType): m_pType(inref_pType) {}

˜AutoPointer() { delete m_pType; }};

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 38: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Anwendung von AutoPointer:

typedef AutoPointer<Bus> BusAPtr;typedef AutoPointer<Fahrrad> FahrradAPtr;

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 39: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die C++ Standard Template Library

STL-Klassen

1. stack<T>

2. queue<T>

3. list<T>

4. vector<T> (− >Index-Zugriff)

5. set<T>

6. map<Tkey,Tvalue> (− > Dictionary)

7. string = basic string<char>

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 40: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die C++ Standard Template Library

Beispiel: vector<T>

typedef vector<int> IntegerVector;

IntegerVector vec;vec.push_back(10);vec.push_back(20);vec.push_back(30);

cout << ‘‘Letzter Wert : ‘‘ << vec[vec.size()-1] << endl;

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 41: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die C++ Standard Template Library

Iteratoren

Iteratoren dienen dazu, auf Elemente in einem Standard-Container derSTL zuzugreifen. Dabei liegt der Schwerpunkt auf der Effizenz, mit der diesgeschieht.

Man unterscheidet verschiedene Iterator-Typen:

• Vorwarts-Iterator

• Bidirektionaler Iterator

• RandomAccess Iterator

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 42: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die C++ Standard Template Library

• Input-/Output-Iterator

• funktionale Iteratoren

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 43: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die C++ Standard Template Library

Bei den Vorwarst-Iteratoren unterscheidet man noch weiter in

iterator

const iterator

Code-Beispiel:

vector<int> vec;vector<int>::iterator iter = vec.begin();

// den vector um den faktor 2 Streckenfor(; iter!=vec.end(); ++iter) {

(*iter) *= 2;}

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 44: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Die C++ Standard Template Library

UML

Eine Aggregation von Klasse Child an Klasse Parent:

class Parent {private:list<Child> m_children;

...};

Die Modellierung von Klassen-Strukturen mit der STL bietet typechtheit.

WS 2003/04 Martin Lumkemann C++ Coding Style

Page 45: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

Referenzen

Literatur

[1] Bjarne Stroustrup. Die C++ Programmiersprache. Addison-Wesley-Verlag, 2000.

[2] Rolf Isernhagen. Softwaretechnik in C und C++. Carl Hanser Verlag,2001.

[3] Bernd Oestreich. Objektorientierte Softwareentwicklung - Analyse undDesign mit der Unified Modeling Language. R.Oldenbourg Verlag, 1998.http://www.oose.de/uml.

WS 2003/04 Martin Lumkemann

Page 46: C++ Coding Style STL - Standard Template Libraryrhaschke/lehre/WS03/greifen/... · C++ Coding Style STL - Standard Template Library Martin L umkemann 24. November 2003 WS 2003/04

[4] C++ User’s Guide. http://www.ictp.trieste.it/∼manuals/programming/sun/c-plusplus/c++ ug/index.html.

[5] Thomas Hermann. C++ Coding Guidelines. http://www.techfak.uni-bielefeld.de/∼thermann/projects/CPP-coding.html.

[6] Peter Thommes. Notizen zu C++. [PC-NI]file://homes/rhaschke/docs/mgoetting/c++/C++ eBook.pdf.

[7] Charles Simonyi. Hungarian Notation, Reprinted November1999. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvsgen/html/hunganotat.asp.

[8] Java2 Kompendium, Kapitel 16, Andere Sprachen in Verbindung mitJava. http://www.informit.de/books/java2 komp/data/kap16.htm.

WS 2003/04 Martin Lumkemann