Jump to content

C++ Define operators (for vector)

- - - - -

  • Please log in to reply
2 replies to this topic

#1
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Hey,

I am trying to make this 'homework' :

/*Define class vector with components :
private pointer to integer vector and lenght of vector,
constructor with int parameter which creates zeroth vector with requested lenght,
second constructor int* type which creates vector on example of given array,
operators '>','<','==' which compare two vectors.
Write for class 'vector' operators that'll show '<<' vector on a screen in a shape of [v1,v2,...,vn]
and operator '>>' that will write vector v1,v2,...,vn from keyboard.
Define class NewVector that will inherit from class vector and also have :
variable for saving name of a vector, operator[] which writes el. of vector by requested index - if vector doesn't exist once shall
extend so the operation is correct.
add operator
extra :
function - write vector to a file with name that is also parameter of a class NewVector.
*/

I think I have correct greater, equal... operators from another program but now Im stuck with writing << and >> operators. I could really use some advice and probbably will need more ;p

Here is what I have done so far :

#include <cstdlib>

#include <iostream>

#include <vector>

#include <math.h>

using namespace std;


class wektor {

      private :

              vector <int> number;

              int length;

      public :

             wektor (int len) : length(len)

             {

                 for (int i=0;i<length;i++)

                     number[i]=0; 

             }     

                

             //wektor(int*);

             

             friend ostream& operator <<(ostream& out_str, wektor v);

             

             friend istream& operator >>(istream in_str, wektor &v);

};


ostream& operator <<(ostream& out_str, wektor v)

{

    cout <<" [";

    for (int i=0;i<v.length;i++)

    {

       out_str<<v.number[i]<<",";

       //return out_str;

    }

    return out_str;

    cout << "] ";

}


int main(int argc, char *argv[])

{

    wektor a(4);

    cout <<a;

    system("PAUSE");

    return EXIT_SUCCESS;

}

The program compiles(Dev C++ - I now its old but im used to it ;/) but It crashes just after start. "The program vector.exe has stopped working bla bla.."
Any ideas ? As you can see I tried to put return out_str; in the 'for' loop - it crashes the same.

Also I have a question, is it possible to write program like that in 1h30min ? Im asking cause this was one of my exam - which I failed.

Thanks in advance,
Notes.

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
It's a segfault, and it's happening in your wektor constructor:
             wektor (int len) : length(len)             {
                 for (int i=0;i<length;i++)
                     number[i]=0; 
             }
In order to do this correctly, you don't simply assign a value to a location and expect std::vector to resize itself accordingly, instead you use the push_back method, which will place the element on the end of the list and expand the list as necessary for any additional elements.
             wektor (int len) : length(len)
             {
                 for (int i=0;i<length;i++)
                     number.push_back(0); 
             }
Also, you have output bugs, but those should be pretty easy to solve. My computer doesn't have the trailing ']' get printed, has an additional ',' at the end, and prints "sh: PAUSE: not found" when you call system("PAUSE").
Wow I changed my sig!

#3
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Thanks for help.

I figured out rest by brushing my teeth today ;p

Here is whole code if any1 needed, feel free to give my any advice how to make it better if u want.

/*Define class vector with components :

 private pointer to integer vector, lenght of vector, 

 constructor with int parameter which creates zeroth vector with requested lenght,

 constructor  int* type which creates vector on example of given array,

 operators '>','<','==' which compare two vectors.

Write for class 'vector' operators that'll show '<<' vector on a screen in a shape of [v1,v2,...,vn]

  and operator '>>' that will write vector v1,v2,...,wn from keyboard.

Define class NewVector that will inherit from class vector and also have :

variable for saving name of a vector, operator[] which writes el. of vector by requested index - if vector doesn't exist once shall 

         extend so the operation is correct.

add operator

extra :

function - write vector to a file with name that is also parameter of a class NewVector.


*/


#include <cstdlib>

#include <iostream>

#include <vector>

#include <math.h>

#include <cstdio>

using namespace std;

//////////////////////////////CLASS WEKTOR /////////////////////////////////////

class wektor {

      private :

              vector <int> number;

              int length;

      public :

             wektor (int len) : length(len)

             {

                 for (int i=0;i<length;i++)

                    {

                        number.push_back(0);

                    } 

                    cout << "int constructor is working.\n"; //Just to know which one I initialized.

             }     

             wektor(int*);

             wektor()

             {

                 cout << "def. constructor\n"; // (?)

             }

             ~wektor(void)

             { }

             bool operator >(wektor);

	         bool operator <(wektor);

	         bool operator ==(wektor);

             friend ostream& operator <<(ostream& out_str, wektor v);             

             friend istream& operator >>(istream& in_str, wektor &v);

             void add(int n)

             {

                 number.push_back(n);

                 length++;

             }

             void indexAdd(int index, int value);

             

};


wektor::wektor(int* array)

{

    cout << "int* constructor is working.\n";

    int i=0;


        do

        {

            number.push_back(*array);

            array++;

            i++;

        }

        while(*array);

        length = i;        

}


bool wektor::operator > (wektor v)

{

    float total=0.00, total_v=0.00;

    bool gt;

    for (int i=0; i<length;i++)

    {

        total+=number[i];

    }

    for (int i=0;i<v.length;i++)

    {

        total_v+=v.number[i];

    }

    total/=length;

    total_v/=v.length;

    ( total>total_v ) ? gt=true : gt=false;

    return gt;

}


bool wektor::operator < (wektor v)

{

    float total=0.00, total_v=0.00;

    bool gt;

    for (int i=0; i<length;i++)

    {

        total+=number[i];

    }

    for (int i=0;i<v.length;i++)

    {

        total_v+=v.number[i];

    }

    total/=length;

    total_v/=v.length;

    ( total<total_v ) ? gt=true : gt=false;

    return gt;

}


bool wektor::operator == (wektor v)

{

    float total=0.00, total_v=0.00;

    bool gt;

    for (int i=0; i<length;i++)

    {

        total+=number[i];

    }

    for (int i=0;i<v.length;i++)

    {

        total_v+=v.number[i];

    }

    total/=length;

    total_v/=v.length;

    ( total==total_v ) ? gt=true : gt=false;

    return gt;

}


void wektor::indexAdd(int i, int v)

{

    

    if (length>=i)

    {

        number[i]=v;

    }

    if (length<i)

    {

        int differ=i-length;

        do

        {

            number.push_back(0);

            differ--;

            length++;

        }

        while(differ);

        number.push_back(v);

        length++;

    }

}

////////////////////////////////CLASS NEWWEKTOR////////////////////////////////


class nWektor : public wektor {

      public :

             char name[20];

             nWektor(char *n, int l);

             void add();

             void indexAdd();

};


nWektor::nWektor(char *n, int l) : wektor(l)

{

strcpy(name,n);

}


void nWektor::add()

{

    int n;

    cout << "What number would u like to add ? ";

    cin >> n; cout << endl;

    wektor::add((n));

}


void nWektor::indexAdd()

{

    int index=0, value=0;

    cout << "Which index would you like to change ? ";

    cin >> index;

    cout << "\nWhat value do you want to place there ? ";

    cin >>value;

    

    wektor::indexAdd((index-1), value);

}

//////////////////////////////////FRIENDS :)///////////////////////////////////

ostream& operator <<(ostream& out_str, wektor v)

{

    if (v.length>0)

    {

        cout <<" [";

        out_str<<v.number[0];

        for (int i=1;i<v.length;i++)

        {

           out_str<<", "<<v.number[i];

        } 

        cout << "] ";

    }

    else

        cout << "Error ! There is nothing to show\n";

    return out_str;

}


istream& operator >>(istream& in_str, wektor &v)

{

    int i=0;

    if (v.length>0)

    {

       for (i;i<v.length;i++)

       {

           cout << " Insert " <<(i+1)<<" value :";

           in_str >> v.number[i];

           

       }

    }

    else

       cout << "Error!! length of vector is 0!\n";

    return in_str;


}


int main(int argc, char *argv[])

{

    int ar[]={3,3,4,5,6};

    wektor a(5);

    cout <<a << endl;

    wektor b(ar);

    cout << b << endl;

    cin >> a;

    cout << endl << a << endl;

    char ch[20]=("My Name");

    int l=3;

    nWektor d(ch, l);

    d.add();

    cout << endl << d<< endl;

    d.indexAdd();

    cout << endl << d << endl;

    ( a== b ) ? cout << "a and b are equal\n" : cout <<"a and b are not equal\n";

    system("PAUSE");

    return EXIT_SUCCESS;

}


Edited by notes, 26 August 2011 - 04:48 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users