10 Replies - 2362 Views - Last Post: 04 April 2010 - 06:22 PM Rate Topic: -----

#1 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Inheritance and strings

Posted 04 April 2010 - 10:43 AM

Below is the coding that I am working on a for a college class that is dealing with inheritance. I'm having trouble trying to understand how to change my dates into strings. I know I don't have the main program written yet. All I'm looking for right now is some help doing the string conversions. At the bottom for anyone that wants to know is a a screenshot of how the program should look when it is finished. Any help or advice would be appreciated.



#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

class DateClass {
  private:
       int day;
       int month;
       int year;
	   static int objectCount;
  public:
       int setDate(int theDay, int theMonth, int theYear);
       void getDate(int &theDay, int &theMonth, int &theYear);
	   void displayDate(void);
       //constructors
	   DateClass(); 
       DateClass(int theDay, int theMonth, int theYear);
       DateClass(const DateClass &dateObject);
	   //destructor
       ~DateClass();   
}; 
int DateClass::objectCount = 0;

class FancyDateClass: public DateClass {
public:
	string toString(void);
	string getMonth(void);
	string getDayOfWeek(void);
	int julianDate(void);
	bool isLeapYear();
	int subtract(FancyDateClass &aDateObj);
	FancyDateClass(int theDay, int theMonth, int theYear);
};


int main (void){

}

/*************************************************************
FancyDateClass function implemetation
*************************************************************/

string FancyDateClass::toString(void){
	std::stringstream ss;
	int month = 10;
	string str;
	ss << month;
	ss >> str;
	return month;
}

string FancyDateClass::getDayOfWeek(void){
	// return day of the week based on the date 		
	int day = julianDate() % 7;	
	std::stringstream ss;
	string str;
	ss << day;
	ss >> str;
	return day;
	//Mon=0, Tues=1, Wed=2, Thurs=3, Fri=4, Sat=5, Sun=6		
} 

int FancyDateClass::julianDate(void){
	//Using the algorithm by Fliegel & Van Flandern to find the julian date
	int theJulianDate = ( 1461 * ( year + 4800 + ( month - 14 ) / 12 ) ) / 4 + ( 367 * ( month - 2 - 12 * ( ( month - 14 ) / 12 ) ) ) / 12 - ( 3 * ( ( year + 4900 + ( month - 14 ) / 12 ) / 100 ) ) / 4 + day - 32075;
	return theJulianDate;
}

bool FancyDateClass::isLeapYear(){
	bool leapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	return leapYear;	
}


/*************************************************************
Date Class function implementation
***************************************************************/

DateClass::DateClass(){
	//default constructor
	day = 0;
	month = 0;
	year = 0;
	objectCount++;
}

DateClass::DateClass(int theDay, int theMonth, int theYear){
     //parameterized constructor
	 setDate(theDay, theMonth, theYear); 
     objectCount++;
}

DateClass::DateClass(const DateClass &dateObject) {
	//copy constructor
	day = dateObject.day;
	month = dateObject.month;
	year = dateObject.year;
	objectCount++;
}

DateClass::~DateClass() {
	//destructor
	objectCount--;
	cout << "Number of DateClass objects instantiated " << objectCount << endl; 
}


int DateClass::setDate(int theDay, int theMonth, int theYear){
	//gives the member variables a value
	//returns an error code if invalid date is entered
	//Error codes: 0 -valid date, 3 - bad year, 2 - bad month, 1 bad day
	
	//check each arguement to ensure it is valid
	int dateCode = 0;

	//check the year
	if(theYear < 1)
		dateCode = 3;
	
	//check the month
	if(theMonth < 1 || theMonth > 12)
		dateCode = 2;

	//check the day
    int maxDays = 31;
	switch (theMonth) {
		case 2:
			maxDays = 28;
		    break;
		case 4:
		case 6:
		case 9:
		case 11:
			maxDays = 30;
		    break;
		break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			maxDays = 31;
			break;
	}
	if (theDay < 1 || theDay > maxDays)
		dateCode = 1;
    
	//set the values right or wrong...
	month = theMonth;
    day = theDay;
	year = theYear;
	
	return dateCode;
}

void DateClass::getDate(int &theDay, int &theMonth, int &theYear){
//set the value of the arguements
	theDay = day;
	theMonth = month;
	theYear = year;
}

void DateClass::displayDate(void) {
	//display the date to the screen
	cout <<"\n\n";
	cout << "day : " << day << endl
		<< "month : " << month << endl
		<< "year : " << year << endl;
}

//julian Date Notes:
//FROM Julian
//	  p = J + 68569
//   q = 4*p/146097
//   r = p - (146097*q + 3)/4
//   s = 4000*(r+1)/1461001
//   t = r - 1461*s/4 + 31
//   u = 80*t/2447
//   v = u/11
//
//Then
//     Y = 100*(q-49)+s+v
//     M = u + 2 - 12*v
//     D = t - 2447*u/80 .




Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance and strings

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Inheritance and strings

Posted 04 April 2010 - 10:47 AM

You have some basic errors. Here:
string FancyDateClass::toString(void){
        std::stringstream ss;
        int month = 10;
        string str;
        ss << month;
        ss >> str;
        return month;
}


The function return-type is std::string, but you try to return an integer ("month"). Perhaps you meant to return "str"?
Secondly:
int day;
       int month;
       int year;


these variables must be under "protected" (or "public"?) zone (not "private"), so the derived class can have access to them.

This post has been edited by sarmanu: 04 April 2010 - 10:52 AM

Was This Post Helpful? 1
  • +
  • -

#3 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Inheritance and strings

Posted 04 April 2010 - 10:54 AM

Wow. That was so simple a fix. Thank you so much. Going to try and get the rest of this going here. I will ask more questions if I get hung up again. Thank you so much again!
Was This Post Helpful? 0
  • +
  • -

#4 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Inheritance and strings

Posted 04 April 2010 - 11:56 AM

Okay, next part. I'm trying to display the month name. However whenever I run the program, is just displays a blank. I'll post the entire code below, but I am sure that it is in the following section:

string FancyDateClass::getMonth(void) {
	std::stringstream ss;
	string str;
	switch(month){
		case 1: ss << 'Jan';
				ss >> str;
			 break;
		case 2: ss << 'Feb';
				ss >> str;
			break;
		case 3: ss << 'Mar';
				ss >> str;
			break;
		case 4: ss << 'Apr';
				ss >> str;
			break;
		case 5: ss << 'May';
				ss >> str;
			break;
		case 6: ss << 'Jun';
				ss >> str;
			break;
		case 7: ss << 'Jul';
				ss >> str;
			break;
		case 8: ss << 'Aug';
				ss >> str;
			break;
		case 9: ss << 'Sep';
				ss >> str;
			break;
		case 10: ss << 'Oct';
				 ss >> str;
			 break;
		case 11: ss << 'Nov';
				 ss >> str;
			 break;
		case 12: ss << 'Dec';
				 ss >> str;
			break;
	};
	return str;
}


And here is the full code as I have it thus far:

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

class DateClass {
protected:
       int day;
       int month;
       int year;
private:
	   static int objectCount;
public:
       int setDate(int theDay, int theMonth, int theYear);
       void getDate(int &theDay, int &theMonth, int &theYear);
	   void displayDate(void);
       //constructors
	   DateClass(); 
       DateClass(int theDay, int theMonth, int theYear);
       DateClass(const DateClass &dateObject);
	   //destructor
       ~DateClass();   
}; 
int DateClass::objectCount = 0;

class FancyDateClass: public DateClass {
public:
	string toString(void);
	string getMonth(void);
	string getDayOfWeek(void);
	int julianDate(void);
	bool isLeapYear();
	int subtract(FancyDateClass &aDateObj);
	FancyDateClass(int theDay, int theMonth, int theYear);
};


int main (void){
	FancyDateClass myDateObj(24, 2, 2008);

	cout << "\n\n";
	cout << "The date is ";  
	myDateObj.displayDate();
	cout << "\n\n";
	cout << "The month for ";
	myDateObj.displayDate();
	cout << " is ";
	myDateObj.getMonth();

	return 0;
}

/*************************************************************
FancyDateClass function implemetation
*************************************************************/

string FancyDateClass::toString(void){
	std::stringstream ss;
	int month = 10;
	string str;
	ss << month;
	ss >> str;
	return str;
}

string FancyDateClass::getDayOfWeek(void){
	// return day of the week based on the date 		
	int day = julianDate() % 7;	
	std::stringstream ss;
	string str;
	ss << day;
	ss >> str;
	return str;
	//Mon=0, Tues=1, Wed=2, Thurs=3, Fri=4, Sat=5, Sun=6		
} 
string FancyDateClass::getMonth(void) {
	std::stringstream ss;
	string str;
	switch(month){
		case 1: ss << 'Jan';
				ss >> str;
			 break;
		case 2: ss << 'Feb';
				ss >> str;
			break;
		case 3: ss << 'Mar';
				ss >> str;
			break;
		case 4: ss << 'Apr';
				ss >> str;
			break;
		case 5: ss << 'May';
				ss >> str;
			break;
		case 6: ss << 'Jun';
				ss >> str;
			break;
		case 7: ss << 'Jul';
				ss >> str;
			break;
		case 8: ss << 'Aug';
				ss >> str;
			break;
		case 9: ss << 'Sep';
				ss >> str;
			break;
		case 10: ss << 'Oct';
				 ss >> str;
			 break;
		case 11: ss << 'Nov';
				 ss >> str;
			 break;
		case 12: ss << 'Dec';
				 ss >> str;
			break;
	};
	return str;
}
int FancyDateClass::julianDate(void){
	//Using the algorithm by Fliegel & Van Flandern to find the julian date
	int theJulianDate = ( 1461 * ( year + 4800 + ( month - 14 ) / 12 ) ) / 4 + ( 367 * ( month - 2 - 12 * ( ( month - 14 ) / 12 ) ) ) / 12 - ( 3 * ( ( year + 4900 + ( month - 14 ) / 12 ) / 100 ) ) / 4 + day - 32075;
	return theJulianDate;
}

bool FancyDateClass::isLeapYear(){
	bool leapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	return leapYear;	
}

FancyDateClass::FancyDateClass(int theDay, int theMonth, int theYear){
	setDate(theDay, theMonth, theYear); 
}
/*************************************************************
Date Class function implementation
***************************************************************/

DateClass::DateClass(){
	//default constructor
	day = 0;
	month = 0;
	year = 0;
	objectCount++;
}

DateClass::DateClass(int theDay, int theMonth, int theYear){
     //parameterized constructor
	 setDate(theDay, theMonth, theYear); 
     objectCount++;
}

DateClass::DateClass(const DateClass &dateObject) {
	//copy constructor
	day = dateObject.day;
	month = dateObject.month;
	year = dateObject.year;
	objectCount++;
}

DateClass::~DateClass() {
	//destructor
	objectCount--;
	cout << "\n\nNumber of DateClass objects instantiated " << objectCount << endl; 
}


int DateClass::setDate(int theDay, int theMonth, int theYear){
	//gives the member variables a value
	//returns an error code if invalid date is entered
	//Error codes: 0 -valid date, 3 - bad year, 2 - bad month, 1 bad day
	
	//check each arguement to ensure it is valid
	int dateCode = 0;

	//check the year
	if(theYear < 1)
		dateCode = 3;
	
	//check the month
	if(theMonth < 1 || theMonth > 12)
		dateCode = 2;

	//check the day
    int maxDays = 31;
	switch (theMonth) {
		case 2:
			maxDays = 28;
		    break;
		case 4:
		case 6:
		case 9:
		case 11:
			maxDays = 30;
		    break;
		break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			maxDays = 31;
			break;
	}
	if (theDay < 1 || theDay > maxDays)
		dateCode = 1;
    
	//set the values right or wrong...
	month = theMonth;
    day = theDay;
	year = theYear;
	
	return dateCode;
}

void DateClass::getDate(int &theDay, int &theMonth, int &theYear){
//set the value of the arguements
	theDay = day;
	theMonth = month;
	theYear = year;
}

void DateClass::displayDate(void) {
	//display the date to the screen
	cout << month << "/" << day << "/" << year;
	
}

//julian Date Notes:
//FROM Julian
//	  p = J + 68569
//   q = 4*p/146097
//   r = p - (146097*q + 3)/4
//   s = 4000*(r+1)/1461001
//   t = r - 1461*s/4 + 31
//   u = 80*t/2447
//   v = u/11
//
//Then
//     Y = 100*(q-49)+s+v
//     M = u + 2 - 12*v
//     D = t - 2447*u/80 .



Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Inheritance and strings

Posted 04 April 2010 - 12:04 PM

You need double quotes for array of characters. I mean, use "Jan" instead of 'Jan', etc ...
Also, by using only:
myDateObj.getMonth();


nothing will get printed, since this function returns something. Use it along with a printing function, e.g:
std::cout << myDateObj.getMonth() << std::endl;


Was This Post Helpful? 1
  • +
  • -

#6 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Inheritance and strings

Posted 04 April 2010 - 12:38 PM

So here is question that someone might be able to anwser for me. From the main code that I have post above, I have changed the following section:

bool FancyDateClass::isLeapYear(){
	bool leapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	if (leapYear = 1)
		cout << " falls in a Leap year";
	else 
		cout << " does not fall in a Leap year";
	return leapYear;
}


The formula was given to us by the instructor. The problem that I am running into is, no matter what year I put in, the program says that it is a leap year. Below is my full code thus far:

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

class DateClass {
protected:
       int day;
       int month;
       int year;
private:
	   static int objectCount;
public:
       int setDate(int theDay, int theMonth, int theYear);
       void getDate(int &theDay, int &theMonth, int &theYear);
	   void displayDate(void);
       //constructors
	   DateClass(); 
       DateClass(int theDay, int theMonth, int theYear);
       DateClass(const DateClass &dateObject);
	   //destructor
       ~DateClass();   
}; 
int DateClass::objectCount = 0;

class FancyDateClass: public DateClass {
public:
	string toString(void);
	string getMonth(void);
	string getDayOfWeek(void);
	int julianDate(void);
	bool isLeapYear();
	int subtract(FancyDateClass &aDateObj);
	FancyDateClass(int theDay, int theMonth, int theYear);
};


int main (void){
	FancyDateClass myDateObj(24, 2, 2008);

	cout << "\n\n";
	cout << "The date is ";  
	myDateObj.displayDate();
	cout << "\n\n";
	cout << "The month for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getMonth();
	cout << "\n\n";
	cout << "The day of the week for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getDayOfWeek();
	cout << "\n\n";
	cout << "This date ";
	myDateObj.displayDate();
	myDateObj.isLeapYear();

	return 0;
}

/*************************************************************
FancyDateClass function implemetation
*************************************************************/

string FancyDateClass::toString(void){
	std::stringstream ss;
	int month = 10;
	string str;
	ss << month;
	ss >> str;
	return str;
}

string FancyDateClass::getDayOfWeek(void){
	// return day of the week based on the date 		
	int day = julianDate() % 7;	
	std::stringstream ss;
	string str;
	switch(day){
		case 0: ss << "Monday";
				ss >> str;
			break;
		case 1: ss << "Tuesday";
				ss >> str;
			break;
		case 2: ss << "Wednesday";
				ss >> str;
			break;
		case 3: ss << "Thursday";
				ss >> str;
			break;
		case 4: ss << "Friday";
				ss >> str;
			break;
		case 5: ss << "Saturday";
				ss >> str;
			break;
		case 6: ss << "Sunday";
				ss >> str;
			break;
	}
	return str;
	//Mon=0, Tues=1, Wed=2, Thurs=3, Fri=4, Sat=5, Sun=6		
} 
string FancyDateClass::getMonth(void) {
	std::stringstream ss;
	string str;
	switch(month){
		case 1: ss << "Jan";
				ss >> str;
			 break;
		case 2: ss << "Feb";
				ss >> str;
			break;
		case 3: ss << "Mar";
				ss >> str;
			break;
		case 4: ss << "Apr";
				ss >> str;
			break;
		case 5: ss << "May";
				ss >> str;
			break;
		case 6: ss << "Jun";
				ss >> str;
			break;
		case 7: ss << "Jul";
				ss >> str;
			break;
		case 8: ss << "Aug";
				ss >> str;
			break;
		case 9: ss << "Sep";
				ss >> str;
			break;
		case 10: ss << "Oct";
				 ss >> str;
			 break;
		case 11: ss << "Nov";
				 ss >> str;
			 break;
		case 12: ss << "Dec";
				 ss >> str;
			break;
	};
	return str;
}
int FancyDateClass::julianDate(void){
	//Using the algorithm by Fliegel & Van Flandern to find the julian date
	int theJulianDate = ( 1461 * ( year + 4800 + ( month - 14 ) / 12 ) ) / 4 + ( 367 * ( month - 2 - 12 * ( ( month - 14 ) / 12 ) ) ) / 12 - ( 3 * ( ( year + 4900 + ( month - 14 ) / 12 ) / 100 ) ) / 4 + day - 32075;
	return theJulianDate;
}

bool FancyDateClass::isLeapYear(){
	bool leapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	if (leapYear = 1)
		cout << " falls in a Leap year";
	else 
		cout << " does not fall in a Leap year";
	return leapYear;
}

FancyDateClass::FancyDateClass(int theDay, int theMonth, int theYear){
	setDate(theDay, theMonth, theYear); 
}
/*************************************************************
Date Class function implementation
***************************************************************/

DateClass::DateClass(){
	//default constructor
	day = 0;
	month = 0;
	year = 0;
	objectCount++;
}

DateClass::DateClass(int theDay, int theMonth, int theYear){
     //parameterized constructor
	 setDate(theDay, theMonth, theYear); 
     objectCount++;
}

DateClass::DateClass(const DateClass &dateObject) {
	//copy constructor
	day = dateObject.day;
	month = dateObject.month;
	year = dateObject.year;
	objectCount++;
}

DateClass::~DateClass() {
	//destructor
	objectCount--;
	cout << "\n\nNumber of DateClass objects instantiated " << objectCount << endl; 
}


int DateClass::setDate(int theDay, int theMonth, int theYear){
	//gives the member variables a value
	//returns an error code if invalid date is entered
	//Error codes: 0 -valid date, 3 - bad year, 2 - bad month, 1 bad day
	
	//check each arguement to ensure it is valid
	int dateCode = 0;

	//check the year
	if(theYear < 1)
		dateCode = 3;
	
	//check the month
	if(theMonth < 1 || theMonth > 12)
		dateCode = 2;

	//check the day
    int maxDays = 31;
	switch (theMonth) {
		case 2:
			maxDays = 28;
		    break;
		case 4:
		case 6:
		case 9:
		case 11:
			maxDays = 30;
		    break;
		break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			maxDays = 31;
			break;
	}
	if (theDay < 1 || theDay > maxDays)
		dateCode = 1;
    
	//set the values right or wrong...
	month = theMonth;
    day = theDay;
	year = theYear;
	
	return dateCode;
}

void DateClass::getDate(int &theDay, int &theMonth, int &theYear){
//set the value of the arguements
	theDay = day;
	theMonth = month;
	theYear = year;
}

void DateClass::displayDate(void) {
	//display the date to the screen
	cout << month << "/" << day << "/" << year;
	
}

//julian Date Notes:
//FROM Julian
//	  p = J + 68569
//   q = 4*p/146097
//   r = p - (146097*q + 3)/4
//   s = 4000*(r+1)/1461001
//   t = r - 1461*s/4 + 31
//   u = 80*t/2447
//   v = u/11
//
//Then
//     Y = 100*(q-49)+s+v
//     M = u + 2 - 12*v
//     D = t - 2447*u/80 .



Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Inheritance and strings

Posted 04 April 2010 - 12:44 PM

if (leapYear = 1)


You may want to use the comparision operator == instead of assignment operator =. Try:
if (leapYear == 1)


By the way, if you use "bool" data type, why don't you compare with "true"/"false"? Like:
if (leapYear == true) // equivalent with if (leapYear)


Was This Post Helpful? 1
  • +
  • -

#8 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Inheritance and strings

Posted 04 April 2010 - 12:47 PM

View Postsarmanu, on 04 April 2010 - 11:44 AM, said:

if (leapYear == 1)


By the way, if you use "bool" data type, why don't you compare with "true"/"false"? Like:
if (leapYear == true) // equivalent with if (leapYear)


These would be it. I always forget to use ==. Thank you so much again. Only a few more parts left.

This post has been edited by Zango: 04 April 2010 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#9 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Inheritance and strings

Posted 04 April 2010 - 01:25 PM

Okay, last part of this program is a subtract member.
int subtract(FancyDateClass &aDateObj)

This is where I am at. Here are the instructions for it:
This function returns the difference in days between two fancyDateClass objects. To calculate the difference in days, subtract the Julian date of the object passed to the function from the Julian date of the object 'owning' the function.
According to the screen shot above the second date should be 2/25/2008. I'm boggled as how to write this so here is what I have:
int FancyDateClass::subtract(FancyDateClass &aDateObj){
	day = (FancyDateClass::julianDate() - aDateObj.julianDate());
	return day;

Here is the full coding with the main program mostly written, just stuck at this part, just a little guidance in the right direction would be helpful.
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

class DateClass {
protected:
       int day;
       int month;
       int year;
private:
	   static int objectCount;
public:
       int setDate(int theDay, int theMonth, int theYear);
       void getDate(int &theDay, int &theMonth, int &theYear);
	   void displayDate(void);
       //constructors
	   DateClass(); 
       DateClass(int theDay, int theMonth, int theYear);
       DateClass(const DateClass &dateObject);
	   //destructor
       ~DateClass();   
}; 
int DateClass::objectCount = 0;

class FancyDateClass: public DateClass {
public:
	string toString(void);
	string getMonth(void);
	string getDayOfWeek(void);
	int julianDate(void);
	bool isLeapYear();
	int subtract(FancyDateClass &aDateObj);
	FancyDateClass(int theDay, int theMonth, int theYear);
};


int main (void){
	FancyDateClass myDateObj(24, 2, 2008);

	
	cout << "\n\n";
	cout << "The date is ";  
	myDateObj.displayDate();
	cout << "\n\n";
	cout << "The month for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getMonth();
	cout << "\n\n";
	cout << "The day of the week for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getDayOfWeek();
	cout << "\n\n";
	cout << "This date ";
	myDateObj.displayDate();
	myDateObj.isLeapYear();
	cout << "\n\n";
	myDateObj.subtract();
	
	



	return 0;
}

/*************************************************************
FancyDateClass function implemetation
*************************************************************/

string FancyDateClass::toString(void){
	std::stringstream ss;
	int month = 10;
	string str;
	ss << month;
	ss >> str;
	return str;
}

string FancyDateClass::getDayOfWeek(void){
	// return day of the week based on the date 		
	int day = julianDate() % 7;	
	std::stringstream ss;
	string str;
	switch(day){
		case 0: ss << "Monday";
				ss >> str;
			break;
		case 1: ss << "Tuesday";
				ss >> str;
			break;
		case 2: ss << "Wednesday";
				ss >> str;
			break;
		case 3: ss << "Thursday";
				ss >> str;
			break;
		case 4: ss << "Friday";
				ss >> str;
			break;
		case 5: ss << "Saturday";
				ss >> str;
			break;
		case 6: ss << "Sunday";
				ss >> str;
			break;
	}
	return str;
	//Mon=0, Tues=1, Wed=2, Thurs=3, Fri=4, Sat=5, Sun=6		
} 
string FancyDateClass::getMonth(void) {
	std::stringstream ss;
	string str;
	switch(month){
		case 1: ss << "Jan";
				ss >> str;
			 break;
		case 2: ss << "Feb";
				ss >> str;
			break;
		case 3: ss << "Mar";
				ss >> str;
			break;
		case 4: ss << "Apr";
				ss >> str;
			break;
		case 5: ss << "May";
				ss >> str;
			break;
		case 6: ss << "Jun";
				ss >> str;
			break;
		case 7: ss << "Jul";
				ss >> str;
			break;
		case 8: ss << "Aug";
				ss >> str;
			break;
		case 9: ss << "Sep";
				ss >> str;
			break;
		case 10: ss << "Oct";
				 ss >> str;
			 break;
		case 11: ss << "Nov";
				 ss >> str;
			 break;
		case 12: ss << "Dec";
				 ss >> str;
			break;
	};
	return str;
}
int FancyDateClass::julianDate(void){
	//Using the algorithm by Fliegel & Van Flandern to find the julian date
	int theJulianDate = ( 1461 * ( year + 4800 + ( month - 14 ) / 12 ) ) / 4 + ( 367 * ( month - 2 - 12 * ( ( month - 14 ) / 12 ) ) ) / 12 - ( 3 * ( ( year + 4900 + ( month - 14 ) / 12 ) / 100 ) ) / 4 + day - 32075;
	return theJulianDate;
}

bool FancyDateClass::isLeapYear(){
	bool leapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	if (leapYear == true)
		cout << " falls in a Leap year";
	else 
		cout << " does not fall in a Leap year";
	return leapYear;
}

FancyDateClass::FancyDateClass(int theDay, int theMonth, int theYear){
	setDate(theDay, theMonth, theYear); 
}

int FancyDateClass::subtract(FancyDateClass &aDateObj){
	day = (FancyDateClass::julianDate() - aDateObj.julianDate());
	return day;
}
/*************************************************************
Date Class function implementation
***************************************************************/

DateClass::DateClass(){
	//default constructor
	day = 0;
	month = 0;
	year = 0;
	objectCount++;
}

DateClass::DateClass(int theDay, int theMonth, int theYear){
     //parameterized constructor
	 setDate(theDay, theMonth, theYear); 
     objectCount++;
}

DateClass::DateClass(const DateClass &dateObject) {
	//copy constructor
	day = dateObject.day;
	month = dateObject.month;
	year = dateObject.year;
	objectCount++;
}

DateClass::~DateClass() {
	//destructor
	objectCount--;
	cout << "Number of DateClass objects instantiated " << objectCount << endl; 
}


int DateClass::setDate(int theDay, int theMonth, int theYear){
	//gives the member variables a value
	//returns an error code if invalid date is entered
	//Error codes: 0 -valid date, 3 - bad year, 2 - bad month, 1 bad day
	
	//check each arguement to ensure it is valid
	int dateCode = 0;

	//check the year
	if(theYear < 1)
		dateCode = 3;
	
	//check the month
	if(theMonth < 1 || theMonth > 12)
		dateCode = 2;

	//check the day
    int maxDays = 31;
	switch (theMonth) {
		case 2:
			maxDays = 28;
		    break;
		case 4:
		case 6:
		case 9:
		case 11:
			maxDays = 30;
		    break;
		break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			maxDays = 31;
			break;
	}
	if (theDay < 1 || theDay > maxDays)
		dateCode = 1;
    
	//set the values right or wrong...
	month = theMonth;
    day = theDay;
	year = theYear;
	
	return dateCode;
}

void DateClass::getDate(int &theDay, int &theMonth, int &theYear){
//set the value of the arguements
	theDay = day;
	theMonth = month;
	theYear = year;
}

void DateClass::displayDate(void) {
	//display the date to the screen
	cout << month << "/" << day << "/" << year;
	
}

//julian Date Notes:
//FROM Julian
//	  p = J + 68569
//   q = 4*p/146097
//   r = p - (146097*q + 3)/4
//   s = 4000*(r+1)/1461001
//   t = r - 1461*s/4 + 31
//   u = 80*t/2447
//   v = u/11
//
//Then
//     Y = 100*(q-49)+s+v
//     M = u + 2 - 12*v
//     D = t - 2447*u/80 .



This post has been edited by Zango: 04 April 2010 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 Zango  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-April 10

Re: Inheritance and strings

Posted 04 April 2010 - 04:01 PM

So here is my coding thus far:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

class DateClass {
protected:
       int day;
       int month;
       int year;
private:
	   static int objectCount;
public:
       int setDate(int theDay, int theMonth, int theYear);
       void getDate(int &theDay, int &theMonth, int &theYear);
	   void displayDate(void);
       //constructors
	   DateClass(); 
       DateClass(int theDay, int theMonth, int theYear);
       DateClass(const DateClass &dateObject);
	   //destructor
       ~DateClass();   
}; 
int DateClass::objectCount = 0;

class FancyDateClass: public DateClass {
public:
	string toString(void);
	string getMonth(void);
	string getDayOfWeek(void);
	int julianDate(void);
	bool isLeapYear();
	int subtract(FancyDateClass &aDateObj);
	FancyDateClass(int theDay, int theMonth, int theYear);
};


int main (void){
	FancyDateClass myDateObj(24, 2, 2008);

	
	cout << "\n\n";
	cout << "The date is ";  
	myDateObj.displayDate();
	cout << "\n\n";
	cout << "The month for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getMonth();
	cout << "\n\n";
	cout << "The day of the week for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getDayOfWeek();
	cout << "\n\n";
	cout << "This date ";
	myDateObj.displayDate();
	myDateObj.isLeapYear();
	cout << "\n\n";
	cout << myDateObj.subtract();
	cout << "\n\nClosing down the program\n";



	return 0;
}

/*************************************************************
FancyDateClass function implemetation
*************************************************************/

string FancyDateClass::toString(void){
	std::stringstream ss;
	int month = 10;
	string str;
	ss << month;
	ss >> str;
	return str;
}

string FancyDateClass::getDayOfWeek(void){
	// return day of the week based on the date 		
	int day = julianDate() % 7;	
	std::stringstream ss;
	string str;
	switch(day){
		case 0: ss << "Monday";
				ss >> str;
			break;
		case 1: ss << "Tuesday";
				ss >> str;
			break;
		case 2: ss << "Wednesday";
				ss >> str;
			break;
		case 3: ss << "Thursday";
				ss >> str;
			break;
		case 4: ss << "Friday";
				ss >> str;
			break;
		case 5: ss << "Saturday";
				ss >> str;
			break;
		case 6: ss << "Sunday";
				ss >> str;
			break;
	}
	return str;
	//Mon=0, Tues=1, Wed=2, Thurs=3, Fri=4, Sat=5, Sun=6		
} 
string FancyDateClass::getMonth(void) {
	//returns the month based on the date
	std::stringstream ss;
	string str;
	switch(month){
		case 1: ss << "Jan";
				ss >> str;
			 break;
		case 2: ss << "Feb";
				ss >> str;
			break;
		case 3: ss << "Mar";
				ss >> str;
			break;
		case 4: ss << "Apr";
				ss >> str;
			break;
		case 5: ss << "May";
				ss >> str;
			break;
		case 6: ss << "Jun";
				ss >> str;
			break;
		case 7: ss << "Jul";
				ss >> str;
			break;
		case 8: ss << "Aug";
				ss >> str;
			break;
		case 9: ss << "Sep";
				ss >> str;
			break;
		case 10: ss << "Oct";
				 ss >> str;
			 break;
		case 11: ss << "Nov";
				 ss >> str;
			 break;
		case 12: ss << "Dec";
				 ss >> str;
			break;
	};
	return str;
}
int FancyDateClass::julianDate(void){
	//Using the algorithm by Fliegel & Van Flandern to find the julian date
	int theJulianDate = ( 1461 * ( year + 4800 + ( month - 14 ) / 12 ) ) / 4 + ( 367 * ( month - 2 - 12 * ( ( month - 14 ) / 12 ) ) ) / 12 - ( 3 * ( ( year + 4900 + ( month - 14 ) / 12 ) / 100 ) ) / 4 + day - 32075;
	return theJulianDate;
}

bool FancyDateClass::isLeapYear(){
	//determining if the year is a leap year or not
	bool leapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
	if (leapYear == true)
		cout << " falls in a Leap year";
	else 
		cout << " does not fall in a Leap year";
	return leapYear;
}

FancyDateClass::FancyDateClass(int theDay, int theMonth, int theYear){
//parameterized constructor
	setDate(theDay, theMonth, theYear); 
}

int FancyDateClass::subtract(FancyDateClass &aDateObj){
	aDateObj.setDate(25, 2, 2008);
	day = (FancyDateClass::julianDate() - aDateObj.julianDate());
	return day;
}
/*************************************************************
Date Class function implementation
***************************************************************/

DateClass::DateClass(){
	//default constructor
	day = 0;
	month = 0;
	year = 0;
	objectCount++;
}

DateClass::DateClass(int theDay, int theMonth, int theYear){
     //parameterized constructor
	 setDate(theDay, theMonth, theYear); 
     objectCount++;
}

DateClass::DateClass(const DateClass &dateObject) {
	//copy constructor
	day = dateObject.day;
	month = dateObject.month;
	year = dateObject.year;
	objectCount++;
}

DateClass::~DateClass() {
	//destructor
	objectCount--;
	cout << "Number of DateClass objects instantiated " << objectCount << endl; 
}


int DateClass::setDate(int theDay, int theMonth, int theYear){
	//gives the member variables a value
	//returns an error code if invalid date is entered
	//Error codes: 0 -valid date, 3 - bad year, 2 - bad month, 1 bad day
	
	//check each arguement to ensure it is valid
	int dateCode = 0;

	//check the year
	if(theYear < 1)
		dateCode = 3;
	
	//check the month
	if(theMonth < 1 || theMonth > 12)
		dateCode = 2;

	//check the day
    int maxDays = 31;
	switch (theMonth) {
		case 2:
			maxDays = 28;
		    break;
		case 4:
		case 6:
		case 9:
		case 11:
			maxDays = 30;
		    break;
		break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			maxDays = 31;
			break;
	}
	if (theDay < 1 || theDay > maxDays)
		dateCode = 1;
    
	//set the values right or wrong...
	month = theMonth;
    day = theDay;
	year = theYear;
	
	return dateCode;
}

void DateClass::getDate(int &theDay, int &theMonth, int &theYear){
//set the value of the arguements
	theDay = day;
	theMonth = month;
	theYear = year;
}

void DateClass::displayDate(void) {
	//display the date to the screen
	cout << month << "/" << day << "/" << year;
	
}

//julian Date Notes:
//FROM Julian
//	  p = J + 68569
//   q = 4*p/146097
//   r = p - (146097*q + 3)/4
//   s = 4000*(r+1)/1461001
//   t = r - 1461*s/4 + 31
//   u = 80*t/2447
//   v = u/11
//
//Then
//     Y = 100*(q-49)+s+v
//     M = u + 2 - 12*v
//     D = t - 2447*u/80 .



Here is the error I am recieving:
1>c:\users\randy\documents\visual studio 2008\projects\cpetersonweek5lab05exercise1and2\cpetersonweek5lab05exercise1and2\inheritancecrp.cpp(62) : error C2660: 'FancyDateClass::subtract' : function does not take 0 arguments

I know I need to have something put in the following beside the subtract line in the main program:
int main (void){
	FancyDateClass myDateObj(24, 2, 2008);

	
	cout << "\n\n";
	cout << "The date is ";  
	myDateObj.displayDate();
	cout << "\n\n";
	cout << "The month for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getMonth();
	cout << "\n\n";
	cout << "The day of the week for ";
	myDateObj.displayDate();
	cout << " is ";
	cout << myDateObj.getDayOfWeek();
	cout << "\n\n";
	cout << "This date ";
	myDateObj.displayDate();
	myDateObj.isLeapYear();
	cout << "\n\n";
	cout << myDateObj.subtract();
	cout << "\n\nClosing down the program\n";



	return 0;
}


The issue is, I have no idea what I'm supposed to put in there. Regardless of what I enter it comes back with an error? Can someone explain what I'm doing wrong?
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7055
  • View blog
  • Posts: 14,734
  • Joined: 16-October 07

Re: Inheritance and strings

Posted 04 April 2010 - 06:22 PM

I'm actually a little incredulous that you could produce all this code and not actually know how to use it.

It would work with something like this:
FancyDateClass dt2(24, 5, 2008);
cout << myDateObj.subtract(dt2);
//...

int FancyDateClass::subtract(FancyDateClass &other){
	return (this->julianDate() - other.julianDate());
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1