Wenn fertig, bitte Fenster schließen
|
// Lösung zu Default-Parameter // Dateien einbinden #include <iostream> #include <iomanip> using std::cout; using std::endl; // Für verkürzte Schreibweise typedef unsigned char BYTE; // Struktur für Farbwerte struct Color { BYTE red; BYTE green; BYTE blue; }; // Klassendefinition class Rect { short xPos, yPos; // Position short width, height; // Grösse Color actColor; // Farbe public: // ctor, ein dtor wird hier nicht benötigt Rect(const short x, const short y, const short w, const short h, const BYTE r=0x00, const BYTE g=0x00, const BYTE b=0x00); // Verschieben void Move(const short x, const short y); // Grösse ändern void Resize(const short w, const short h); // Farbe umsetzen void SetColor(const BYTE r, const BYTE g, const BYTE b); // Rechteck zeichnen void DrawIt() const; }; // Definition der Memberfunktionen // ctor Rect::Rect(const short x, const short y, const short w, const short h, const BYTE r, const BYTE g, const BYTE b) { xPos = x; yPos = y; width = w; height = h; actColor.red = r; actColor.green = g; actColor.blue = b ; } // Rechteck verschieben inline void Rect::Move(const short x, const short y) { xPos = x; yPos = y; } // Rechteck in der Grösse ändern inline void Rect::Resize(const short w, const short h) { width = w; height = h; } // Rechteck-Farbe setzen inline void Rect::SetColor(const BYTE r, const BYTE g, const BYTE b) { actColor.red = r; actColor.green = g; actColor.blue = b; } // Rechteck 'zeichnen' void Rect::DrawIt() const { cout << "Position: " << xPos << ',' << yPos << endl; cout << "Grösse : " << width << ',' << height << endl; cout << std::hex << std::showbase; cout << "RGB-Wert: " << static_cast<int>(actColor.red) << ',' << static_cast<int>(actColor.green) << ',' << static_cast<int>(actColor.blue) << endl; cout << std::dec << std::noshowbase; } // 1. (globales) Rechteck definieren Rect Rect1(10,10,640,480); // main() Funktion int main() { // 2. (lokales) Rechteck definieren Rect Rect2(100,50,800,600,0x80,0x80,0x80); // Rechteckdaten ausgeben cout << "1. Rechteck:\n"; Rect1.DrawIt(); cout << "2. Rechteck:\n"; Rect2.DrawIt(); // 1. Rechteck verschieben Rect1.Move(20,20); // 2. Rechteck vergrössern und Farbe abändern Rect2.Resize(1024,786); Rect2.SetColor(0xC0, 0xC0, 0xC0); // Rechteckdaten ausgeben cout << "1. Rechteck:\n"; Rect1.DrawIt(); cout << "2. Rechteck:\n"; Rect2.DrawIt(); } |