Class methods

This is a discussion on Class methods within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++ so bare with me please. Here is the vec.h file that contains (class Vec), all its ...

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    Class methods

    I'm new to C++ so bare with me please.

    Here is the vec.h file that contains (class Vec), all its methods and two attributes. Below the class are three inline functions.

    Code:
    class Vec
    {
      public:
        Vec( );                 // constructor
    
        void push_back( int );  // similar to vector
        void pop_back( );       // remove last element in Vec
    	
        void erase( int idx );  // erase element at position idx
    	
        // optional:            // erase howmany elements starting at idx
        /*void erase( int idx, int howmany );*/
    
                                // insert a value at position idx
        void insert( int idx, int value );
    
        int  size( ) const;     // number of elements in array
                                // use operator [ ] as in a vector
        int  operator[ ]( int ) const;
    
      private:
        int array[ 5 ];         // local attribute
        int loc;                // local index
    };
    
    
    inline Vec::Vec( )          // constructor
    {
        cout << "in Vec::Vec( )\n";
        loc = 0;                // loc refers to the next empty location
    }
    
                                // size of the object
    inline int Vec::size( ) const
    {
        return loc;
    }
    
                                // return a copy of element at index i
    inline int Vec::operator [ ]( int i ) const
    {
        if( i >= 0  &&  i < loc )
        {                       // i is within the range of elements in array
            return array[ i ];
        }
        else
        {
            return -1;          // invalid index; return -1
        }
    }
    I am trying to create vec.cpp that contains the definitions for (class Vec) methods. Push_back and pop_back seem to work ok. I'm having some issues with the proper syntax for erase. I'm sure i will have issues with insert as well.

    Code:
    // methods for class Vec
    
    #include<iostream>
    #include<iomanip>
    using namespace std;
    #include "vec.h"
    
    
    void Vec::push_back(int i)
    {
    	if( loc >= 5 )
        {
            cerr << "object of type Vec is full...\n";
        }
        else
        {
            cout << "push_back( " << i << " )\t at location "
                 << loc << '\n';
            array[ loc ] = i;
            ++loc;              // advance to the next empty location
        }
    }
    
    void Vec::pop_back()
    {
            --loc;
    		cout << "pop_back( ) " << array[ loc ]
                 << " at location " << loc << '\n';
            return;
    }
    
    void Vec::erase(int idx)
    {
    	
    	cout << "erase ( 0 )" << endl;
    
    
    	return;
    }
    
    void Vec::insert(int idx, int howmany)
    {
    	cout << "insert( 0, 60 )" << endl;
    	array[0] = 60;
    }

    In methods for class Vec I need erase to point to the location of the first element of the array and remove it. i tried using begin() + n int the method definition but had syntax/errors. Not sure how to tackle this. Ive used begin() + n successfully in other programs, so I'm not sure if I used it correctly here.


    Here is function main() with the calls;


    Code:
    #include    <iostream>
    #include    <iomanip>
    using namespace std;
    
    #include    "vec.h"
    
    
    ostream &operator <<( ostream &out, const Vec &v )
    {
        for( int i = 0; i < v.size( ); ++i )
        {
            out << setw( 5 ) << v[ i ];
        }
    
        return out;
    }
    
    
    int main( )
    {
        Vec v;                  // v is an object of type Vec
    
        v.push_back( 10 );      // push 5 ints into v
        v.push_back( 20 );
        v.push_back( 30 );
        v.push_back( 40 );
        v.push_back( 50 );
    
        v.push_back( 60 );      // try to push one element too many
        cout << "after initializing v:\n" << v << '\n';
    
        v.pop_back( );          // remove the last element of v
        cout << "after popping last element of v:\n" << v << '\n';
    
        v.erase( 0 );           // erase the first element of v
        cout << "after erasing first element of v:\n" << v << '\n';
    
        v.insert( 0, 60 );      // insert value 60 at index 0
        cout << "after inserting 60 at index 0 of v:\n" << v << '\n';
    
        v.erase( 1 );           // erase the second element of v
        cout << "after erasing second element of v:\n" << v << '\n';
    
                                // erase the last element of v
        v.erase( v.size( ) - 1 );
        cout << "after erasing the last element of v:\n" << v << '\n';
    
        v.erase( 0 );           // erase the first  element of v
        cout << "after erasing the 1st element of v:\n" << v << '\n';
    
        v.insert( 0, 10 );      // insert 10 at position 0
        v.insert( 1, 20 );      // insert 20 at position 1
        v.insert( 3, 40 );      // insert 40 at position 3
        cout << "after inserting three elements in v:\n" << v << '\n';
    
        v.insert( 5, 50 );      // try to insert 50 at position 5; invalid!
        cout << "after trying to insert 50 at an invalid loc in v:\n"
             << v << '\n';
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by CASHOUT
    In methods for class Vec I need erase to point to the location of the first element of the array and remove it. i tried using begin() + n int the method definition but had syntax/errors. Not sure how to tackle this. Ive used begin() + n successfully in other programs, so I'm not sure if I used it correctly here.
    You don't appear to have implemented erase to do what it should do. You should implement it the way you have in mind, then post the errors that you get. I expect that it should be "erasing" the element at the given index by shifting the following elements up by one position, then decrementing loc.

    Also, don't do this:
    Code:
    using namespace std;
    #include "vec.h"
    That is, do not place a using directive before a header inclusion, otherwise the using directive will apply to the contents of the header, potentially introducing name collisions or changing the meaning of the code. Along this note, your header should not contain any using directives except within some local scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by CASHOUT View Post
    bare with me please.
    Please, no nudity in this forum.

  4. #4
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    This can't be right

    This can't be right. Any pointers on better removing the first element??

    How can I remove another element later in the program without affect the first removal??

    How can I insert into another element later in the program without affecting the first insert?


    // methods for class Vec
    // it works with betapour2012_lab04.cpp

    Code:
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
    #include "vec.h"
    
    
    
    void Vec::push_back(int i)
    {
    	if( loc >= 5 )
        {
            cerr << "object of type Vec is full...\n";
        }
        else
        {
            cout << "push_back( " << i << " )\t at location "
                 << loc << '\n';
            array[ loc ] = i;
            ++loc;              // advance to the next empty location
        }
    }
    
    void Vec::pop_back()
    {
            --loc;
    		cout << "pop_back( ) " << array[ loc ]
                 << " at location " << loc << '\n';
            return;
    }
    
    void Vec::erase(int idx)
    {	
    	cout << "erase ( 0 )" << endl;
    	loc = 3;
    	for(int j = 0; j < loc - 1; ++j) 
    	{
    	 array[loc - 3] = array[loc - 2];
    	}
    	array [0] = 20; 
    	array [1] = 30;
    	array [2] = 40;
    return;
    }
    
    
    void Vec::insert(int idx, int howmany)
    {
    	cout << "insert( 0, 60 )" << endl;
    	/*for(int j = loc - 1; j > 0; --j) 
    	{
    	 array[j] = array[j - 1];
    	}*/	
    		
    	array[0] = 60;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Avoid using magic numbers. For example, you could write:
    Code:
    private:
        static const std::size_t max_size = 5;
        int array[max_size];
    Then use max_size elsewhere too. Also, if you intend to use a using directive for the std namespace, then do not use array as a variable name as it is the name of a standard container.

    Quote Originally Posted by CASHOUT
    This can't be right. Any pointers on better removing the first element??
    It isn't. Why aren't you making use of the idx parameter?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by laserlight View Post
    Avoid using magic numbers. For example, you could write:
    Code:
    private:
        static const std::size_t max_size = 5;
        int array[max_size];
    Then use max_size elsewhere too. Also, if you intend to use a using directive for the std namespace, then do not use array as a variable name as it is the name of a standard container.


    It isn't. Why aren't you making use of the idx parameter?

    I believe i fixed insert. Finally made sense of it. I have about 40 mins to figure out erase. lol fml

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class communication methods
    By cboard_member in forum Game Programming
    Replies: 2
    Last Post: 02-23-2006, 11:25 AM
  2. Defining class methods
    By sand_man in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2005, 05:09 AM
  3. Where are class methods in memory?
    By BrownB in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2005, 01:36 AM
  4. Exporting Class methods to a DLL
    By EvBladeRunnervE in forum Windows Programming
    Replies: 2
    Last Post: 12-09-2003, 06:29 PM
  5. Help with methods of a fraction class
    By NebulousMenace in forum C++ Programming
    Replies: 10
    Last Post: 04-01-2003, 11:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21