Aufgabe zu Klasse mit GraphObj- punkt, rechteck, kreis



  • Ich weiss jetzt zwar nicht genau, was du meinst, aber mach doch eine abstrakte Funktion für die Fläche und implementier die dann spezifisch für den Kreis und das Rechteck.



  • also so ?
    So Fläche des Rechtecks klappt noch nicht, zuweit gedacht ?

    /*
     * Graphobjekte.cpp
     *
     *  Created on: 04.02.2010
     *      Author: c.groupe
     */
    
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    class Point
    {
    public:
    	Point() {itsX = itsY = 0;}
    	Point(double x, double y) : itsX(x), itsY(y) {}
    	double getX()const { return itsX;}
    	double getY()const { return itsY;}
    	void setX(double x) { itsX = x; }
    	void setY(double y) { itsY = y; }
    private:
    	double itsX;
    	double itsY;
    };
    
    Point operator+ (const Point& left, const Point& right){
    	return Point( left.getX()+right.getX(), left.getY()+right.getY() );
    }
    
    Point operator- (const Point& left, const Point& right){
    	return Point( left.getX()-right.getX(), left.getY()-right.getY() );
    }
    
    Point operator* (const Point& point, int other){
    	return Point ( point.getX() * other , point.getY() * other);
    }
    
    Point operator/ (const Point& point, int div){
    	return Point ( point.getX() / div , point.getY() / div);
    	// hier wird klar: Es gibt Rundungsfehler, wenn die
    	// Division nicht aufgeht! Darum wird in Fällen wo Rechteck
    	// ungerade Seitenlängen hat ein falscher Mittelpunkt berechnet!
    }
    
    double distanz( const Point& left, const Point& right){
    	return sqrt( ( left.getX()*right.getX() )+( left.getY()*right.getY() ) );
    }
    
    class GraphObj
    {
    public:
    	GraphObj(){}
    	virtual double flaeche()=0;
    	virtual Point referenzpunkt() const =0;
    };
    
    double distanz( const GraphObj& left, const GraphObj& right){
    	return distanz( left.referenzpunkt(), right.referenzpunkt() );
    }
    
    class Rechteck : public GraphObj
    {
    public:
    	Rechteck(){}
    	Rechteck( Point ul, Point br, Point bl)
    	: itsUpperLeft(ul), itsBottomRight(br), itsBottomLeft(bl) {}
    	Point referenzpunkt() const { return (itsUpperLeft + itsBottomRight) / 2; }
    	virtual double flaeche()=0;
    	void setUpperLeft ( Point upperleft ) { itsUpperLeft = upperleft; }
    	Point getUpperLeft() const { return itsUpperLeft; }
    	void setBottomRight ( Point bottomright ) { itsBottomRight = bottomright; }
    	Point getBottomRight() const { return itsBottomRight; }
    	void setBottomLeft ( Point bottomleft ) { itsBottomLeft = bottomleft; }
    	Point getBottomLeft() const { return itsBottomLeft; }
    
    private:
    //  Point p1, p2; umbenannt in:
    	Point itsUpperLeft;
    	Point itsBottomRight;
    	Point itsBottomLeft;
    };
    
    class Kreis : public GraphObj
    {
    public:
    	Kreis(){}
    	Kreis(float r)  { itsRadius = r; }
    	Point mittelpunkt() const { return itsMittelpunkt; }
    	virtual double flaeche()=0;
    	void setRadius ( int radius ) { itsRadius = radius; }
    	double getRadius() const { return itsRadius; }
    
    private:
    	Point itsMittelpunkt;
    	float itsRadius;
    };
    
    double Rechteck::flaeche()
    {
    	Point hoehe, breite;
    	hoehe = getBottomLeft()-getUpperLeft();
    	breite = getBottomLeft()-getBottomRight();
    	return hoehe*breite;
    }
    
    double Kreis::flaeche()
    {
    	return getRadius()*getRadius()*3.1415;
    }
    
    int main()
    {
    
    }
    


  • manno ,entwerder ich bekomme die fläche von rechteck hin, oder die fläche von kreis, beides klappt irgendwie nicht, weil ich einfach point nicht in double beziehen kann oder umgekehrt, ausserdem ist der radius in float.. ach man

    /*
     * Graphobjekte.cpp
     *
     *  Created on: 04.02.2010
     *      Author: c.groupe
     */
    
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    class Point
    {
    public:
    	Point() {itsX = itsY = 0;}
    	Point(double x, double y) : itsX(x), itsY(y) {}
    	double getX()const { return itsX;}
    	double getY()const { return itsY;}
    	void setX(double x) { itsX = x; }
    	void setY(double y) { itsY = y; }
    private:
    	double itsX;
    	double itsY;
    };
    
    Point operator+ (const Point& left, const Point& right){
    	return Point( left.getX()+right.getX(), left.getY()+right.getY() );
    }
    Point operator- (const Point& left, const Point& right){
    	return Point( left.getX()-right.getX(), left.getY()-right.getY() );
    }
    Point operator* (const Point& hoehe, const Point& breite){
    	return Point ( hoehe.getX()*breite.getX() , hoehe.getY()*breite.getY() );
    }
    Point operator/ (const Point& point, int div){
    	return Point ( point.getX() / div , point.getY() / div);
    	// hier wird klar: Es gibt Rundungsfehler, wenn die
    	// Division nicht aufgeht! Darum wird in Fällen wo Rechteck
    	// ungerade Seitenlängen hat ein falscher Mittelpunkt berechnet!
    }
    
    double distanz( const Point& left, const Point& right){
    	return sqrt( ( left.getX()*right.getX() )+( left.getY()*right.getY() ) );
    }
    
    class GraphObj
    {
    public:
    	GraphObj(){}
    	virtual Point flaeche()=0;
    	virtual Point referenzpunkt() const =0;
    };
    
    double distanz( const GraphObj& left, const GraphObj& right){
    	return distanz( left.referenzpunkt(), right.referenzpunkt() );
    }
    
    class Rechteck : public GraphObj
    {
    public:
    	Rechteck(){}
    	Rechteck( Point ul, Point br, Point bl)
    	: itsUpperLeft(ul), itsBottomRight(br), itsBottomLeft(bl) {}
    	Point referenzpunkt() const { return (itsUpperLeft + itsBottomRight) / 2; }
    
    	virtual Point flaeche()=0;
    
    	void setUpperLeft ( Point upperleft ) { itsUpperLeft = upperleft; }
    	void setBottomRight ( Point bottomright ) { itsBottomRight = bottomright; }
    	void setBottomLeft ( Point bottomleft ) { itsBottomLeft = bottomleft; }
    	Point getUpperLeft() const { return itsUpperLeft; }
    	Point getBottomRight() const { return itsBottomRight; }
    	Point getBottomLeft() const { return itsBottomLeft; }
    
    private:
    //  Point p1, p2; umbenannt in:
    	Point itsUpperLeft;
    	Point itsBottomRight;
    	Point itsBottomLeft;
    };
    
    class Kreis : public GraphObj
    {
    public:
    	Kreis(){}
    	Kreis(float r)  { itsRadius = r; }
    	Point mittelpunkt() const { return itsMittelpunkt; }
    
    	virtual Point flaeche()=0;
    
    	void setRadius ( float radius ) { itsRadius = radius; }
    	float getRadius() const { return itsRadius; }
    
    private:
    	Point itsMittelpunkt;
    	float itsRadius;
    };
    Point Rechteck::flaeche()
    {
    	Point hoehe, breite;
    	hoehe = getBottomLeft()-getUpperLeft();
    	breite = getBottomLeft()-getBottomRight();
    	return hoehe*breite;
    }
    Point Kreis::flaeche()
    {
    	return getRadius()*getRadius()*3.1415;
    }
    
    int main()
    {
    
    }
    


  • und wenn ich meine Main erstelle, mit einem Zeiger auf zwei elemente,
    kann ich die funktion flaeche und referenzpunkt nicht abstrackt lassen ,warum ?

    /*
     * Graphobjekte.cpp
     *
     *  Created on: 04.02.2010
     *      Author: c.groupe
     */
    
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    class Point
    {
    public:
    	Point() {itsX = itsY = 0;}
    	Point(double x, double y) : itsX(x), itsY(y) {}
    	double getX()const { return itsX;}
    	double getY()const { return itsY;}
    	void setX(double x) { itsX = x; }
    	void setY(double y) { itsY = y; }
    private:
    	double itsX;
    	double itsY;
    };
    
    Point operator+ (const Point& left, const Point& right){
    	return Point( left.getX()+right.getX(), left.getY()+right.getY() );
    }
    Point operator- (const Point& left, const Point& right){
    	return Point( left.getX()-right.getX(), left.getY()-right.getY() );
    }
    Point operator* (const Point& hoehe, const Point& breite){
    	return Point ( hoehe.getX()*breite.getX() , hoehe.getY()*breite.getY() );
    }
    Point operator/ (const Point& point, int div){
    	return Point ( point.getX() / div , point.getY() / div);
    	// hier wird klar: Es gibt Rundungsfehler, wenn die
    	// Division nicht aufgeht! Darum wird in Fällen wo Rechteck
    	// ungerade Seitenlängen hat ein falscher Mittelpunkt berechnet!
    }
    
    double distanz( const Point& left, const Point& right){
    	return sqrt( ( left.getX()*right.getX() )+( left.getY()*right.getY() ) );
    }
    
    class GraphObj
    {
    public:
    	GraphObj(){}
    	virtual Point flaeche();
    	virtual Point referenzpunkt() const;
    };
    
    double distanz( const GraphObj& left, const GraphObj& right){
    	return distanz( left.referenzpunkt(), right.referenzpunkt() );
    }
    
    class Rechteck : public GraphObj
    {
    public:
    	Rechteck(){}
    	Rechteck( Point ul, Point br, Point bl)
    	: itsUpperLeft(ul), itsBottomRight(br), itsBottomLeft(bl) {}
    	Point referenzpunkt() const { return (itsUpperLeft + itsBottomRight) / 2; }
    
    	virtual Point flaeche();
    
    	void setUpperLeft ( Point upperleft ) { itsUpperLeft = upperleft; }
    	void setBottomRight ( Point bottomright ) { itsBottomRight = bottomright; }
    	void setBottomLeft ( Point bottomleft ) { itsBottomLeft = bottomleft; }
    	Point getUpperLeft() const { return itsUpperLeft; }
    	Point getBottomRight() const { return itsBottomRight; }
    	Point getBottomLeft() const { return itsBottomLeft; }
    
    private:
    //  Point p1, p2; umbenannt in:
    	Point itsUpperLeft;
    	Point itsBottomRight;
    	Point itsBottomLeft;
    };
    
    class Kreis : public GraphObj
    {
    public:
    	Kreis(){}
    	Kreis(float r)  { itsRadius = r; }
    	Point mittelpunkt() const { return itsMittelpunkt; }
    
    	virtual Point flaeche();
    
    	void setRadius ( float radius ) { itsRadius = radius; }
    	float getRadius() const { return itsRadius; }
    
    private:
    	Point itsMittelpunkt;
    	float itsRadius;
    };
    Point Rechteck::flaeche()
    {
    	Point hoehe, breite;
    	hoehe = getBottomLeft()-getUpperLeft();
    	breite = getBottomLeft()-getBottomRight();
    	return hoehe*breite;
    }
    Point Kreis::flaeche()
    {
    	Point ra, pi;
    //	return getRadius()*getRadius();
    	return ra*pi;
    }
    
    int main()
    {
    	GraphObj* graphobj[2];
    	graphobj[0]	= new Rechteck;
    	graphobj[1]	= new Kreis;
    
    	for ( int i=0; i<2; ++i)
    		graphobj[i]->flaeche();
    	for ( int i=0; i<2; ++i){
    		graphobj[i]->referenzpunkt();
    		delete graphobj[i];
    	}
    }
    

    zeigt zwar keine fehler an, aber geht nicht.



    1. Bitte in Zukunft nur noch den relevanten Code posten! Sonst liest sich das keiner mehr durch...
    2. flaeche kannst du nicht als Point ausdrücken, oder?
      Ein Punkt ist nämlich per Definition ausdehnungslos, und somit kann er niemals eine Fläche darstellen!
    3. "Point hoehe, breite;"
      ist das selbe, Punkt ist ausdehnungslos und blahblah 😉
    4. "virtual double flaeche() const =0;"
      also pure virtual deklaration ist nur in GraphObj nötig, in Kreis und Rechteck willst du das ja implementieren!

    Hier also der relevante Code:

    class GraphObj
    {
    public:
        virtual double flaeche() const =0;
    };
    
    class Rechteck : public GraphObj
    {
    public:
        double flaeche() const;
    };
    
    class Kreis : public GraphObj
    {
    double m_radius;  // von mir aus auch float, ist wurscht...
    public:
        double flaeche() const;
    };
    
    double
    Kreis::flaeche() const
    {
        return m_radius * m_radius * M_PI;  // M_PI kommt aus <cmath>
    }
    
    double
    Rechteck::flaeche() const
    {
        double hoehe = /* tja, du hast zwei Punkte, bottomRight und topLeft,
                          da solltest du doch hoehe und breite ausrechnen können */;
        double breite = /* Berechnung hier */;
        return hoehe * breite;
    }
    


  • danke, habs mal abgeändert:

    Stimts mit der höhe ?

    hier mein aktueller code: Edit: update.. 1 fehler left

    /*
     * Graphobjekte.cpp
     *
     *  Created on: 04.02.2010
     *      Author: c.groupe
     */
    
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    class Point
    {
    public:
    	Point() {itsX = itsY = 0;}
    	Point(double x, double y) : itsX(x), itsY(y) {}
    	double getX()const { return itsX;}
    	double getY()const { return itsY;}
    	void setX(double x) { itsX = x; }
    	void setY(double y) { itsY = y; }
    private:
    	double itsX;
    	double itsY;
    };
    
    Point operator+ (const Point& left, const Point& right){
    	return Point( left.getX()+right.getX(), left.getY()+right.getY() );
    }
    Point operator- (const Point& left, const Point& right){
    	return Point( left.getX()-right.getX(), left.getY()-right.getY() );
    }
    Point operator* (const Point& hoehe, const Point& breite){
    	return Point ( hoehe.getX()*breite.getX() , hoehe.getY()*breite.getY() );
    }
    Point operator/ (const Point& point, int div){
    	return Point ( point.getX() / div , point.getY() / div);
    	// Achtung: Rundungsfehler, wenn die Division nicht aufgeht!
    	// Rechteck mit ungerade Seitenlängen berechnet einen falschen Mittelpunkt!
    }
    
    double distanz( const Point& left, const Point& right){
    	return sqrt( ( left.getX()*right.getX() )+( left.getY()*right.getY() ) );
    }
    
    class GraphObj
    {
    public:
    //	GraphObj(){}
    	virtual double flaeche() const =0;
    	virtual Point referenzpunkt() const;
            virtual ~GraphObj() {}
    };
    
    double distanz( const GraphObj& left, const GraphObj& right){
    	return distanz( left.referenzpunkt(), right.referenzpunkt() );
    }
    
    class Rechteck : public GraphObj
    {
    public:
    	Rechteck(){}
    	Rechteck( Point ul, Point br)
    	: itsUpperLeft(ul), itsBottomRight(br) {}
    	Point referenzpunkt() const { return (itsUpperLeft + itsBottomRight) / 2; }
    
    	double flaeche() const;
    
    	void setUpperLeft ( Point upperleft ) { itsUpperLeft = upperleft; }
    	void setBottomRight ( Point bottomright ) { itsBottomRight = bottomright; }
    	Point getUpperLeft() const { return itsUpperLeft; }
    	Point getBottomRight() const { return itsBottomRight; }
    
    private:
    //  Point p1, p2; umbenannt in:
    	Point itsUpperLeft;
    	Point itsBottomRight;
    };
    
    class Kreis : public GraphObj
    {
    public:
    	Kreis(){}
    	Kreis(float r)  { itsRadius = r; }
    	Point referenzpunkt() const { return itsMittelpunkt; }
    
    	double flaeche() const;
    
    	void setRadius ( float radius ) { itsRadius = radius; }
    	float getRadius() const { return itsRadius; }
    
    private:
    	Point itsMittelpunkt;
    	double itsRadius;
    };
    
    double Rechteck::flaeche() const
    {
            double hoehe = itsUpperLeft.getX()-itsBottomRight.getX();
    	double breite = itsBottomRight.getY()-itsUpperLeft.getY();
    	return hoehe*breite;
    }
    
    double Kreis::flaeche() const
    {
    return getRadius()*getRadius()*M_PI;
    }
    
    int main()
    {
    	GraphObj* graphobj[2];
    	graphobj[0]	= new Rechteck;
    	graphobj[1]	= new Kreis;
    
    	for ( int i=0; i<2; ++i)
    		graphobj[i]->flaeche();
    	for ( int i=0; i<2; ++i){
    		graphobj[i]->referenzpunkt();
    		delete graphobj[i];
    	}
    }
    


  • kanns immernoch nicht starten:

    1. undefined reference to vtable for GraphObj' 2\. undefined reference toGraphObj::referenzpunkt() const'

    finde aber nicht den fehler..



  • Wo ist Kreis::referenzpunkt() const?



  • l'abra d'or schrieb:

    Wo ist Kreis::referenzpunkt() const?

    ...
    //  Point mittelpunkt() const { return itsMittelpunkt; }
    ...
    Point referenzpunkt() const { return itsMittelpunkt; }
    ...
    

    gut habs übersehen.

    bleibt noch ein fehler:
    " undefined reference to `vtable for GraphObj' "



  • GraphObj braucht definitiv keinen Konstruktor, hingegen wäre ein virtueller Destruktor wünschenswert:

    class GraphObj
    {
    public:
        virtual ~GraphObj() {}
    };
    


  • l'abra d'or schrieb:

    GraphObj braucht definitiv keinen Konstruktor, hingegen wäre ein virtueller Destruktor wünschenswert:

    class GraphObj
    {
    public:
        virtual ~GraphObj() {}
    };
    

    gut einen schritt weiter.. 😕



  • bekomme es immernoch nicht hin.
    findet wohl auch keiner den bug.. 😕



  • Wenn du schreibst "gut einen Schritt weiter" nehm ich an es ist alles i.O.

    Zum Problem:
    virtual Point referenzpunkt() const =0;
    Dann geht es auch.
    Ohne dem "=0" solltest du auch eine Implementierung abgeben - macht hier aber gar keinen Sinn.



  • l'abra d'or_off schrieb:

    Wenn du schreibst "gut einen Schritt weiter" nehm ich an es ist alles i.O.

    Zum Problem:
    virtual Point referenzpunkt() const =0;
    Dann geht es auch.
    Ohne dem "=0" solltest du auch eine Implementierung abgeben - macht hier aber gar keinen Sinn.

    ach klar, wie geil, und wie lange ich an dieser Aufgabe schon dran bin, hätte ich
    ohne eure hilfe nicht geschaft, danke an alle und besonder dir l'abra d'or_off!


Anmelden zum Antworten