counting characters in object of class

Hi!
Im trying to write a function that returns the number of characters in the object..

this is the prototype of the function:
int Length()

So, i cant pass anything into the function.
I was thinking of using a while loop and a count?
Something like :
while( != NULL)
count++

then return count.

But, how do i specify the object?
Last edited on
this
but you can simply refer to its members.
this is a function in my class:
1
2
3
4
5
6
7
8
9
10
11
12
A4String::A4String(const A4String &that){   //copy constructor
    if (this != &that)
    {
        data = new char[that.capacity];
        length = that.length;
        capacity = that.capacity;
        for (int i = 0; i < length; i++)
        {
            this->data[i] = that.data[i];
        }
    }
}


before the program had a length variable in the private section of class, but now I need to take that out and use the function length()
the length in the above function refers to the variable that returns the number of characters, but now I need to substitute it with the other function in the class (int length())

So, how do i edit this function?
I'm having trouble on how to address the function inside the function and referring to the object called that
Last edited on
that.length()
but it gives me this erroe: A4String' as 'this' argument of 'int A4String::length()' discards qualifiers|
Since the that parameter in your copy constructor is const, the compiler will not let you call a non-constant member function within the copy constructor. Make the length function a const member function.

int A4String::length() const;

1
2
3
4
int A4String::length() const
{
    // code for length function
}

I am having trouble writing the length function for the program...the one from above, here is what i have so far:
1
2
3
4
5
6
7
8
9
int A4String::length() const
{
    int count = 0;
    while(data[count] != '\0')
    {
        count++;
    }
    return count;
}


A4String is the string class and this function is supposed to count the number of characters in the object .

char * data is one of the variables given in the private section of the class.

Here is the assignmment:
An A4String class should provide the ability to store a variable length character array. You will
achieve this by using a dynamically resizing char array. The A4String will be fairly restricted in
its functionality, when compared to the STL string class. But, we will be able to alter the size of
an existing array to accommodate more characters. Your A4String should start with enough
space to store 5 characters including the ‘\0’. If more space is required, you should double the
size. Rest assured that we will be adding more functionality in a later assignment.
Deliverables:
Attributes:
int capacity - the number of characters allocated in the object.
char *data - a character pointer that points to an array of characters that is
terminated with a ‘\0’.
Member Function:
A4String( )
Default constructor
~A4String( )
Destructor
A4String( const A4String & )
Copy constructor
A4String( const char * )
Convert constructor
A4String& operator = ( const A4String & )
Overloaded assignment operator
bool operator == ( const A4String & ) const
Overloaded equivalence operator
int length( ) const
Return the length of the A4String
string toString()
Returns a string representation of an A4String
Topic archived. No new replies allowed.