![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 2
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
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); 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);
} |
|
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|