class constructor to create date and time
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: class constructor to create date and time

  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 online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,566

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,126

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center