Vererbung

Vererbung ist ein wichtiges Merkmal der objektorientierten Programmierung. Und: Vererbung ist sehr leistungsfähig. Da ist zuerst die Basisklasse. Von dieser bereits vorhandenen Basisklasse wird mindestens eine neue Klasse abgeleitet. Die durch Vererbung abgeleitete Klasse "erbt" alle Fähigkeiten der Basisklasse. Sie kann aber durch zusätzliche Fähigkeiten ergänzt werden.

Das erste Beispiel ist sehr einfach:

#include <iostream>
using namespace std;

class humanBeing
  {
    public:
    humanBeing()
    {
      cout << "Calling the human being class constructor"<<endl;
    }
    void display()
    {
      cout << "I'm a human being"<<endl;
    } 
  };//humanBeing
class Man : public humanBeing
  {
    public:
      Man()
      {
       cout << "I'm a man, a male human being"<<endl;
      }
  };//Man
 
int main()
{
      Man yann;
      yann.display();
      return 0;
}

Und das zweite Beispiel:

//zaehl.cpp
#include <iostream>

using namespace std;

class Zaehler
{
    protected:
        unsigned int zaehl;
    public:
        Zaehler() { zaehl=0; }
        Zaehler(int c) { zaehl=c; }
        int getZaehler() { return zaehl; }
        Zaehler operator ++()
        {
            zaehl++;
            return Zaehler(zaehl);
        }
};//class Zaehler
class CountDown : public Zaehler
{
    public:
        Zaehler operator -- ()
        {
            zaehl--;
            return Zaehler(zaehl);
        }
};//class CountDown

int main()
{
    CountDown c1;
    cout << "Zaehlerstand: " << c1.getZaehler();//0
    ++c1;
    ++c1;
    ++c1;
    cout << endl << "c1 ist nun: " << c1.getZaehler();//3
    --c1;
    --c1;
    cout << endl << "c1 ist am Ende: " << c1.getZaehler();//1
    return 0;
}//main()

Das nächste Programm ist eine ganz kleine Datenbank. Die Basisklasse Employee ist der Ursprung. Die anderen abgeleiteten Klassen verwenden diese Basisklasse.

//employ.cpp
#include <iostream>
using namespace std;

class Employee
{
    private:
        string name;
        unsigned long number;
    public:
        void getData()
         {
           cout << endl << "Nachname: "; cin >> name;
           cout << "Nummer: "; cin >> number;
         }   
        void putData()
        {
            cout << endl << name;
            cout << endl << number;
        }
       
}; //class Employee
class Manager : public Employee
{
    private:
        string title;
        double dues;
    public:
        void getData()
        {
            Employee::getData();
            cout << "Titel: "; cin >> title;
            cout << "Golfclubeinlage: "; cin >> dues;
        }
        void putData()
        {
            Employee::putData();
            cout << endl << title;
            cout << endl << dues;
            }   
};
class Scientist : public Employee
{
    private:
        int pubs;//Zahl Publikationen
    public:
        void getData()
        {
            Employee::getData();
            cout << "Publikationen: "; cin >> pubs;
           
            }   
       void putData()
       {
              Employee::putData();
              cout << endl << pubs;
       }
};

class Laborer : public Employee {};

int main()
{
  Manager m1, m2;
  Scientist s1;
  Laborer l1;
  cout << endl;
  cout << endl << "Geben Sie fuer Manager 1 ein.";
  m1.getData();
  cout << endl << "Geben Sie fuer Manager 2 ein.";
  m2.getData();
  cout << endl << "Geben Sie fuer Wissenschaftler 1 ein.";
  s1.getData();
  cout << endl << "Geben Sie fuer Arbeiter 1 ein.";
  l1.getData();
 
  cout << endl << "Die Daten des Managers 1:";
  m1.putData();
  cout << endl << "Die Daten des Managers 2:";
  m2.putData();
  cout << endl << "Daten des Wissenschaftlers:";
  s1.putData();
  cout << endl << "Die Daten des Arbeiters:";
  l1.putData();
  return 0;
}//main()

Das Thema "Vererbung" ist hier erst einmal zuende.


Spezialitäten

Dieses kleine Programm ist um die Ecke gedacht - und hat etwas mit Aussagenlogik zu tun.

//logi.cpp
//Aussagenlogik in Zahlen verpackt:
#include <iostream>//cout
#include <conio.h>//getch
using namespace std;

int main()
{
    int a = 5;
    int b = 3;
    int c = a & b;
    int d = a | b;
    int e = a ^ b;
    cout<< hex << c << " " << d << " " << e << endl;   
    int f=0xC;
    int g=0xA;
    int i = f & g;//und
    int j = f | g;//oder
    int k = f ^ g;//exklusives Oder
    cout << hex << i << "  " << j << "  " << k;
    cout << endl << "Taste ...";
    getch();
    return 0;
}


Seiteneffekte


Komplexe Zahlen