Problem z operatorem wyjścia
Ostatnio zmodyfikowano 2014-02-07 12:01
Pawel9218 Temat założony przez niniejszego użytkownika |
Problem z operatorem wyjścia » 2014-02-06 23:13:00 #include<iostream> #include<cstdlib> #include<vector>
using namespace std;
class figura { protected: public: virtual void wypisz( ostream & out ) { } virtual double pole() const = 0; friend ostream & operator <<( ostream & out, const figura & r ) { out << r.pole() << " "; return out; } };
class prostokat : public figura { protected: string * kolor; int a, b; public: prostokat() { } prostokat( const string & kol, const int & a1, const int & a2 ) : kolor( new string( kol ) ) , a( a1 ) , b( a2 ) { } double pole() const { return a * b; } ~prostokat() { cout << "destruktor prostokat" << endl; } void wypisz( ostream & out ) { out << this->pole(); } friend ostream & operator <<( ostream & out, const prostokat & r ) { out << r.pole() << " "; return out; } };
class prosto : public prostokat { protected: int c; public: prosto() { } prosto( const string & kol, const int & a1, const int & a2, const int & b ) : prostokat( kol, a1, a2 ) , c( b ) { } double pole() const { return a * b * c; } ~prosto() { cout << "destruktor prosto" << endl; } void wypisz( ostream & out ) { out << this->pole(); } friend ostream & operator <<( ostream & out, const prosto & r ) { out << r.pole() << " "; return out; } };
class kolo : public figura { protected: string * kolor; int r; public: kolo() { } kolo( const string & kol, const int & c ) : kolor( new string( kol ) ) , r( c ) { } double pole() const { return r * r; } ~kolo() { cout << "destruktor kolo" << endl; } void wypisz( ostream & out ) { out << this->pole(); } friend ostream & operator <<( ostream & out, const kolo & r ) { out << r.pole() << " "; return out; } };
int main() { figura * tab[ 5 ]; const kolo test1( "czarny", 100 ); const prosto test2( "szary", 2, 2, 2 ); tab[ 0 ] = new kolo( "czerwony", 1 ); tab[ 1 ] = new kolo; tab[ 2 ] = new prostokat( "niebieski", 1, 1 ); tab[ 3 ] = new prosto( "zielony", 1, 1, 1 ); tab[ 4 ] = new prosto; for( int i = 0; i < 5; i++ ) cout << tab[ i ]->pole << endl; system( "pause" ); return 0; }
Niestety pokazuje jakieś dziwne znaczki przy petli for jak próbuje wyrzucić na ekran wynik funkcji polę :([/i] |
|
MrPoxipol |
» 2014-02-06 23:38:08 |
|
alixir |
» 2014-02-07 07:32:59 cout << tab[ i ]->pole << endl; Brakuje nawiasu. cout << tab[ i ]->pole() << endl; |
|
Pawel9218 Temat założony przez niniejszego użytkownika |
» 2014-02-07 12:01:09 Dzięki wielkie ,strasznie głupi błąd.Do programowania za bardzo się nie nadaję. :( |
|
« 1 » |