C-Programmierung

Beispiel Zur Vererbung

Exceptions | | Bibliotheken

Gegeben eine Klasse “Fridge”, welche einen Kühlschrank mit einem gegebenen maximalen Fassungsvermögen in Litern repräsentieren soll:

class Fridge
{
   public:

   // ctor
   Fridge(double liter=100)
   {
      liter_ = 0;
      max_liter_ = liter;
   }

   // dtor
   virtual ~Fridge() {}

   double get_content() // getter
      {return(liter_);}

   void add_content(double liter=1.0) // overloaded setter
   {
      if (liter_+liter>max_liter_)
      {
         myexception e;
         throw e;
      }
      else
         liter_ += liter;
   }

   void retrieve_content(double liter=1.0) // overloaded setter
   {
      if (liter_-liter<0)
      {
         myexception e;
         throw e;
      }
      else
         liter_ -= liter;
   }

   Fridge &operator += (double r)
   {
      add_content(r);
   }

   Fridge &operator -= (double r)
   {
      retrieve_content(r);
   }

   virtual double get_modelnumber()
      {return(1);}

   protected:

   double liter_;
   double max_liter_;
};

Abgeleitetes Exception Objekt:

class myexception: public std::exception // inheritance from std exception object
{
   virtual const char* what() const throw()
   {
      return("fridge capacity disrespected");
   }
};

ostream& operator << (ostream &out, myexception &e) // overloaded ostream operator
{
   out << e.what();
   return(out);
}

Ãœberladener Ausgabe-Operator <<:

ostream& operator << (ostream &out, Fridge &f) // overloaded ostream operator
{
   out << "fridge with model number " << f.get_modelnumber() << " contains " << f.get_content() << " liter";
   return(out);
}

Test der Klasse:

void main()
{
   try
   {
      Fridge f(1);
      f.add_content(1);
      std::cout << f << std::endl; // -> "fridge with model number 1 contains 1 liter"

      f+=0.5;
      std::cout << f << std::endl; // -> "fridge with model number 1 contains 1.5 liter"

      f.retrieve_content();
      f.retrieve_content(); // -> exception
   }
   catch (myexception &e)
   {
      std::cout << "error occured: " << e.what() << std::endl;
   }
   catch (std::exception &e)
   {
      std::cout << "error occured: " << e << std::endl;
   }
}

Vererbung an eine Klasse “FridgeWithFreezer”, welche eine Spezialisierung der Basisklasse darstellt, so dass der Kühlschrank zusätzlich ein Gefrierfach enthält:

class FridgeWithFreezer: public Fridge // inheritance from base class
{
   public:

   // ctor
   FridgeWithFreezer(double liter=100, double freezer=20)
      : Fridge(liter) // base class ctor
   {
      liter_freezer = 0;
      max_liter_freezer_ = freezer;
   }

   // copy ctor
   FridgeWithFreezer(const Fridge &f)
   {
      liter_ = f.liter_;
      liter_freezer_ = 0;

      if (liter_>max_liter_) throw ...
   }

   double get_content_freezer() // getter
      {return(liter_freezer_);}

   void add_content_freezer(double liter=1.0) // overloaded setter
   {
      ...
   }

   void retrieve_content_freezer(double liter=1.0) // overloaded setter
   {
      ...
   }

   virtual double get_modelnumber()
      {return(2);}

   protected:

   double liter_freezer_;
   double max_liter_freezer_;
}

Statischer Test der Klasse:

Fridge f(1000);
f.add_content(500);

FridgeWithFreezer ff(200);
ff.add_content(100);
ff.add_content_freezer(10);
ff=f; //!! exception

Dynamischer Test der Klasse:

Fridge *f = new Fridge;
Fridge *ff = new FridgeWithFreezer;

std::cout << f->get_modelnumber() << std::endl; // -> 1
std::cout << ff->get_modelnumber() << std::endl; // -> 2

delete f;
delete ff;


Exceptions | | Bibliotheken

Options: