Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2006, 9:19 PM   #1
Rawr101
Newbie
 
Join Date: Apr 2006
Posts: 2
Rep Power: 0 Rawr101 is on a distinguished road
Need help writing a member function

I am doing an assignment that requires me to write an implementation file for a header file I was given that involve sequences. There is a dynamic array that grows as needed. But there is a CAPACITY constant, which provides the initial size of the array for a sequence created by the default constructor. There is 1 constructor, 1 copy constructor, 1 destructor, 7 modification member functions, and 3 constant member functions. I need help writing 1 of the 7 modification member functions.

I've written everything except the resize(size_type ). The resize function allows the user to explicitly set the capacity of the sequence:
i.e.: resize(number);

I guess my question is do I treat size_type as the "number" in the resize(number);?

Here is the header file:
#ifndef SEQUENCE_H
#define SEQUENCE_H
#include <cstdlib>  // Provides size_t

namespace CISP430_A2
{
    class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        typedef size_t size_type;
        enum { CAPACITY = 30 };
        // CONSTRUCTOR
        sequence(size_type entry=CAPACITY );
		   // COPY CONSTRUCTOR
        sequence(const sequence& entry)   ; 
    // Library facilities used: cstdlib
        // MODIFICATION MEMBER FUNCTIONS
        void start( );
        void advance( );
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void remove_current( );
		void resize(size_type );
		void sequence::operator =(const sequence&);
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const;
        bool is_item( ) const;
        value_type current( ) const;
		//Destructor
		 ~sequence()  ;
    private:
        value_type *data;
        size_type used;
		size_type capacity;
        size_type current_index;
    };
}

#endif
Rawr101 is offline   Reply With Quote
Old Apr 10th, 2006, 10:06 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Rawr101
I've written everything except the resize(size_type ). The resize function allows the user to explicitly set the capacity of the sequence:
i.e.: resize(number);

I guess my question is do I treat size_type as the "number" in the resize(number);?
Your question isn't particularly clear. I'm guessing my response below answers it. If it doesn't, ask your question again, assuming we know nothing about what you are trying to achieve.

In your code, size_type is an alias for size_t, which is an unsigned integral type. One side effect of that is that integer values can be implicitly converted to a size_type.

So if you do
    some_sequence.resize(25);
the compiler will silently convert the value 25 into a size_type and pass it to your resize() function.

Worst case is that the compiler may give some warning (eg about passing a signed int where an unsigned value is expected) but, as long as the value you pass can be stored in a size_t the warning can be safely ignored. If you get such a warning, there are three possible fixes;

1) Tell the compiler not to issue the warning. Most compilers that issue such a warning have an option to turn it off. If the conversion is safe disabling the warning is also safe. The only time the conversion will be unsafe is if you attempt to pass a negative value.

2) Use a cast when you call the function. This is actually a portable way of telling the compiler not to complain about any necessary conversion
   some_sequence.resize(
       (CISP430_A2::sequence::size_type)25);

3) Provide a second resize() member function which takes an int argument, checks if it valid, does the conversion, and calls your resize() function;
     // within the class declaration
    void resize(int newsize)
    {
          if (newsize >= 0)   // negative values cannot be stored in a size_type
                                    //   but all positive int values can
              resize((size_type)newsize);
    }
There are some advanced cases where this will also cause ambiguity. I won't discuss those cases: if you encounter them, you should know exactly what you are doing and therefore know how you introduced the ambiguity and how to placate the compiler. If you encounter such ambiguity without knowing why, then you are typing code in without thought and should spend more time thinking about what you are doing.
grumpy is offline   Reply With Quote
Old Apr 10th, 2006, 11:36 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
I think the OP might be confused by the resize declaration missing its parameter name
		void resize(size_type );

When you come to implement it, just use a parameter name
void sequence::resize(size_type newSize)
{
}

None of the other functions have missing paramter names, so it might just be a typo by the teacher.
The Dark is online now   Reply With Quote
Old Apr 11th, 2006, 3:18 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Ah. In a header file that declares a class, the only information the compiler needs about member functions are it's name, the type(s) of any arguments, and the return type. The only time the compiler requires arguments to be named in the class declaration is if the function is implemented inline within the class declaration AND if the function uses the argument.

The following is quite legal;
// in a header file
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

#include <iostream>   // so we can use cout

class Foo
{
     public:

         void SomeMemberFunction(int stuff, double more_stuff);
            // the names stuff and more_stuff are discarded by the compiler

         void SomeOtherMemberFunction(int stuff, double)
         {
              //   the name stuff in the argument list are needed because we use it here here
              std::cout << stuff << '\n';
              // the name of the second argument is not needed as we
              // do nothing with it
         };

         void aMemberFuntionWithDefaultArguments(int = 0, double = 2.5, unsigned = 6U);
            // see ma!! no argument names
};

#endif

// in the .cpp file

void Foo::SomeMemberFunction(int x, double y)
{
   //   note that the names of arguments here are different from those
   //    in the previous class declaration  
   std::cout << x << ' ' << y << '\n';

    //   the following line, if commented out will make the compiler complain
    //     about unknown variables as the names in the header file are
    //     not visible here
    //    std::cout << stuff << ' ' << more_stuff << '\n';
}

void aMemberFunctionWithDefaultArguments(int x, double y, unsigned)
    //  note we do not use default argument values here
    //   interestingly, if we do, they will generally be ignored
{
   std::cout << x << ' ' << y << '\n';
     // note that we do nothing with the third argument, so we can
     // leave it's name out of the function implementation
}
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC