Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*1. Dodaj klasy Trojkat i Prostokat
- 2. Modyfikujemy FabrykeFigur
- 3. Modyfikujemy menu
- 4. Modyfikujemy funkcje ustawParametry
- */
- #include <cstdlib>
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Figura
- {
- public:
- Figura() {}
- virtual double pole() { return 0; }
- virtual const char * nazwa() { return "Figura"; }
- };
- class Kwadrat : public Figura
- {
- public:
- Kwadrat( double bok = 0 ) : bok( bok ) {} //zwraca 0, gdyby nie dostał wartości; : lista inicjalizacyjna w konstruktorze
- double pole() { return bok * bok; }
- const char * nazwa() { return "Kwadrat"; }
- double bok;
- };
- class Kolo : public Figura
- {
- public:
- Kolo( double promien = 0 ) : promien( promien ) {}
- double pole() { return 3.14 * promien * promien; }
- const char * nazwa() { return "Kolo"; }
- double promien;
- };
- class Prostokat : public Figura
- {
- public:
- Prostokat( double bok1 = 0, double bok2 = 0 ) : bok1( bok1 ), bok2( bok2 ) {}
- double pole() { return bok1 * bok2; }
- const char * nazwa() { return "Prostokat"; }
- double bok1, bok2;
- };
- class Trojkat : public Figura
- {
- public:
- Trojkat( double podst = 0, double wys = 0 ) : podst( podst ), wys( wys ) {}
- double pole() { return 0.5 * podst * wys; }
- const char * nazwa() { return "Trojkat"; }
- double podst, wys;
- };
- class FabrykaFigur
- {
- public:
- Figura * zrobFigure( int jakFigura )
- {
- switch( jakFigura ) // tu dopisuj nowe
- {
- case 1 : return new Kwadrat;
- case 2 : return new Kolo;
- case 3 : return new Prostokat;
- case 4 : return new Trojkat;
- default : return 0;
- }
- }
- };
- class Program
- {
- public:
- Program( const char * nazwa = 0 )
- {
- cout << "\nWitaj w programie " << ( ( nazwa != 0 ) ? nazwa : "" ) << endl;
- }
- ~Program()
- {
- cout << "\n\nPa, Pa!\nNacisnij Enter by zakonczyc...";
- cin.ignore();
- cin.get();
- }
- int wyborFigury()
- {
- int wybor;
- cout << "\n1. Kwadrat\n2. Kolo\n3. Prostokat\n4. Trojkat\n0. Koniec\n?> ";
- cin >> wybor;
- return wybor;
- }
- void ustalParametry( Figura * f )
- {
- if( stricmp( f->nazwa(), "Kwadrat" ) == 0 ) // poriwnywanie dwoch stringow ta funkcja na oczatku, 0 jesli rowne;
- {
- double b;
- cout << "Podaj bok: " << endl;
- cin >> b;
- ( ( Kwadrat * ) f )->bok = b; // Tu wartosc odczytana z klawiatury, rzutowanie tu jest
- }
- // Ustalanie parametrow dla innych figur
- if( stricmp( f->nazwa(), "Kolo" ) == 0 )
- {
- double p;
- cin >> p;
- ( ( Kolo * ) f )->promien = p;
- }
- if( stricmp( f->nazwa(), "Prostokat" ) == 0 )
- {
- double b1, b2;
- cin >> b1;
- cin >> b2;
- // zapytac uzytkownika o bok
- ( ( Prostokat * ) f )->bok1 = b1;
- ( ( Prostokat * ) f )->bok2 = b2;
- }
- if( stricmp( f->nazwa(), "Trojkat" ) == 0 )
- {
- double po, w;
- cin >> po;
- cin >> w;
- ( ( Trojkat * ) f )->podst = po;
- ( ( Trojkat * ) f )->wys = w;
- }
- }
- void wykonajSie()
- {
- Figura * f = 0;
- int wybor;
- FabrykaFigur fabryka;
- do
- {
- wybor = wyborFigury();
- f = fabryka.zrobFigure( wybor );
- if( f != 0 )
- {
- ustalParametry( f );
- liczIPisz( f );
- delete f;
- }
- }
- while( wybor != 0 );
- }
- void liczIPisz( Figura * figura )
- {
- cout << "\nFigura: " << figura->nazwa();
- cout << "\nPole: " << figura->pole();
- }
- };
- int main()
- {
- Program * mojProgramWspanialy = new Program( "Jedyny Taki :)" );
- mojProgramWspanialy->wykonajSie();
- delete mojProgramWspanialy;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement