Jump to content
  • Advertisement
Sign in to follow this  
gnomer

help with overloaded constructor

This topic is 4807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Class Date {

public:
    Date( int dy, int yr );
    Date( int mn, int dy, int yr );

};

I need one constructor to take 2 ints and another to take 3 ints. Gives me an error. What do I need to do to make this work?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You need a default constructor:

Date();

Share this post


Link to post
Share on other sites
that doesnt work either. it already thinks I have multiple default constructors. I'm guessing because they both take similar arguments.

Share this post


Link to post
Share on other sites
i also have this constructor and it works if i get rid of one of the others.
Date( const char *mn, int dy = 1, int yr = 1900 );

Share this post


Link to post
Share on other sites
Check this out:


class A
{
public:
A(int a, int b);
A(int c, int d, double e=0);
};

A foo(3,4);


How would the compiler know which constructor you intended to call?

In situations like this, strongly consider using the Named Constructor Idiom.

Share this post


Link to post
Share on other sites
Ok I see why it's giving an error now. Problem is my book has not covered Named Constructor Idiom. It wants me to used overloaded constructors to initialize Date objects with the following formats:

DDD YYYY
MM/DD/YY
June 14, 1992

So is the only way to handle this to use Named Constructor Idiom? If so my book is giving bad exercises :(

Share this post


Link to post
Share on other sites
This is my class definition:

class Date {

public:
Date( int dy = 1, int yr = 1900 );
Date( int mn = 1, int dy = 1, int yr = 1900 );
Date( const char *mn, int dy = 1, int yr = 1900 );
void print() const;
void printDays() const;
void printLong() const;
~Date();

private:
int month;
int day;
int year;
// utility function to test proper day for month and year
int checkDay( int ) const;

};

Share this post


Link to post
Share on other sites
Use default parameters very sparingly. When you see a constructor like this:

Date( int mn = 1, int dy = 1, int yr = 1900 );

Read it like this:

Date( int mn, int dy, int yr );
Date( int mn, int dy );
Date( int mn );
Date( );


Think: Would you EVER specify a month but not a day or year? When would it make sense to specify a day and month and default to the year 1900? Is it worth it to allow such a practice, at the cost of potentially accidentally using the wrong constructor?

Share this post


Link to post
Share on other sites
Quote:
Think: Would you EVER specify a month but not a day or year? When would it make sense to specify a day and month and default to the year 1900? Is it worth it to allow such a practice, at the cost of potentially accidentally using the wrong constructor?


Well the exercise was to modify a class from the chapter. I just copied it as is and added functionality to print the date in the various formats and then I was supposed to make it be able to be initialized with any of those formats via overloaded constructors, which I mentioned above. While I agree it doesn't make sense I think it's purpose was to get the reader used to setting up default parameters. /shrug

That aside I still need a constructor that takes 3 ints and one that takes 2 ints, right? or can I do this another way...without using Named Constructor Idiom? I'm just trying to see if this exercise is solvable just using what was covered in the chapters so far.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!