Hej, ćwicząc nowo nabywaną wiedzę zacząłem projektować prostą klasę bazową o nazwie BaseRectangle, z niej dziedziczy klasa opisująca kwadrat oraz opisująca prostokąt. Dla klas potomnych chciałem przeciążyć operator<<
( jego definicja wygląda inaczej dla dwóch klas dziedziczących ), problem jest jednak taki że podany operator nie powinna przeciążać metoda lecz zwykła funkcja z pierwszy argumentem jako referencja na obiekt klasy std::ostream a z drugim jako np. stała referencja na nasz obiekt. Wobec tego funkcja operatora<< wywołuje odpowiednią metodę wirtualną ( przeciążającą << ) w zależności od podanego w argumencie obiektu dziedziczącego po BaseRectangle. Wszystko wygląda tak:
#include <iostream>
#include <string>
class BaseRectangle;
std::ostream& operator<<(std::ostream&, const BaseRectangle&); //drugi parametr to
//referencja typu klasy abstrakcyjnej
///---------------------------------------------------------------------------------------
class BaseRectangle { //klasa abstrakcyjna
friend std::ostream& operator<<(std::ostream&, const BaseRectangle&);
public:
struct Coords { int x; int y; };
private:
Coords pos;
public:
BaseRectangle(int pos_x=0, int pos_y=0) : pos{ pos_x,pos_y } {}
Coords& returnPos() {
return pos;
}
Coords returnPos() const {
return pos;
}
void move(int x, int y) {
pos = { x,y };
}
virtual long area() = 0;
protected:
virtual std::ostream& operator<<(std::ostream& os) const = 0; //by klasy potomne wiedzialy o istnieniu tej metody i mogły mieć jej
//własną definicje ale by reszta programu nie miała do tej metody dostępu
};
///---------------------------------------------------------------------------------------
class Rectangle : public BaseRectangle { //prostokąt
friend std::ostream& operator<<(std::ostream&, const BaseRectangle&);
virtual std::ostream& operator<<(std::ostream& os) const { //prywatna sekcja by nie moc stosować zapisu w stylu:
//Rectangle << cout; ( z tej metody korzysta funkcja zaprzyjaźniona )
os << "width = " << width << ", height = " << height << std::endl;
os << "pos(x,y) = ( " << returnPos().x << ", " << returnPos().y << " )\n";
return os;
}
private:
int width;
int height;
public:
Rectangle(int w=0, int h=0, int x=0, int y=0) : BaseRectangle(x,y), width(w), height(h) {}
virtual long area() {
return width*height;
}
};
///---------------------------------------------------------------------------------------
class Square : public BaseRectangle { //kwadrat
friend std::ostream& operator<<(std::ostream&, const BaseRectangle&);
virtual std::ostream& operator<<(std::ostream& os) const { //taka sama sytuacja
os << "dimension = " << dimension << std::endl;
os << "pos(x,y) = ( " << returnPos().x << ", " << returnPos().y << " )\n";
return os;
}
private:
int dimension;
public:
Square(int d=0, int x=0, int y=0) : BaseRectangle(x, y), dimension(d) {}
virtual long area() {
return dimension*dimension;
}
};
///---------------------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const BaseRectangle& bs) {
bs << os; //wywołanie prywatnej metody
//operatora<< w zależności od podanego argumentu
return os;
}
Błędy kompilacji jaki mam:
Jestem w temacie dziedziczenia jeszcze bardzo zielony, trochę tu nakombinowałem i musiałem coś zawalić, wiecie może co? Czy w ogóle dobrze myślałem/kombinowałem nad sposobem rozwiązania tego problemu?
Z góry bardzo wam dziękuje za pomoc ;)