{"id":969,"date":"2015-11-17T19:12:41","date_gmt":"2015-11-17T19:12:41","guid":{"rendered":"http:\/\/coolt.ch\/notizen\/?p=969"},"modified":"2015-11-17T19:12:41","modified_gmt":"2015-11-17T19:12:41","slug":"c-operator-overloding","status":"publish","type":"post","link":"https:\/\/coolt.ch\/notizen\/c-operator-overloding\/","title":{"rendered":"c++ Operator overloding"},"content":{"rendered":"<p><strong>Kapitel 9:<\/strong> <strong>Operatoren nicht nur f\u00fcr Integers<\/strong><br \/>\n(\u00dcbung Buchhandlung mit Christian: == f\u00fcr den Vergleich der Klasse Buch)<\/p>\n<pre class=\"lang:sh decode:true\">class <strong>Book<\/strong> {\r\nprivate:\r\n    string <strong>ISBN<\/strong>;\r\n    string title;\r\n    string author;\r\npublic: \r\n    Book(string isbn, string t, string a){ \r\n        ISBN = isbn; \r\n        title = t; \r\n        author = a; \r\n}<\/pre>\n<p>\/\/ buch1 == buch2<\/p>\n<pre class=\"lang:sh decode:true\">bool <strong>operator==<\/strong>(const <strong>Book&amp;<\/strong> b2)       \/\/ new Datatyp is Book\r\n  {\r\n    if(ISBN == b2.ISBN)               \/\/ this new Typ is addresd by b2\r\n      return true;\r\n    else\r\n      return false;\r\n  }<\/pre>\n<pre class=\"lang:sh decode:true\">Book buch1 = Book{978-8-123-12345-6, \"Andorra\", \"Frisch\"};\r\nBook buch3 = Book{978-8-123-12345-6, \"Mein Name ist Gantenbein\", \"Frisch\"};<\/pre>\n<pre class=\"lang:sh decode:true\">if (buch1 == buch3)\r\n    cout &lt;&lt; \"Die B\u00fccher sind gleich.\" &lt;&lt; endl;\r\n  else\r\n    cout &lt;&lt; \"Die B\u00fccher sind nicht gleich.\" &lt;&lt; endl;<\/pre>\n<p>Hier sind buch1 und buch3 gleich, weil sie die gleichen ISBN haben.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Kapitel 10: IO<\/strong><br \/>\nDer Operator &lt;&lt; ist urspr\u00fcnglich <strong>nur f\u00fcr String<\/strong>.<br \/>\nSollen bei &lt;&lt; auch andere Datentypen \u00fcbergeben werden k\u00f6nnen, braucht es ein Overloading.<\/p>\n<pre class=\"lang:sh decode:true\">ostream&amp; operator <strong>&lt;&lt;<\/strong>(ostream&amp; os, <strong>const Date&amp; d<\/strong>)<\/pre>\n<p>Ab sofort kann auch die Klasse Date dem stream \u00fcbergeben werden.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kapitel 9: Operatoren nicht nur f\u00fcr Integers (\u00dcbung Buchhandlung mit Christian: == f\u00fcr 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&amp; b2) \/\/ new &hellip; <a href=\"https:\/\/coolt.ch\/notizen\/c-operator-overloding\/\" class=\"more-link\"><span class=\"screen-reader-text\">c++ Operator overloding<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,65],"tags":[],"_links":{"self":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/969"}],"collection":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/comments?post=969"}],"version-history":[{"count":4,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":973,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/969\/revisions\/973"}],"wp:attachment":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}