Wenn fertig, bitte Fenster schließen
|
// Lösung zu Konstruktor und Destruktor // Dateien einbinden #include <iostream> #include <iomanip> #include <string> using std::cout; using std::endl; using std::setw; // Klassendefinition class Window { // enum mit Fensterstilen // enums sind hier Bitwerte da Fensterstile verodert werden können! public: enum eWINSTYLE {SYSMENU=0x01, MINBOX=0x02, MAXBOX=0x04, CLOSEBOX=0x08}; // Eigenschaften private: int width, height; int xPos, yPos; std::string title; enum eWINSTYLE style; // Memberfunktionen public: Window (const int, const int, const int, const int, const char *const, const eWINSTYLE s); ~Window(); void Paint() const; void MoveWin(const int, const int); void ResizeWin(const int, const int); }; // Konstruktor // ACHTUNG! Das string Objekt für den Fenstertitel sollte // über die Initialisiererliste initialisiert werden damit // es bei seiner Definition auch gleich initialisert wird! Window::Window(const int x, const int y, const int w, const int h, const char *const t, const eWINSTYLE s): title(t) { xPos = x; yPos = y; // Fensterposition width = w; height = h; // Fenstergrösse style = s; // Fensterstil cout << title << " erstellt\n"; } // Destruktor Window::~Window() { cout << title << " gelöscht\n"; // Nur Meldung ausgeben sonst nichts } // Fenster 'darstellen' void Window::Paint() const { cout << title << endl; cout << "Position:" << setw(3) << xPos << "," << setw(3) << yPos << endl; cout << "Grösse :" << setw(3) << width << "," << setw(3) << height << endl; cout << "Stil :" << style << endl << endl; } // Fenster verschieben void Window::MoveWin(const int x, const int y) { xPos = x; yPos = y; } // Fenstergrösse verändern void Window::ResizeWin(const int w, const int h) { width = w; height = h; } // main() Funktion int main () { // Fenster mit Standard-Stil definieren Window myWin(10,10,300,200,"Fenster1",Window::SYSMENU); // Fenster mit MINBOX und SYSMENU definieren Window yourWin(20,20,600,800,"Fenster2", Window::eWINSTYLE(Window::MINBOX|Window::SYSMENU)); // Fensterdaten ausgeben myWin.Paint(); yourWin.Paint(); // 1. Fenster verschieben myWin.MoveWin(30,30); // 2. Fenster in der Grösse verändern yourWin.ResizeWin(640,480); // Neue Fensterdaten ausgeben myWin.Paint(); yourWin.Paint(); } |