Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Konstuktor (mit bzw. ohne PArameter)

Empfohlene Antworten

Veröffentlicht

Servus,

geht es ein Objekt wahlweise mit und ohne Parameter zu erzeugen ?

Denn wenn ich im untenstehenden Code beim erzeugen vom Objekt die

PArameter für den Konsturktor weg lasse bringt er mir ein Fehler.

Auch wenn ich versuche den Konstruktor zu überladen indem ich

zusätzlich einen Standradkonstruktor ohne PArameter mache, will er das nicht.

Bitte um nen kleinen Tipp.

Codebeispiel:

#include <iostream.h> // für cout

#include <stdio.h>

class Cat // Beginn der Klassendeklaration

{

public: // Beginn des öffentlichen Abschnitts

Cat(int initialAge); // Konstruktor

~Cat(); // Destruktor

int GetAge(); // Zugriffsfunktion

void SetAge(int age); // Zugriffsfunktion

void Meow();

private: // Beginn des privaten Abschnitts

int itsAge; // Elementvariable

};

// Konstruktor von Cat.

Cat::Cat(int initialAge)

{

itsAge = initialAge;

}

Cat::~Cat() // Destruktor, führt keine Aktionen aus

{

}

// Die öffentliche Zugriffsfunktion GetAge gibt

// den Wert des Datenelements itsAge zurück.

int Cat::GetAge()

{

return itsAge;

}

// Definition von SetAge als öffentliche

// Zugriffsfunktion

void Cat::SetAge(int age)

{

// Elementvariable itsAge auf den als

// Parameter age übergebenen Wert setzen.

itsAge = age;

}

// Definition der Methode Meow

// Rückgabe: void

// Parameter: Keine

// Aktion: Gibt "Miau" auf dem Bildschirm aus

void Cat::Meow()

{

cout << "Miau.\n";

}

// Eine Katze erzeugen, Alter festlegen, Miauen lassen

// Alter mitteilen lassen, dann erneut miauen.

int main()

{

Cat Frisky(1000);

Frisky.Meow();

cout << "Frisky ist eine Katze, die " ;

cout << Frisky.GetAge() << " Jahre alt ist.\n";

Frisky.Meow();

Frisky.SetAge(7);

cout << "Jetzt ist Frisky " ;

cout << Frisky.GetAge() << " Jahre alt.\n";

getchar();

return 0;

}

Gruß

alligator

  • Autor

Hi,

nee des isch aus dem Buch:

http://www.amazon.de/exec/obidos/ASIN/3827256631/qid=1043609538/sr=2-1/ref=sr_aps_prod_1_1/302-1071378-4696815

Aber irgendwie isch mir aufgefallen, dass des fast identische ist mit

c++ in 21 Tagen

( http://www.informit.de/books/c++21/data/kap05.htm#821490 )

Des war kein Fehler sondern nur nicht erklärt wie man beides machen kann und

da hab ichs halt ausprobiert und hab halt die Deklaration vergessen.

Bei meinen mageren C++-Kenntnissen kann das mal passieren :o)

cya

alligator

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.