Thread: need help with class based string

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    17

    need help with class based string

    I have an assignment where I hav eto use a string. I kinda know how to use the pointer based string, but don't really know how to use class based string, so thats the reason for this post...

    This is what I have to do....

    Create a Rabbit class. Let that class have an instance variable called eyeColor. Make eyeColor a string . In the Rabbit constructor, use an initializer list to initialize the eyeColor instance variable. Declare the constructor such that it receives a reference to a string as the eyeColor parameter. Specify a class variable (that means it will be a static variable) call population that acts as a counter of the number of Rabbit object that have been instantiated. That means that in your constructor you’ll need to increment the population variable, and in your destructor you’ll need to decrement your population variable.
    Define a displayProperties() method that displays the eyeColor of the Rabbit and the population value. In your Rabbit destructor, call the displayProperties method.

    How do I do this using a class based string?

    This is the main.cpp i was given....

    Code:
    int main()
    {
       Rabbit *rabbit1Ptr = new Rabbit(“Blue”);
       rabbitPtr->displayProperties();
    
       Rabbit *rabbit2Ptr = new Rabbit(“Green”);
       Rabbit2Ptr->displayProperties();
    
       Rabbit *rabbit3Ptr = new Rabbit(“Red”);
       Rabbit3Ptr->displayProperties();
    
       Rabbit *rabbit4Ptr = new Rabbit(“Red”);
       Rabbit4Ptr->displayProperties();
    
       Rabbit *rabbit5Ptr = new Rabbit(“Red”);
       Rabbit5Ptr->displayProperties();
    
       Rabbit *rabbit6Ptr = new Rabbit(“Red”);
       Rabbit6Ptr->displayProperties();
    
       Rabbit *rabbit6Ptr = new Rabbit(“Red”);
       Rabbit6Ptr->displayProperties();
    
       Rabbit *rabbit7Ptr = new Rabbit(“Red”);
       Rabbit7Ptr->displayProperties();
    
       Rabbit *rabbit8Ptr = new Rabbit(“Red”);
       Rabbit8Ptr->displayProperties();
    
       Rabbit *rabbit9Ptr = new Rabbit(“Red”);
       Rabbit9Ptr->displayProperties();
    
       Rabbit *rabbit10Ptr = new Rabbit(“Red”);
       Rabbit10Ptr->displayProperties();
    
       //Now let’s kill all the Rabbits! (So said Elmer Fudd.)
       delete rabbit1Ptr;
       delete rabbit2Ptr;
       delete rabbit3Ptr;
       delete rabbit4Ptr;
       delete rabbit5Ptr;
       delete rabbit6Ptr;
       delete rabbit7Ptr;
       delete rabbit8Ptr;
       delete rabbit9Ptr;
       delete rabbit10Ptr;
       
       return 0; // indicate successful termination
    } // end main

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    This is a very simple assignment, i wont help you by doing it for you, but hopefully put you towards the solution with some advice.

    Firstly break down the problem into several steps. From reading the brief i can clearly see simple steps to solving this problem.

    1. Create a Rabbit class - look up basic class creation
    2. Give the Rabbit class an attribute eyeColor - look up class attributes.
    3. Create a constructor with a reference to a string variable - look up passing by reference
    4. Give the class a static variable population - look up what a static class attribute means/does
    ...
    And so on until you complete each sentence in the brief.

    When the steps are finished put this new Rabbit class together with the main.cpp file you have and you should have completed your assignment(!) - look up any key steps/keywords in a manual/reference book on c++ or on the tutorials on this site.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Thanks. That actually helps alot.
    I will get back and let you know if I have any problems.
    By putting the rabbit class together with the main.cpp, you mean just make sure the names are the same, right?

  4. #4
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    er not sure what you mean by that, but by the time you come to the end this bit should be obvious... (hopefully!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM