Introduction to C – Classes: Heredity, Adjustment, Friends, abstract – Part5


Heredity

Classes can inherit functions and member variables of other classes.

Class A{
public:
 void f();
};
Class B : public A{};//B erbt public von A
B* b = new B;
b.f(); //Funktion ist vorhanden, da geerbt
  • public: can be accessed from anywhere on these elements
  • protected: Access to member only of parent classes and derived classes (and the class, that defines the item)
  • private: only the defining class

Inheritance rules

Heredity: public

Class B : public A{};

Basisklasse/abgeleitete Klasse
private/nicht sichtbar
protected/protected
public/public

Heredity: protected

Class B : protected A{};

Basisklasse/abgeleitete Klasse
private/nicht sichtbar
protected/protected
public/protected

Heredity: private

Class B : private A{};

Basisklasse/abgeleitete Klasse
private/nicht sichtbar
protected/private
public/private
  • derived class is also a Base Class
Class A{};
Class B : public A{};
A* a = new B; //ok
B* b = new A; //Fehler, Umkehrung gilt nicht

Adjustment

  • restore the original rights (the base class) in the derived class
  • only for private / protected inheritance logically
  • only restoring, possible no other / better rights
Class A{
 public:
 int a1;
};
Class B : private A{
 public:
 A::a1; //Adjustment nach public von private
};

B* b = new B;
int a = b.a1; //ok, da public

Friends

  • Access to private / protected variable possible from outside the class
  • are not inherited

Friend Functions

Class A{
 public:
 int a; //private
friend void writeA(A& einA);
};

void writeA(A& einA){
 einA.a = 2; //Zugriff von außerhalb der Klasse auf privates Element
}

Friend classes

Class A{
 A(){}; //Konstruktor private gesetzt
 public:
 friend class Creator;
};

Class Creator{
  A* newA; //Konstruktor private gesetzt
 public:
  Creator(){
   newA = new A;
  }; //Konstruktor mit Zugriff auf private Konstruktor von A
};

A* a = new A; //Fehler, Konstruktor ist private
Creator* or = new Creator; // ok, A Objekt in Creator angelegt

Virtuality and abstract classes

  • abstract classes and methods as possible in Java
  • are in base class “Interfaces” described, must be implemented in derived class
  • is possible by late binding: per class is a virtual method table(VMT) created
Class A{
 public:
 virtual void print(){cout "ein A"};
};
Class B : public A{
 public:
 void print(){cout "ein B"};
};

B* pb = new B;
A* a = b; //ok, ist ein Vererbung, siehe oben
pa->print(); // "ein B", ohne virtual wäre es "ein A"
pb->print(); // "ein B"

But:

A* pa2 = new A;
pa2->print(); //"ein A", ist bei abstrakten Klassen nicht erwünscht

Therefore:

Class A{
 public:
 virtual void print() = 0; //"pure virtual function", richtige abstrakte Funktion
};

The Class A is thus a so-called “abstract base class”, because they have a “pure virtual function” contains, the need to implement the child classes.

Contents:

Introduction to C - Advantages, Compiler, Namespaces - Teil1

Introduction to C - Pointer, Pointer,References - Part2

Introduction to C - Data Types, Casting, Classes - Part 3

Introduction to C - Functions and Operator Overloading (Overloading) - Part4

Introduction to C - Classes: Heredity, Adjustment, Friends, abstract - Part5

  1. Hello, an error in your example of the abstract classes appears to have smuggled,should there not
    B * b = new B;
    A* a = b ;
    stand?
    LG