Klassen sind der zentrale Baustein der Objektorientierung in C++. In dieser Lektion vertiefen wir die Definition, Konstruktoren, Methoden, Destruktoren und Zugriffsmodifikatoren.
class Auto {
public:
std::string marke;
int baujahr;
void anzeigen() {
std::cout << marke << ", " << baujahr << std::endl;
}
};
class Auto {
public:
std::string marke;
int baujahr;
Auto(std::string m, int j) : marke(m), baujahr(j) {} // Konstruktor
};
class Auto {
public:
~Auto() {
std::cout << "Objekt wird gelöscht" << std::endl;
}
};
public, private und protected.
class Konto {
private:
double saldo;
public:
Konto(double s) : saldo(s) {}
void einzahlen(double betrag) { saldo += betrag; }
double getSaldo() { return saldo; }
};
private haltenpublic Methoden bereitstellen