Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. //Klasse MetallScheibe
  8. class MetallScheibe
  9. {
  10.  
  11.     protected:
  12.         static const double PI; //Wie initialisiert man eine Klassenvariable richtig ?
  13.         double radius;
  14.  
  15.     public:
  16.         MetallScheibe(double radius)
  17.         {
  18.             this->radius = radius;
  19.         }
  20.  
  21.         double getRadius() const
  22.         {
  23.             return radius;
  24.         }
  25.  
  26.         virtual double flaeche() //was macht das virtual ?
  27.          {
  28.             return PI * radius *radius;
  29.         }
  30.  
  31.  
  32.         bool isSchwererAls(MetallScheibe* ms)
  33.         {
  34.             return flaeche() > ms->flaeche();
  35.         }
  36. };
  37. const double MetallScheibe::PI = 3.14152;
  38.  
  39.  
  40. //Klasse Sieb
  41. class Sieb : public MetallScheibe
  42. {
  43.     private:
  44.     int anzahlLoecher;
  45.     int maxAnzahlLoecher;
  46.     double lochRadius;
  47.     MetallScheibe** loch;
  48.  
  49. public:
  50.     //Einen Konstruktor
  51.     Sieb(double radius, int maxAnzahlLoecher)  : MetallScheibe(radius)
  52.     {
  53.         this->maxAnzahlLoecher = maxAnzahlLoecher;     
  54.         loch = new MetallScheibe* [maxAnzahlLoecher];
  55.  
  56.         this->lochRadius = radius/maxAnzahlLoecher;
  57.         this->anzahlLoecher = 0;
  58.     }
  59.  
  60.     void neuesLochStanzen() throw (out_of_range) //diese methode wirft eine out_of_range exception
  61.     {
  62.         if(anzahlLoecher + 1 > maxAnzahlLoecher)
  63.             throw out_of_range("max Anzahl Loecher erreicht");
  64.         else
  65.         {          
  66.             loch[anzahlLoecher] = new MetallScheibe(lochRadius);
  67.             anzahlLoecher++;
  68.         }
  69.     }
  70.  
  71.     double flaeche()
  72.     {
  73.         return MetallScheibe::flaeche() - (anzahlLoecher * (PI * lochRadius * lochRadius));
  74.     }
  75.  
  76.     void deleteLoecher()
  77.     {
  78.         //Vorerst müssen alle einzelen dynamisch angelegten Pointer-MetallScheiben-Objekte aus loch entfernt werden
  79.         for (int i = 0; i < anzahlLoecher; i++)
  80.         {
  81.             delete [] loch[i];
  82.         }
  83.  
  84.         //Schlussendlich müssen wir noch den "pointer auf ein Feld von pointern" selbst köschen
  85.         delete[] loch;
  86.     }
  87.  
  88.     //Einen Destruktor, der den dynamisch angelegten Speicherplatz freigibt
  89.     ~Sieb()
  90.     {
  91.         deleteLoecher();
  92.     }
  93.  
  94.     //Einen Kopierkonstruktor...Hier werden die Attribute dieses Objektes von einem anderen sieb überschrieben
  95.     Sieb (const Sieb& sieb) : MetallScheibe(sieb.radius)  // auch beim Kopierkonstruktor muss der Konstruktor der Basisklasse aufgerufen werden
  96.     {
  97.         if (this != &sieb)
  98.         {
  99.             anzahlLoecher = sieb.anzahlLoecher;
  100.             maxAnzahlLoecher = sieb.maxAnzahlLoecher;
  101.             lochRadius = sieb.radius/maxAnzahlLoecher;
  102.  
  103.             loch = new MetallScheibe*[maxAnzahlLoecher];
  104.             //auch alle Loecher des Siebes müssen kopiert werden !
  105.             for (int i = 0; i < sieb.anzahlLoecher; i++)
  106.             {
  107.                 loch[i] = new MetallScheibe(sieb.lochRadius);
  108.             }
  109.         }
  110.     }
  111.  
  112.     //Einen überladenen Zuweisungsoperator
  113.     Sieb& operator= (const Sieb& sieb)
  114.     {
  115.         if(this != &sieb)
  116.         {
  117.             anzahlLoecher = sieb.anzahlLoecher;
  118.             maxAnzahlLoecher = sieb.maxAnzahlLoecher;
  119.             lochRadius = sieb.radius/maxAnzahlLoecher;
  120.  
  121.             //im Gegensatz zum Kopierkonstruktor muss hier die
  122.             //dynamisch erzeutgen MetallScheiben gelöscht werden,
  123.             //da operator= auf bereits erzeugt und inizialisierte
  124.             //objekte angewendet wird
  125.            
  126.             deleteLoecher(); //WHAT'S WRONG ???
  127.  
  128.             loch = new MetallScheibe*[maxAnzahlLoecher];
  129.             for (int i = 0; i < sieb.anzahlLoecher; i++)
  130.             {
  131.                 loch[i] = new MetallScheibe(sieb.lochRadius);
  132.             }
  133.         }
  134.  
  135.         return *this;
  136.     }
  137. };
  138.  
  139.  
  140. //TODO: Globale Funktion lochen()
  141. Sieb& lochen(MetallScheibe ms, int maxAnzahl, int anzahlLoecher) throw (out_of_range)
  142. {
  143.    
  144.     if(anzahlLoecher+1 > maxAnzahl)
  145.     {
  146.         throw out_of_range("max Anzahl Löcher erreicht");
  147.     }
  148.     else
  149.     {  
  150.         Sieb* sieb = new Sieb(ms.getRadius(), maxAnzahl);
  151.         for (int i; i < anzahlLoecher; i++)
  152.             sieb->neuesLochStanzen();
  153.  
  154.         return *sieb;
  155.     }
  156.    
  157. }
  158.  
  159.  
  160. int _tmain(int argc, _TCHAR* argv[])
  161. {
  162.  
  163.     Sieb sieb(2.0f,3);
  164.     sieb.neuesLochStanzen();
  165.     sieb.neuesLochStanzen();
  166.    
  167.     cout << sieb.flaeche() << endl;
  168.  
  169.     Sieb sieb2(2.0f,4);
  170.     sieb2 = sieb;
  171.  
  172.     cout << sieb2.flaeche() << endl;
  173.  
  174.    
  175.  
  176.  
  177.  
  178.     char f;
  179.     cin >> f;
  180.     return 0;
  181. }
Add Comment
Please, Sign In to add comment