Arithmetic Vector class



  • Hallo allerseits,
    bin ein Neuling was das Thema betrifft, hoffe es kann mir einer Helfen, den Startpunkt zu finden...

    Aufgabe:

    you will implement an Arithmetic Vector class inherited from the vector class in the standart template library. On this inherited class, some arithmetic operations can be applied. Some rules must be taken into consideration and if the given rules are not met, error messages must be thrown.
    - To sum two vector objects, their sizes must be equal.
    - To divide two vector objects, their sizes must be equal and the divisor vector should not
    include any elements which is 0.
    - You can simply apply divide operator as (Assume two vectors with the size of 3);
    a1 a2 a3 b1 b2 b3 a1/b1 a2/b2 a3/b3
    - A vector object cannot be compared or assigned to another object if their sizes are different.
    - “Index out of bounds” message should be printed if one tries to access vector with the non-
    existing index values.
    A test program is given below. It illustrates the usage of all the methods and operators you will implement. Your implementation must be compatible with this test program.

    #include <iostream> 
    #include <cstdlib> 
    #include <ctime> 
    #include "ArithmeticVector.h" 
    using namespace std; 
    int main() { 
        srand(time(NULL)); 
        ArithmeticVector<int> v1(3); // creating some objects 
        ArithmeticVector<int> v2(3); // vector elements are assigned randomly from 0 to 10 
        ArithmeticVector<double> v3(5); 
        ArithmeticVector<double> v4(5); 
        try { 
            // trying to get the element at(4) 
            // should give an error 
            cout << v1[4] << endl; 
        } catch (const string & err_msg) { 
            cout << err_msg << endl; 
        } 
        cout << "Printing v4" << endl; 
        v4.print(); 
        try { // trying to divide two vectors 
        ArithmeticVector<double> v6 = v4 / v3; 
        cout << "Printing v6" << endl; 
        v6.print(); }
        catch (const string & err_msg) { 
            cout << err_msg << endl; 
        } 
        if (v1.contains(4)) 
            // checking if the Vector has an element with value 4 
            cout << "Vector contains the element" << endl; 
        else 
            cout << "Vector does not contain the element" << endl;      
        ArithmeticVector<int> v5 = v2; 
        cout << "Printing v5" << endl; 
        v5.print(); 
        try { 
            v1 = v2 + v5; // sum v2 and v5 vectors and assign result to v1 
            --v1; // decrement v1's vector elements by 1 
            v5 = v2 / v1; // divide v2 and v1 vectors and assign result to v5 
            ++v5; // increment v5's vector elements by 1 
            if (v5 == v1) // comparing two vectors 
                cout << "Objects are equal" << endl; 
            else 
                cout << "Objects are not equal" << endl; 
        } catch (const string & err_msg) { 
            cout << err_msg << endl; 
        } cout << "Printing v1" << endl; 
        v1.print(); 
        cout << "Printing v5" << endl; 
        v5.print(); 
    }
    

  • Mod

    Was ist deine Frage? So wie ich das derzeit sehe ist deine Frage "kann mir jemand die Hausaufgaben machen?". Was denkst du, sind die Erfolgsaussichten?


  • Mod

    Eine typische drauf-los-Aufgabe, die genau in der Reihenfolge bearbeitet werden kann, in der sie gestellt ist.



  • da steht, den startpunkt zu finden und nicht macht mir meine Hausaufgaben.
    Meine frage lautet, wie soll ich eine Vector klasse erstellen? und wie genau arbeitet man damit?



  • you will implement an Arithmetic Vector class inherited from the vector class in the standard template library.

    Das ist ja gleich doppelt verkehrt -- std::vector ist keine Klasse, und aus std::vector gestanzte Klassen sind nicht polymorphisch. Wer denkt sich so eine Aufgabe aus?


  • Mod

    ChillStudent schrieb:

    da steht, den startpunkt zu finden und nicht macht mir meine Hausaufgaben.
    Meine frage lautet, wie soll ich eine Vector klasse erstellen?

    Wie wäre es damit, als Anfang mit "inherited from the vector class in the template library"?

    Das ist übrigens eher keine so gute Idee, auch wenn es die Aufgabe ist. Der Aufgabensteller kann vermutlich nicht so gut C++. Ein mathematischer Vektor ist kein Datenvektor, was aber durch diese Beziehung zum Ausdruck gebracht wird. Viele Operationen, die geerbt würden, sind auf mathematischen Vektoren absoluter Schwachsinn. 👎 Zudem hat ein std::vector weder virtuelle noch protected Member (insbesondere keinen virtuellen Destruktor!), womit Vererbung ohnehin kritisch ist, sowohl vom Sinn her, als auch von der technischen Umsetzung.

    und wie genau arbeitet man damit?

    Das zeigt dir doch das Testprogramm:

    It illustrates the usage of all the methods and operators you will implement.

    Die Operationen sind etwas komisch, vermutlich damit du nirgendwo abschreibst *winkmitdemzaunpfahl*.



  • Ist nur eine Vermutung...? könnte das so hinkommen?

    template<class T>
    class ArithmeticVector {
    public:
      ArithmeticVector(T f1, T f2) {
        x=f1;
        y=f2;
      }
      ArithmeticVector() {
      }
      T x;
      T y;
    
      ArithmeticVector operator+(const ArithmeticVector& v1) {
        ArithmeticVector result;
        result.x = v1.x+this->x;
        result.y = v1.y+this->y;
        return result;
      };
      ArithmeticVector operator+(const ArithmeticVector& v2) {
        ArithmeticVector result;
        result.x = v2.x+this->x;
        result.y = v2.y+this->y;
        return result;
      };
    };
    


  • Gibt es einen Grund wieso du den Operator zweimal überlädst?

    Weiteres:

    ArithmeticVector(T f1, T f2) {
        x=f1;
        y=f2;
      }
    

    Googlestichwort: Initialisierungslisten ➡

    ArithmeticVector(T f1, T f2):
          x(f1),
          y(f2) {}
    
    ArithmeticVector operator+(const ArithmeticVector& v1) {
        ArithmeticVector result;
        result.x = v1.x+this->x;
        result.y = v1.y+this->y;
        return result;
      };
    

    ➡

    ArithmeticVector operator+(const ArithmeticVector& v1) const // Die Funktion hat jetzt einen const-qualifier
      {
        return ArithmeticVector( v1.x + x, v2.y + y ); // Bzw. wenn du einen C++11-konformen Compiler hast, mit einer initializer-list: return {... };
      };
    


  • ...



  • Swordfish schrieb:

    Nur um sicherzugehen: Wenn sichergestellt wird, daß Instanzen der erbenden Klasse niemals über einen Basisklassenzeiger deleted werden, ist alles supi?

    Jo, gibt Fälle, da ist public Vererbung von Containern ok:

    struct line : std::string {};
    std::istream& operator>>(std::istream& in, line& l) { return getline(in, l); }
    

    Gibt aber auch Fälle, da ist das nicht ok, z.B. einen Vektor von std::vector abzuleiten nur um Funktionalität hinzuzufügen, die auch über freie Funktionen möglich wäre UND um gleichzeitig Funktionalität einzuschränken.

    Das bedeutet, dass, falls der Autor der Aufgabe auch nur etwas taugt (Alarmzeichen: STL != Container+Algorithmen in std), hier private Vererbung das Mittel zum Zweck wäre.


Anmelden zum Antworten