====== C++ Crashkurs / Übung 6 ======
Durch Templates schreibt man Klassen ohne sie an konkrete Datentypen zu binden. Der konkrete Typ wird in der Deklaration und Definition durch Platzhalter ersetzt.
**Ueb3a.h:**
// File : Ueb3a.h
//
#include "..\stdafx.h"
#include "Ueb3a.VerketteteListe.h"
using namespace Ueb3a;
#include
int _ueb (int argc, _TCHAR* argv[])
{
// Lösung mit list kann _auch_ andere Datetypen:
std::list v;
v.push_back(4711);
v.push_back(0.815);
v.push_back(42);
std::cout << "Liste mit vector:" << std::endl;
for (auto iter1 = v.begin(); iter1 != v.end(); iter1++)
{
std::cout << " " << *iter1 << std::endl;
}
auto pListe = new CListe();
pListe->ElementAnhaengen(4711);
pListe->ElementAnhaengen(0.815);
pListe->ElementAnhaengen(42);
std::cout << "Liste mit double als Datentyp:" << std::endl;
for (auto iter2 = pListe->begin(); iter2 != pListe->end(); ++iter2)
{
std::cout << " " << *iter2 << std::endl;
}
delete pListe;
return 0;
}
// EOF
**Ueb3a.VerketteteListe.h:**
// File : Ueb3a.VerketteteListe.h
//
namespace Ueb3a
{
template
class CListe
{
private:
class CListenElement
{
friend CListe;
private:
TElement nInhalt;
CListenElement* pNaechster;
public:
CListenElement (TElement newInhalt)
{
nInhalt = newInhalt;
pNaechster = nullptr;
}
~CListenElement ()
{
delete pNaechster;
pNaechster = nullptr;
}
TElement GetInhalt ()
{
return nInhalt;
}
CListenElement* GetNaechster ()
{
return pNaechster;
}
};
class CListeIterator
{
private:
CListenElement* element;
public:
CListeIterator (CListenElement* pElem)
{
element = pElem;
}
bool operator!=(CListeIterator another)
{
return (this->element != another.element);
}
CListeIterator& operator++()
{
element = element->GetNaechster();
return *this;
}
TElement operator*()
{
return element->GetInhalt();
}
};
public:
CListenElement* pStart;
CListe ()
{
pStart = nullptr;
}
CListe (const CListe& other)
{
pStart = nullptr;
for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
}
~CListe ()
{
delete pStart;
pStart = nullptr;
}
CListe& operator=(const CListe& other)
{
if (pStart != nullptr) delete pStart;
pStart = nullptr;
for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
return *this;
}
CListeIterator begin () const
{
return CListeIterator(pStart);
}
CListeIterator end () const
{
return CListeIterator(nullptr);
}
void ElementAnhaengen (TElement nInhalt)
{
auto pNeu = new CListenElement(nInhalt);
if (pStart == nullptr)
{
pStart = pNeu;
}
else
{
auto pLetztes = pStart;
while (pLetztes->GetNaechster() != nullptr) pLetztes = pLetztes->GetNaechster();
pLetztes->pNaechster = pNeu;
} // if
}
};
}
// EOF
**Programm.cpp:** - Der Einstiegspunkt
// Programm.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Ueb3a.h"
int _tmain (int argc, _TCHAR* argv[])
{
return _ueb(argc, argv);
}
// EOF
**stdafx.h:**
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include
#include
// TODO: reference additional headers your program requires here
#include
#include
// EOF
**targetver.h:**
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#pragma once
#include
----
Stand: 19.09.2014
--- //[[feedback.jk-wiki@kreick.de|: Jürgen Kreick]]//
EOF