C-Programmierung

Interface Beispiel

Interfaces | | Templates

class IPrinter
{
   public:

   IPrinter() {}
   virtual ~IPrinter();

   virtual void print(int day, int month, int year) = 0; // pure virtual
};

class USPrinter : public IPrinter
{
   public:

   USPrinter() {}
   ~USPrinter() {}

   virtual void print(int day, int month, int year)
   {
      cout << "Datum=" << month << "/" << day << "/" << year;
   }
};

class GermanPrinter : public IPrinter
{
   public:

   GermanPrinter() {}
   ~GermanPrinter() {}

   virtual void print(int day, int month, int year)
   {
      cout << "Datum=" << day << "." << month << "." << year;
   }
};

class Date
{
   public:

   Date(int d,int m,int y) {day=d; month=m; year=y;}

   void print(IPrinter *p) {p->print(day,month,year);}

   private:

   int day,month,year;
};

GermanPrinter gerp;
USPrinter usp;

Date d(2,7,1971);
d.print(&gerp); // output: 2.7.1971
d.print(&usp); // output: 7/2/1971


Interfaces | | Templates

Options: