Thread: Help writing code (Can't think of a proper title)

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    20

    Help writing code (Can't think of a proper title)

    In Java, I would do this:

    Code:
    public interface Date {
    
      public static final String[] month = { "January", "February", "March", etc etc};
    
    }
    or in C#, I would do this:

    Code:
    public class Date
    {
      // you can't have an array of const strings in C#
      public readonly static string[] month = { "January", "February", "March", etc etc};
    }
    How do I achieve this in C++? Like, are there static, const, etc? I promise I'd read the FAQs first, but if only I had a bit more time

    Say this would be in a separate class called Date (Actually, I think Date is a class that already exists. I'm just using date as an example). A .h file and a .cpp file.

    Would my .h file contain all these declarations, and my .cpp file be empty with an #include "Date.h"?

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    use the string class
    Code:
    // date.h
    #include <string>
    
    class Date
    {
    public:
        static const std::string month[];
    };
    Code:
    // date.cpp
    #include "date.h"
    const std::string Date::month = {"jan", "feb", //etc..
    };
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    What is the string class for?

    How come I can declare strings without having to include the string class?

    I just include <iostream> and also use "using namespace std". Does that include strings as well?

    Oh, and should I declare the "Date" class as static as well?

    Edit: Now I'm having problems trying to retrieve those values.

    In another class, say Test.cpp, say I have an #include Date.h

    and I have something like
    cout << "The month is " << Date.month[0] ;

    This gives me an error with compilation. Saying "expected primary-expression before '.' token"

    What am I doing wrong?

    Edit: Problem solved. It's supposed to be
    cout << "The month is" << Date::month[0];
    right?
    Last edited by markcls; 03-25-2007 at 11:30 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What is the string class for?
    The string class is for representing strings.

    How come I can declare strings without having to include the string class?

    I just include <iostream> . Does that include strings as well?
    Simple answer: Yes.

    Oh, and should I declare the "Date" class as static as well?
    Declaring the class as static?

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Hahaha sorry, I don't think I have to set the class as static (I don't even know why I thought of that). Got it working already. Thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I just include <iostream> . Does that include strings as well?
    No.

    On some compilers you might be able to do some stuff with strings (because <iostream> might include some other header that declares some of string) but you should not rely on that because many compilers don't do that. So if you use the string class, #include its header <string>. In general, always #include the header that declares the stuff you need even if it seems to work without it. This will avoid hard to find errors later in your coding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code but am having problems
    By Choppers in forum C Programming
    Replies: 20
    Last Post: 06-25-2009, 06:18 PM
  2. writing code on paper method ..
    By transgalactic2 in forum C Programming
    Replies: 6
    Last Post: 04-15-2009, 04:41 PM
  3. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM