====== C++ Crashkurs / Übung 10 ======
Exceptions.
**Ueb5a.h:**
// File : Ueb5a.h
//
#include "..\stdafx.h"
#include
#include "Ueb5a.VerketteteListe.h"
using namespace Ueb5a;
#include
void Test ()
{
CListe* pListe= new CListe();
try
{
pListe->ElementAnhaengen(4711);
pListe->ElementAnhaengen(815);
pListe->ElementAnhaengen(42);
pListe->ElementAnhaengen(0);
std::cout << "Liste iteriert mit prefix-operator:" << std::endl;
for (auto iter2 = pListe->begin(); iter2 != pListe->end(); ++iter2)
{
std::cout << " " << *iter2 << std::endl;
}
}
catch (std::string ex) // Exceptions vom Typ std::string fangen
{
std::cerr << "Exception: " << ex.c_str() << std::endl;
auto s = std::string("Ein Fehler in ");
s += ((const char*) __FUNCTION__);
s += "aufgetreten.";
throw s; // Exception werfen
//throw; // Ohne Argumente gibt einfach die urspruengliche Exception weiter
}
delete pListe;
}
int _ueb (int argc, _TCHAR* argv[])
{
try
{
Test();
}
catch (...) // Exceptions beliebiger Typs fangen
{
std::cerr << "Im Programm ist irgendein Fehler aufgetreten..." << std::endl;
}
return 0;
}
// EOF
**Ueb5a.VerketteteListe.h:**
// File : Ueb5a.VerketteteListe.h
//
namespace Ueb5a
{
template
class CListe
{
private:
class CListenElement
{
friend CListe;
private:
TElement nInhalt;
std::shared_ptr pNaechster;
public:
CListenElement (TElement newInhalt)
{
int iTmp;
nInhalt = newInhalt;
iTmp = 100 / nInhalt; // Exception provozieren
if (iTmp > 0) iTmp++;
}
~CListenElement ()
{
}
TElement GetInhalt ()
{
return nInhalt;
}
std::shared_ptr 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().get();
return *this;
}
TElement operator*()
{
return element->GetInhalt();
}
};
public:
std::shared_ptr pStart;
CListe ()
{
}
CListe (const CListe& other)
{
for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
}
CListe& operator=(const CListe& other)
{
pStart = nullptr;
for (auto iter = other.begin(); iter != other.end(); ++iter) ElementAnhaengen(*iter);
return *this;
}
CListeIterator begin () const
{
return CListeIterator(pStart.get());
}
CListeIterator end () const
{
return CListeIterator(nullptr);
}
void ElementAnhaengen (TElement nInhalt)
{
if (nInhalt == 0) throw std::string("DIV0"); // Exception werfen
auto pNeu = std::make_shared(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 "Ueb5a.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