code:
#include "stdafx.h"
#include <iostream>
using namespace std;
//Klasse MetallScheibe
class MetallScheibe
{
protected:
static const double PI; //Wie initialisiert man eine Klassenvariable richtig ?
double radius;
public:
MetallScheibe(double radius)
{
this->radius = radius;
}
double getRadius() const
{
return radius;
}
virtual double flaeche() //was macht das virtual ?
{
return PI * radius *radius;
}
bool isSchwererAls(MetallScheibe* ms)
{
return flaeche() > ms->flaeche();
}
};
const double MetallScheibe::PI = 3.14152;
//Klasse Sieb
class Sieb : public MetallScheibe
{
private:
int anzahlLoecher;
int maxAnzahlLoecher;
double lochRadius;
MetallScheibe** loch;
public:
//Einen Konstruktor
Sieb(double radius, int maxAnzahlLoecher) : MetallScheibe(radius)
{
this->maxAnzahlLoecher = maxAnzahlLoecher;
loch = new MetallScheibe* [maxAnzahlLoecher];
this->lochRadius = radius/maxAnzahlLoecher;
this->anzahlLoecher = 0;
}
void neuesLochStanzen() throw (out_of_range) //diese methode wirft eine out_of_range exception
{
if(anzahlLoecher + 1 > maxAnzahlLoecher)
throw out_of_range("max Anzahl Loecher erreicht");
else
{
loch[anzahlLoecher] = new MetallScheibe(lochRadius);
anzahlLoecher++;
}
}
double flaeche()
{
return MetallScheibe::flaeche() - (anzahlLoecher * (PI * lochRadius * lochRadius));
}
void deleteLoecher()
{
//Vorerst müssen alle einzelen dynamisch angelegten Pointer-MetallScheiben-Objekte aus loch entfernt werden
for (int i = 0; i < anzahlLoecher; i++)
{
delete [] loch[i];
}
//Schlussendlich müssen wir noch den "pointer auf ein Feld von pointern" selbst köschen
delete[] loch;
}
//Einen Destruktor, der den dynamisch angelegten Speicherplatz freigibt
~Sieb()
{
deleteLoecher();
}
//Einen Kopierkonstruktor...Hier werden die Attribute dieses Objektes von einem anderen sieb überschrieben
Sieb (const Sieb& sieb) : MetallScheibe(sieb.radius) // auch beim Kopierkonstruktor muss der Konstruktor der Basisklasse aufgerufen werden
{
if (this != &sieb)
{
anzahlLoecher = sieb.anzahlLoecher;
maxAnzahlLoecher = sieb.maxAnzahlLoecher;
lochRadius = sieb.radius/maxAnzahlLoecher;
loch = new MetallScheibe*[maxAnzahlLoecher];
//auch alle Loecher des Siebes müssen kopiert werden !
for (int i = 0; i < sieb.anzahlLoecher; i++)
{
loch[i] = new MetallScheibe(sieb.lochRadius);
}
}
}
//Einen überladenen Zuweisungsoperator
Sieb& operator= (const Sieb& sieb)
{
if(this != &sieb)
{
anzahlLoecher = sieb.anzahlLoecher;
maxAnzahlLoecher = sieb.maxAnzahlLoecher;
lochRadius = sieb.radius/maxAnzahlLoecher;
//im Gegensatz zum Kopierkonstruktor muss hier die
//dynamisch erzeutgen MetallScheiben gelöscht werden,
//da operator= auf bereits erzeugt und inizialisierte
//objekte angewendet wird
deleteLoecher(); //WHAT'S WRONG ???
loch = new MetallScheibe*[maxAnzahlLoecher];
for (int i = 0; i < sieb.anzahlLoecher; i++)
{
loch[i] = new MetallScheibe(sieb.lochRadius);
}
}
return *this;
}
};
//TODO: Globale Funktion lochen()
Sieb& lochen(MetallScheibe ms, int maxAnzahl, int anzahlLoecher) throw (out_of_range)
{
if(anzahlLoecher+1 > maxAnzahl)
{
throw out_of_range("max Anzahl Löcher erreicht");
}
else
{
Sieb* sieb = new Sieb(ms.getRadius(), maxAnzahl);
for (int i; i < anzahlLoecher; i++)
sieb->neuesLochStanzen();
return *sieb;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Sieb sieb(2.0f,3);
sieb.neuesLochStanzen();
sieb.neuesLochStanzen();
cout << sieb.flaeche() << endl;
Sieb sieb2(2.0f,4);
sieb2 = sieb;
cout << sieb2.flaeche() << endl;
char f;
cin >> f;
return 0;
}
i am having problems using the destructor's "deleteLoecher" method in code line: 126get an access violation and i guess it is because i am trying to delete memory that isn't dynamicly allocated.
In line: 54 i am allocating an array of pointers to the variable "MetallScheibe** loch".
In the class function "neuesLochStanzen" in line: 66 i "think" i am allocating memory for a new MetallScheibe() object.
is there something wrong with my allocation ?</iostream>



















