Code
classdemo.cpp
#include <iostream>
#include <string>
using namespace std;
// Alle Eigenschaften der Struktur sind öffentlich zugänglich
struct welcomeStruct {
string name;
string city;
};
class welcomeClass {
public:
// Konstruktor
welcomeClass() {
cout << "Instanz der Klasse erstellt!" << endl;
this->name = "Default Name";
this->city = "Default City";
}
// Destruktor
~welcomeClass() {
cout << "Bye, bye! Es war schön!" << endl;
}
// Schnittstellendefinitionen
// Getter
string getName() { return this->name; }
string getCity() { return this->city; }
// Setter
void setName(string argName) { this->name = argName; }
void setCity(string argCity) { this->city = argCity; }
private:
string name;
string city;
};
int main() {
// TODO Implementation einer Anwendung, die ihre Benutzer begrüßt: "Hallo <Name> aus <Stadt>!"
cout << "1) Variablen" << endl;
cout << "2) Strukturen" << endl;
cout << "3) Klassen" << endl << endl;
string name; // string name[10] mit name[0] bis name[9]
string city;
class welcomeClass helloClass;
cout << "Vorab gespeicherter Name: " << helloClass.getName() << endl << endl;
cout << "Name: ";
cin >> name;
cout << "Stadt: ";
cin >> city;
helloClass.setName(name);
helloClass.setCity(city);
cout << "Hallo " << helloClass.getName() << " aus " << helloClass.getCity() << "!" << endl;
}