CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2014
    Posts
    1

    class constructor to create date and time

    Write your question here.

    im trying to create two classes: a Date class and a Time class. The Date class has to have three private data members: day, month and year. The Time class should have two private data members: hour and minute. Each class should have two member functions: a constructor and display.

    im lost and my code won't run



    class Date {

    private:
    int month, day, year;

    public:
    Date(int m, int d, int y){
    month = m;
    day = d;
    year = y;
    }


    };

    #endif


    #include<iostream>
    #include "Date.h"


    Date::

    void Date:isplay( int m, int d, int y)
    {
    month = m;
    day = 0;
    year = y;
    cout<<month<<" "<<date<<", "<<year<<endl;
    }


    #include "Date.h"
    #include "Time.h"


    int main()
    {
    Date dt(7,4,1776);
    Time tm(12,3);
    dt.display();
    tm.display();
    return 0;

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: class constructor to create date and time

    Define "won't run".
    I also cannot understand the purpose of these two classes.
    for example: what could this mean:
    Code:
    Date dt(7777 ,-3444444, -117769999); 
    Time tm(4512, -888888883);
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: class constructor to create date and time

    Before posting code, please format properly and use code tags. Go advanced, select the code and click '#'.

    Your class Date definition doesn't have a member function for display either declared or defined.

    Code:
    Date:: void Date::display( int m, int d, int y)
    {
    This is an invalid construct. If you have declared the function in the class definition then it should be
    Code:
    void Date::display(int m, int d, int y)
    As Victor notes in post #2, there is no error checking of constructor parameters to make sure they are a valid date.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured