GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-May-2006, 12:19
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Not sure I understand this assignment....


Here's the (abbreviated) code for a simple string class - I've deleted most of the member functions for this post. An assignment asks

Quote:
Modify the constructor to store the null character ('\0') in every character position of a new object of type simpleString.

What I don't get is that the constructor (as copied from the book) initializes with a length of 0, so there aren't any character positions to store into, right?

If the constructor either initialized with a length greater than 0 or accepted an argument for the length, then the assignment would make sense to me.

Am I being dense, or is this assignment bogus?

Header:
CPP / C++ / C Code:
/*
 *  simpleString.h
 *  C10.7 (String class)
 *
 */

#ifndef SIMPLESTRING_H
#define SIMPLESTRING_H

class simpleString
{
    public:
        //Member functions
        //Constructor
        simpleString();
        
                
    private:
        //Data members
        enum {capacity = 255};
        char contents[capacity];
        int length;
};
#endif

Implementation:
CPP / C++ / C Code:
/*
 *  simpleString.cpp
 *  C10.7 (String class)
 *
 */

#include "simpleString.h"
#include <iostream>
using namespace std;

//Member functions
//Constructor
simpleString::simpleString()
{
    length = 0;
}

  #2  
Old 05-May-2006, 12:31
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Not sure I understand this assignment....


Quote:
Originally Posted by earachefl
Here's the (abbreviated) code for a simple string class - I've deleted most of the member functions for this post. An assignment asks

Quote:
Modify the constructor to store the null character ('\0') in every character position of a new object of type simpleString.

What I don't get is that the constructor (as copied from the book) initializes with a length of 0, so there aren't any character positions to store into, right?

If the constructor either initialized with a length greater than 0 or accepted an argument for the length, then the assignment would make sense to me.

Am I being dense, or is this assignment bogus?
In your quote it says to "Modify the constructor". So you need to change "constructor" to "store the null character ('\0') in every character position of a new object".
  #3  
Old 05-May-2006, 12:33
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Not sure I understand this assignment....


Quote:
Originally Posted by Sokar
In your quote it says to "Modify the constructor". So you need to change "constructor" to "store the null character ('\0') in every character position of a new object".
But each new object has a length of 0, so how can you store any character anywhere?
  #4  
Old 05-May-2006, 12:40
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Not sure I understand this assignment....


Quote:
Originally Posted by earachefl
But each new object has a length of 0, so how can you store any character anywhere?
Not trying to give you a hard time but that is part of the problem the assignment wants you to fix.
  #5  
Old 05-May-2006, 12:46
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Not sure I understand this assignment....


Quote:
Originally Posted by Sokar
Not trying to give you a hard time but that is part of the problem the assignment wants you to fix.
Well, that makes sense, except the assignment doesn't state that.

I tend to get into trouble when I read more into the assignment than is explicitly stated, so am tending to be more conservative and sticking to whatever is explicitly stated.

In every other assignment so far, when they want you to modify the constructor to accept an argument, they tell you.

Anyway, thanks for your response(s).
  #6  
Old 05-May-2006, 12:59
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Not sure I understand this assignment....


Quote:
Originally Posted by earachefl
Well, that makes sense, except the assignment doesn't state that.

I tend to get into trouble when I read more into the assignment than is explicitly stated, so am tending to be more conservative and sticking to whatever is explicitly stated.

In every other assignment so far, when they want you to modify the constructor to accept an argument, they tell you.

Anyway, thanks for your response(s).
You are welcome.
  #7  
Old 05-May-2006, 16:15
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: Not sure I understand this assignment....


Quote:
Originally Posted by earachefl
But each new object has a length of 0, so how can you store any character anywhere?

Look at the variables associated with each object... the length is only an integer storing the length of the string.

The attribute holding the actual data is the character array 'contents'. This array is initialized to hold 255 characters as is stated in the enum. So, just because the length variable is set to 0 doesn't mean that the allotted space for that string is 0... it is in fact 255.

Hope that helped.
__________________
If practice makes perfect and nobody's perfect... why practice?

Homepage: http://www.treywhite.com
Blog: http://www.treywhite.com/blog.php
Web Design Company: http://www.ewebproductions.com
  #8  
Old 05-May-2006, 17:12
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Not sure I understand this assignment....


Ack!
I obviously have to much on mind to give any good help. So I think I will take a break for a while.
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Circular Linked Queue Copy Constructor and Assignment Operator Not Working? wc3promet C++ Forum 1 06-Oct-2008 04:00
private copy and assignment operator alcoholic C++ Forum 4 14-Jan-2006 02:35
assignment ..... talal2929 C++ Forum 5 16-Dec-2005 18:36
overload assignment operator with non-member function (assign class to int/dbl/str) ubergeek C++ Forum 6 06-Apr-2005 03:04
help with c++ object assignment Mjkramer21 C++ Forum 31 14-Apr-2004 19:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.