Thread: building a class

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation building a class

    Is this the correct way to build a class for Date?

    #ifndef date_H
    #define date_H

    class date
    {
    private:

    date(); //default constructor
    date(int m , int d, int y);
    void input();// promts user to enter date
    void show();//outputs date
    bool Set (int m, int d, int y); //sets date to specified values
    int GetMonth();//returns month
    int GetDay();// returns day
    int GetYear(); //returns year

    bool SetFormat (char f);//allows user to change format setting
    void Increment();//move date foward one calendar year

    };//ends class date

    #endif

    How would I test a class? Can I have an example?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I don't think there is one 'proper' way
    Aim for an interface which is complete and minimal.
    You're in luck, I was screwing around the other night and I made this:
    Code:
    class Time
    {
    public:
    
        // Array of 3 letter abbreviations for each day name
        static const std::string days[];
        // Array of 3 letter abbreviations for each month name
        static const std::string months[];
    
    public:
        // Default constructor and destructor
        Time();
        Time(const tm* rhs) { copytmlocal(rhs); }
        ~Time() {};
        // Copy constructors for struct tm and class time
        Time(const Time& rhs)
        { m_timestruct = rhs.m_timestruct; }
        Time(const ::tm& rhs)
        { m_timestruct = rhs; }
    
        // Assignment operators for struct tm and class time
        Time& operator=(const Time& rhs)
        { m_timestruct = rhs.m_timestruct; }
        Time& operator=(const ::tm& rhs)
        { m_timestruct = rhs; }
    
    public:
    
        // Equivalent to asctime() in time.h
        std::string strtime() const;
    
        // Equivalent to members of the tm structure, only a much nicer interface
        inline int day() const { return m_timestruct.tm_wday; }
        inline int month() const { return m_timestruct.tm_mon; }
        inline int year() const { return m_timestruct.tm_year + 1900; }
        inline int minute() const { return m_timestruct.tm_min; }
        inline int hour() const { return m_timestruct.tm_hour; }
        inline int second() const { return m_timestruct.tm_sec; }
    
        // Equivalent to time(NULL)
        inline time_t ticks() const { return ::time(NULL); }
        // Creates a tm struct from count and copies it to m_timestruct 
        inline void ticks(time_t count) { copytmlocal(::localtime(&count)); }
    
        inline Time& localtime();
        inline Time& gmtime();
    
    protected:
    
        // Copies a tm structure to m_timestruct using memcpy
        inline void copytmlocal(const tm* copy)
        {
            ::memcpy(reinterpret_cast<void*>(&m_timestruct), 
                reinterpret_cast<const void*>(copy), sizeof(::tm));
        }
    
        ::tm m_timestruct;
    };
    
    const std::string Time::days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    const std::string Time::months[] = { "Jan", "Feb", "Mar", 
                                         "Apr", "May", "Jun", 
                                         "Jul", "Aug", "Sep", 
                                         "Oct", "Nov", "Dec" 
                                       };
    
    Time::Time()
    {
        time_t raw = ::time(NULL);
        ::tm* current = ::localtime(&raw);
    
        copytmlocal(current);
    }
    
    inline Time& Time::localtime() 
    { 
        time_t now = time(NULL);
        copytmlocal(::localtime(&now));
        return *this;
    }
    
    inline Time& Time::gmtime() 
    {
        time_t now = time(NULL);
        copytmlocal(::gmtime(&now));
        return *this;
    }
    
    
    std::string Time::strtime() const
    {
        std::stringstream format;
        format << days[m_timestruct.tm_wday] << ' '
               << months[m_timestruct.tm_mon]<< ' 'z
               << m_timestruct.tm_mday << ' '
               << std::setfill('0') << std::setw(2) 
               << m_timestruct.tm_hour << ':'
               << std::setfill('0') << std::setw(2) 
               << m_timestruct.tm_min << ':'
               << std::setfill('0') << std::setw(2) 
               << m_timestruct.tm_sec << ' '
               << 1900 + m_timestruct.tm_year << std::endl;
    
        return format.str();
    }
    
    std::ostream& operator<<(std::ostream& out, const Time& print)
    {
        out << print.strtime();
        return out;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    first, use code tags.
    second, your constructors should be public.
    third, functions should be set to either public, protected, or private depending on where you are planning to use them.
    fourth, you need private data members.
    fifth, class is automatically set to private, so unless you place private stuff after public or protected stuff, you do not necessarily need the keyword.

    for example:

    Code:
    class date
    {
        int x, y;//these are private by default
        public:
        //public fxns;
    }
    
    class date
    {
        public:
        //public fxns
        protected:
        //protected fxns
        private:  //private keyword needed because private data and fxns placed after public and/or protected.
    }
    do a search on the board, you will find plenty of examples of classes.

    edit: I said constructors should be public. This is true in the case if you want it being used in main. I believe that you could make it protected if you are going to use it with other classes, and not use it in main. I usually make them public, though.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation question

    One of the requirements is that all of the member data ofthe Date class be private. What does that mean? Does it meanthat I did it right by making everything private? Or should I make the constructors and input public?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    data members such as ints, doubles, chars, arrays, strings, etc should be private. they are usually only used by the class, so only the class needs to see them. constructors are not data members, they initialize the data members (usually private). functions can be made either public, protected, or private, depending on use. if only the class needs to use the functions, then they can be made private. if you want to call the functions from main, make them public. if you want to call the function from another class, and not from main, then you can make it protected.

    hope this helps some.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by alpha
    if you want to call the function from another class, and not from main, then you can make it protected.
    Not exactly. You can't call call the functions from all other classes when they are protected. You can only call it from children of that class (or friends).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM