Like Tree3Likes
  • 3 Post By laserlight

Date class.

This is a discussion on Date class. within the C++ Programming forums, part of the General Programming Boards category; Hello programmers, I am trying to write a simple class, with a default constructor to output a date. All it ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    23

    Date class.

    Hello programmers,

    I am trying to write a simple class, with a default constructor to output a date. All it has to do, is output the date "01/01/2013" (MM/DD/YYYY format). I'm compiling, and running, and getting output. However, the output is garbage data, the same data each time. What am I doing wrong?

    Code:
    #include <iostream>
    using namespace std;
    
    class Date
    {
        private:
            int month;
            int day;
            int year;
        public:
            Date (int=01, int=01, int=2013);
    };
    
    Date::Date(int d, int m, int y)
    {
        month=m;
        day=d;
        year=y;
    
    }
    
    int main()
    {
        int d;
        int m;
        int y;
    
        cout<<d<<"/"<<m<<"/"<<y;
    
        return 0;
    }
    Thank you :3

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Forget about the date and see the following
    Let's take this example
    Code:
    int main()
    {
         int a;
         std::cout<<a<<std::endl;
         return 0;
    }
    What value has the variable a????

    Now take this example
    Code:
    int main()
    {
         int a;
         a = 5;
         std::cout<<a<<std::endl;
         return 0;
    }
    What is the value of variable a?
    Now take this example
    Code:
    int foo(int a)
    {
         a = 5;
         return a;
    }
    
    int main()
    {
         int a;
         a = foo(a);
         std::cout<<a<<std::endl;
         return 0;
    }
    what value does the variable a has?


  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    a has a value of 5...

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by mc74 View Post
    a has a value of 5...
    For which case?For example in first piece of code i wrote a has value of 5?

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    Mmm...no value in the first piece.

    Bear with me..I'm not good with programming, and I don't like it. Just gotta pass this C++ class...I try my best though :3

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    The assignment is for the default constructor to automatically insert the date "01/01/2013" if no other date in input.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Correct
    That is what is happening with you..I mean look at the main!
    Code:
    int main()
    {
        int d;
        int m;
        int y;
     
        cout<<d<<"/"<<m<<"/"<<y;
     
        return 0;
    }
    Where is the object of Date?You do not create any object here!You do not call the constructor!
    This piece of code will create an object of class Date
    Code:
    Date dateObject();
    and because you pass no arguments the default values are going to be assigned in the data members of the class(these are d,m,y).
    The next step is to print the values of the data members of this object you just created?How are you going to do that?Two ways.Create a member function of class Date (with name print i would say) which would print the data members of the class when called.Or create getters for every data member of class and when going to write cout in main call this geters in order to take the value of the geter function.

    An example of geter
    Code:
    class Date
    {
        private:
            int month;
            int day;
            int year;
        public:
            Date (int=01, int=01, int=2013);
            int getDay() { return day;}
    };
     
    using namespace std;
    
    int main() {
        Date dateObject();
        int dayVariable = dateObject.getDay(); 
        cout<<dayVariable<<endl;
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    Thanks

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    Quote Originally Posted by std10093
    This piece of code will create an object of class Date
    Code:
    Date dateObject();
    No, it declares a function named dateObject that returns a Date and takes no arguments. What you probably wanted:
    Code:
    Date dateObject;
    Quote Originally Posted by std10093
    Create a member function of class Date (with name print i would say) which would print the data members of the class when called.
    That is one option, though arguably better in this case would be to overload operator<< for std::ostream, e.g.,
    Code:
    std::ostream& operator<<(std::ostream& out, const Date& date)
    {
        return out << date.day << '/' << date.month << '/' << date.year;
    }
    This way, one can write in the main function:
    Code:
    std::cout << dateObject << std::endl;
    Quote Originally Posted by std10093
    Code:
    int getDay() { return day;}
    Since a getter function does not modify the observable/logical state of the object, it should be declared const:
    Code:
    int getDay() const { return day; }
    iMalc, manasij7479 and Elysia like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Thank you for the correction and nice advises

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Definitely overload operator<<.
    Note that we're leaving the goal of formatting the day and month values with two digits as an exercise for the original poster to research and implement.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    We are also leaving it as an exercise to fix the date format to the "correct" format DD/MM/YYYY
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Code:
            Date (int=01, int=01, int=2013);
    But the month and day are in octal Remember remember oct 31 is dec 25.

    We are also leaving it as an exercise to fix the date format to the "correct" format DD/MM/YYYY
    Implying the correct format is not the ISO format. YYYY/MM/DD.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Hence the quotes, whiteflags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date Class
    By kokoro in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2010, 02:51 PM
  2. date class
    By Asagohan in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2005, 06:13 AM
  3. Date class help!! Please
    By C++Newbie in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2001, 01:31 PM
  4. date class
    By aquaman in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2001, 10:57 AM
  5. date class
    By aquaman in forum C++ Programming
    Replies: 6
    Last Post: 09-16-2001, 08:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21