Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; How do i make use of this constructor? Code: // Consructor Date::Date(tm _date) { SetDate(_date); } ............ // Get the ...

  1. #151
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    How do i make use of this constructor?

    Code:
    // Consructor
    Date::Date(tm _date)
    {
    	SetDate(_date);
    }
    
    ............
    
    // Get the local date and time of system
    tm Date::getCurrentTime()
    {
    	time_t rawtime;
    	struct tm* timeinfo;
    
    	time( &rawtime );
    	timeinfo = localtime ( &rawtime );
    
    	return *timeinfo;
    }
    i.e

    Code:
    #include "Date.h"
    
    Class A
    {
    private:
         Date currentDate(Date::getCurrentTime());   // Is this correct?
    };

  2. #152
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    OK, three things:
    _date should probably be date_ instead. Underscore identifiers are typically reserved for implementation.
    2) I forgot: you don't need the "struct" keyword here in C++ - all you need is "tm* timeinfo" - no more!
    3) What are you trying to do?
    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.

  3. #153
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    3) What are you trying to do?
    Set currentDate to system's date using Date Classes's constructor. All i want is to set the initial value of date as i declare the it in class A, or where ever...

  4. #154
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    Then call setDate inside the Date constructor, but what's this about?
    Code:
    Class A
    {
    private:
         Date currentDate(Date::getCurrentTime());   // Is this correct?
    };
    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.

  5. #155
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Then call setDate inside the Date constructor, but what's this about?
    Code:
    Class A
    {
    private:
         Date currentDate(Date::getCurrentTime());   // Is this correct?
    };
    have two constructors...

    Code:
    // Default Constructor
    Date::Date()
    {
    	day = 0;
    	month = 0;
    	year = 0;
    	hour = 0;
    	minute = 0;
    	memset(&local_dt, 0, sizeof local_dt);
    }
    
    // Consructor
    Date::Date(tm _date)
    {
    	SetDate(_date);
    }

  6. #156
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    And you want to do...
    Code:
    Date::Date()
    {
    SetDate( getCurrentTime() );
    }
    ?
    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.

  7. #157
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    And you want to do...
    Code:
    Date::Date()
    {
    SetDate( getCurrentTime() );
    }
    ?
    This is what i need to do

    Code:
    Class A
    {
    private:
         Date startDate, // receives current date
         endDate; // receives zeros
    };
    But if i do it your way, then both start & end have current date... Get my point for two constructors?

  8. #158
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    I think I get what you are trying to do. You are trying to initialize the members of class A, yes?
    In that case, you initialize them via the constructor:

    Code:
    A::A(): /* Initializer list */ startDate(startDate.getCurrentTime()), endDate(something)
    {
    }
    Although, better might be to make the default date constructor store the current time in itself, so the constructor for A would then be:

    Code:
    A::A(): /* Initializer list */ startDate(), endDate(something)
    {
    }
    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.

  9. #159
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    I think I get what you are trying to do. You are trying to initialize the members of class A, yes?
    In that case, you initialize them via the constructor:

    Code:
    A::A(): /* Initializer list */ startDate(startDate.getCurrentTime()), endDate(something)
    {
    }
    Although, better might be to make the default date constructor store the current time in itself, so the constructor for A would then be:

    Code:
    A::A(): /* Initializer list */ startDate(), endDate(something)
    {
    }
    Code:
    class Contract
    {
    public:
    	Contract();
    
    private:
    	Date issued, returned;
    };
    
    Contract::Contract(): issued( issued.getCurrentTime() ); // Is this correct? 
    1. What's the meaning of ':' after the constructor?
    2. returned has been reset by the Date constructor which takes no arguments....
    3. What's wrong with the following, as opposed to the above...?

    Code:
    Contract::Contract()
    {
     issued( issued.getCurrentTime() );
    }

  10. #160
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you probably want to make "currentTime" a static function, so that you can call it without having a valid Date object.

    Then you could do:
    Code:
    Contract::Contract(): issued( Date::getCurrentTime() )
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #161
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    1) The ":" symbolizes the beginning of the initializer list. Everything in the initializer list calls the constructor of what you put there. There it is mandatory, for example, if you have a base class with a constructor that takes one argument. You'll have to use the initializer list to call the correct constructor with the correct argument.

    2) That's why I said probably make the constructor get the default time.

    3) There are two issues:
    3a) It's invalid syntax - you can't call a constructor after the object is constructed. You can only assign. Again, this leads to unnecessary waste of resources.
    3b) It's typically undefined behavior to call a member function of a class before it's constructed, so it's very bad.

    4) Since getCurrentTime does not rely on the object it's a member of (it only gets the current system time and not the current time embedded in the object), it's better to make it static as mats suggests. That way you can call it without having to worry about constructing the object first.
    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.

  12. #162
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    2) That's why I said probably make the constructor get the default time.
    Elysia, i would also like to initialize a time object with zeros, not current time...

    4) Since getCurrentTime does not rely on the object it's a member of (it only gets the current system time and not the current time embedded in the object), it's better to make it static as mats suggests. That way you can call it without having to worry about constructing the object first.
    I don't know if i understand you, where did mats say this [EDIT: Sorry i missed his post]? anyway, here's my date class with now 'static' keyword...

    Code:
    class Date						
    {
    public:
    	Date();
    	Date(tm dt);
    	void SetDate(tm dt);
    	static tm getCurrentTime();
    	int getDay();
    	int getMonth();
    	int getYear();
    	int getHour();
    	int getMinute();
    	tm getLocalDT();
    private:
    	int day;
    	int month;
    	int year;
    	int hour;
    	int minute;
    	tm local_dt;
    };
    
    // Get the local date and time of system
    static tm Date::getCurrentTime()
    {
    	time_t rawtime;
    	tm* timeinfo;
    
    	time( &rawtime );
    	timeinfo = localtime ( &rawtime );
    
    	return *timeinfo;
    }
    What difference that this 'static' function make?
    Last edited by csonx_p; 07-18-2008 at 02:37 AM.

  13. #163
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    Quote Originally Posted by csonx_p View Post
    Elysia, i would also like to initialize a time object with zeros, not current time...
    But initializing them with 0s doesn't make much sense, does it? It's an invalid date.

    I don't know if i understand you, where did mats say this?
    Just above my previous reply.

    anyway, here's my date class with now 'static' keyword...
    You should put static only in front on the definition, not the implementation and it would be correct.

    What difference that this 'static' function make?
    Basically, it does so that all of the classes share only one instance of the function. The reason is that getCurrentTime is not bound to the object - your class. It does nothing of the sort - it doesn't return the time of the object, it doesn't set the time in the object.
    There it should better be static or you can make it a free function.
    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.

  14. #164
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A static function is not passed a "this" pointer of the current class object - which means that it's basically a free function, but it's described within a class. This means that you can call it without having an existing object.

    You can't call a constructor with the object that it is constructing. That's like using the car you are building to transport the bits you need to build the car - doesn't work, right? [yes, you could have a "half-finished car" - but computers don't work well with "half-done" things].

    You also can't call one constructor from another constructor, e.g.
    Code:
    class X
    {
       X() {
         X(0);   // calling X(int x) with zero - not valid. 
       };
       X(int x)
       {
         ... 
       };
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #165
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    But initializing them with 0s doesn't make much sense, does it? It's an invalid date.
    Well, by the time i initialize start time, i don't know about the end time yet. Customer can return a car beyond the requested/expected time. So i make them zeros... But i doubt if this is an issue i should be stressing about because my program won't use 'returned' date unless until the car is returned... But then yeah, i can also make start time equals returned time.. The point of this is learning C++ also. I could be writing a program which resetting to zeros is essential, by then i wouldn't like to pose this question....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

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