{"id":843,"date":"2015-11-03T09:34:39","date_gmt":"2015-11-03T09:34:39","guid":{"rendered":"http:\/\/coolt.ch\/notizen\/?p=843"},"modified":"2015-11-03T11:25:42","modified_gmt":"2015-11-03T11:25:42","slug":"constructor-c","status":"publish","type":"post","link":"https:\/\/coolt.ch\/notizen\/constructor-c\/","title":{"rendered":"Constructor C++"},"content":{"rendered":"<p>Eine <a href=\"http:\/\/coolt.ch\/notizen\/class-public-and-private\/\">Member-Funktion<\/a> mit <em>demselben Name<\/em> wie die Klasse selbst, ist ein <em>Constructor<\/em>.<\/p>\n<p>1. <strong>Konstruktor ohne Funktionen&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/strong><\/p>\n<pre class=\"lang:sh decode:true\">class MidiData {\r\n. public: \r\n.     int pin;\r\n.     double version;\r\n.     int wait;        \/\/ in ms\r\n.     <strong>MidiData<\/strong>(int pin, double version, int wait);  \/\/<strong>Construct<\/strong>\r\n.\r\n}<\/pre>\n<p>Ein <strong>Objekt<\/strong> der Klasse kann nicht ohne Wertzuweisung definiert werden, eine <strong>Initialisierung ist zwingend notwendig<\/strong> beim Bilden eines Objektes. Die Initialiserung kann \u00fcber eine List ober \u00fcber den Constructor erfolgen:<\/p>\n<pre class=\"lang:sh decode:true\">MidireadData system_1 = { 13,  14.04, 10}  \/\/ list\r\n\r\n<strong>MidireadData system_2 { 8, 14.04, 5 }<\/strong>  \/\/ <strong>constructor<\/strong> usually\r\n<strong>MidireadData system_2( 8, 14.04, 5 )<\/strong>   \/\/ <strong>constructor<\/strong> <strong>old style<\/strong>\r\n\r\nMidireadData system_3 = MidireadData{ 10, 14.04, 10 } \/\/special<\/pre>\n<p>In der Initialisierung<strong> m\u00fcssen alle Member-Variablen definiert werden<\/strong>.<br \/>\nVgl. Bjarne Kapitel 9.4.2<\/p>\n<p><strong>2. Konstruktor f\u00fcr Klasse mit Funktionen &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/strong>&#8211;<\/p>\n<pre class=\"lang:sh decode:true\">class Date{\r\n.      public:\r\n.         \/\/ constructor\r\n.         Date( int year, int month, int day);  \r\n.         \/\/ member-functions\r\n.         void add_day( int n);\r\n.         int month();       \/\/ check error of number        \r\n.\r\n.      private:\r\n.         int year, month, day;\r\n\r\n};<\/pre>\n<p><strong>Konstruktor mit Funktion:<\/strong><\/p>\n<pre class=\"lang:sh decode:true \">Date::Date(int y_1,int m_1,int d_1)      \r\n     : month( month1) {}<\/pre>\n<p>&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eine Member-Funktion mit demselben Name wie die Klasse selbst, ist ein Constructor. 1. Konstruktor ohne Funktionen&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; class MidiData { . public: . int pin; . double version; . int wait; \/\/ in ms . MidiData(int pin, double version, int wait); \/\/Construct . } Ein Objekt der Klasse kann nicht ohne Wertzuweisung definiert werden, eine Initialisierung &hellip; <a href=\"https:\/\/coolt.ch\/notizen\/constructor-c\/\" class=\"more-link\"><span class=\"screen-reader-text\">Constructor C++<\/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":[72],"_links":{"self":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/843"}],"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=843"}],"version-history":[{"count":7,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/843\/revisions"}],"predecessor-version":[{"id":864,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/posts\/843\/revisions\/864"}],"wp:attachment":[{"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/media?parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/categories?post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coolt.ch\/notizen\/wp-json\/wp\/v2\/tags?post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}