c++ Operator overloding

Kapitel 9: Operatoren nicht nur für Integers
(Übung Buchhandlung mit Christian: == für den Vergleich der Klasse Buch)

class Book {
private:
    string ISBN;
    string title;
    string author;
public: 
    Book(string isbn, string t, string a){ 
        ISBN = isbn; 
        title = t; 
        author = a; 
}

// buch1 == buch2

bool operator==(const Book& b2)       // new Datatyp is Book
  {
    if(ISBN == b2.ISBN)               // this new Typ is addresd by b2
      return true;
    else
      return false;
  }
Book buch1 = Book{978-8-123-12345-6, "Andorra", "Frisch"};
Book buch3 = Book{978-8-123-12345-6, "Mein Name ist Gantenbein", "Frisch"};
if (buch1 == buch3)
    cout << "Die Bücher sind gleich." << endl;
  else
    cout << "Die Bücher sind nicht gleich." << endl;

Hier sind buch1 und buch3 gleich, weil sie die gleichen ISBN haben.

 

Kapitel 10: IO
Der Operator << ist ursprünglich nur für String.
Sollen bei << auch andere Datentypen übergeben werden können, braucht es ein Overloading.

ostream& operator <<(ostream& os, const Date& d)

Ab sofort kann auch die Klasse Date dem stream übergeben werden.

 

 

 

 

..