Thread: Overloading constructors?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Overloading constructors?

    I've written a Date class, implementation below. It has a default constructor and can print the date in either mm/dd/yyyy format, or as January 1, 2000, e.g. This is ok, it works. But I have to write an overloaded constructor so that besides setting the date with setDate(int, int, int) as default, it will accept the January.... format. The text we're using, Deitel's, says it can be done, but shows no examples of how. If I had a char, int, int constructor, would I then have to write new set and print functions for it? How does this work? We're also supposed to use ddd yyyy format, but I've never heard of that. Thanks.
    Code:
    // DateClass.cpp: implementation of the DateClass class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "DateClass.h"
    #include <iostream>
    
    using namespace std;
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    DateClass::DateClass(int mm, int dd, int yy)
    {
    	cout << "Constructor called\n";
    	setDate(mm, dd, yy);
    }
    
    DateClass::~DateClass()
    {
    	cout << "\nDestructor called\n";
    }
    
    void DateClass::setDate(int mn, int dy, int yr)
    {
    	setMonth(mn);
    	setDay(dy);
    	setYear(yr);
    }
    
    void DateClass::setDay(int d)
    {
    	static const int lastDay[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    	if (month == 2 && d == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
    		day = d;
    	else if (d > 0 && d <= lastDay[month])
    		day = d;
    	else
    		day = 1;
    }
    
    void DateClass::setMonth(int m)
    {
    	month = ((m > 0 && m < 13) ? m : 1);
    }
    
    void DateClass::setYear(int y)
    {
    	year = ((y >= 0 && y <= 9999) ? y : 2000);	
    }
    
    void DateClass::printNumeral()
    {
    	cout << "Date in mm/dd/yy format: \n";
    	cout << month << "/" << day << "/" << year << endl;
    }
    
    void DateClass::printStandard()
    {
    	static const char *mnthName[12] = {"January", "February", "March", "April",
    		"May", "June", "July", "August", "September", "October", "November", "December"};
    
    	cout << "Date in standard format: \n";
    	cout << mnthName[month - 1] << " " << day << ", " << year << endl;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Also, I forgot to add that the header file defaults the constructor to int = 1, int = 1, int = 2000

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could just write a another constructor

    DateClass(char*,int,int);

    move your static monthname array into the private section (it will still want to be static and you'll wont be able to initialise it there) and then use strcmp() in the body of your constructor to get and set the month number.

    I'm not sure what the ddd yyyy format is though.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Well, I couldn't get it to work that way. The array is used in two member functions, but since I couldn't initialize it under private, I would have had to initialize it twice in a cumbersome manner (at least as far as I could figure out). So I figured just as declare & initialize in the two functions. This code works, but I'm sure there's a better way. I couldn't figure it out though.
    Code:
    // DateClass.cpp: implementation of the DateClass class.
    //
    //////////////////////////////////////////////////////////////////////
    #include <cstring>
    #include "DateClass.h"
    #include <iostream>
    
    using namespace std;
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    // Default constructor for DateClass objects
    DateClass::DateClass(int mm, int dd, int yy)
    {
    	cout << "Default constructor called\n";
    	setDate(mm, dd, yy);
    }
    
    DateClass::~DateClass()
    {
    	cout << "\nDestructor called\n";
    }
    
    void DateClass::setDate(int mn, int dy, int yr)		// set date
    {
    	setMonth(mn);
    	setDay(dy);
    	setYear(yr);
    }
    
    void DateClass::setDay(int d)	// check for valid month/day & leap year and set day
    {
    	static const int lastDay[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    	if (month == 2 && d == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
    		day = d;
    	else if (d > 0 && d <= lastDay[month])
    		day = d;
    	else
    		day = 1;
    }
    
    void DateClass::setMonth(int m)	// check month for validity and set
    {
    	month = ((m > 0 && m < 13) ? m : 1);
    }
    
    void DateClass::setYear(int y)	// check year for validity and set
    {
    	year = ((y >= 0 && y <= 9999) ? y : 2000);	
    }
    
    void DateClass::printNumeral()	// print in mm/dd/yy format
    {
    	cout << "Date in mm/dd/yy format: \n";
    	cout << month << "/" << day << "/" << year << endl;
    }
    
    void DateClass::printStandard()	// print in month day, year format
    {
    	// array of month names
    	static char* mnthName[12] = {"January", "February", "March", "April","May", 
    		"June", "July", "August", "September", "October", "November", "December"};
    
    	cout << "Date in standard format: \n";
    	cout << mnthName[month - 1] << " " << day << ", " << year << endl;
    }
    
    DateClass::DateClass(char *mo, int da, int ye)
    {
    	cout << "Non-default constructor called\n";
    
    	int i = 0, m = 0;	// counter and month variables
    
    	// array of month names
    	static char* mnthName[12] = {"January", "February", "March", "April","May", 
    		"June", "July", "August", "September", "October", "November", "December"};
    	
    	// convert character month name to integer for setDate function
    	do
    	{
    		if (strcmp(mo, mnthName[i]) == 0)
    			m = i + 1;
    		i++;
    	}while (m == 0);
    
    	setDate(m, da, ye);
    }

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Smile Nice smileys :)

    Nice smileys
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Re: Nice smileys :)

    Originally posted by Magos
    Nice smileys
    Yeah, some format thing in the forum I haven't looked up yet.

  7. #7
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You initialise static class members like this -

    Code:
    class stat
    {
    	static int f;
    };
    
    int stat::f=10;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. overloading constructors
    By chasingxsuns in forum C++ Programming
    Replies: 2
    Last Post: 05-29-2006, 11:48 AM
  3. classes and overloading constructors
    By barneygumble742 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2005, 05:32 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM