GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jul-2006, 20:03
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Problem with Overloaded Constructors and Class within Class


I am trying to create an instance of a class within a class (Date within CPPTicket), and trying to overload constructors with the date info. When the program runs, I am calculating the correct date (I have testing couts showing the right date and dayofyear), but when I call my GetDayOfYear() function, it is assigning the same date to both instances of my Date class. I can't figure out what I'm doing wrong. I am not getting any errors, but the program is not working as anticipated. The code is below:

//date.h
CPP / C++ / C Code:
//Jerry Brinegar - BringerQ6
//Date.h

#ifndef _DATE_H
#define _DATE_H

#include <string>

using namespace std;

class Date
{
private:
	int month, day, year;
	string description;
	int DayOfYear;
	void CalculateDayOfYear();


public:
	Date();									//constructor object with system date 7/5/2006
	Date(int m, int d, int y, string desc);	//constructor for input values
	void SetDate(int m, int d, int y, string dsc);	
                //sets Date object to input values

	string GetFormattedDate();	
               //returns the date and year description in formatted string

	int GetDayOfYear();	//returns day of year 1-365
	int GetYear() {return year;}
	int GetMonth() {return month;}
	int GetDay() {return day;}
};


#endif

//cppticket.h
CPP / C++ / C Code:
//Jerry Brinegar - BrinegarQ6
//CPPTicket.h


#ifndef _CPPTICKET_H
#define _CPPTICKET_H

#include <string>
#include "Date.h"

using namespace std;

class CPPTicket
{
private:
	string reservation;
	int numberOfPass;
	Date book, travel;
	float fare;
	void CalculateFare();

public:
	CPPTicket();
	CPPTicket(string res, int n, Date bk, Date tr);
	void SetNumberOfPass(int n);
	void SetDates(Date bk, Date tr);
	void SetReservationName(string n);
	float GetFare();
};


#endif

//date.cpp
CPP / C++ / C Code:
//Jerry Brinegar - BrinegarQ6
//Date.cpp


#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include "Date.h"
#include "CPPTicket.h"

using namespace std;


Date::Date()
{
	month=7;
	day=5;
	year=2006;
	CalculateDayOfYear();
	cout << DayOfYear;
	
}


Date::Date(int m, int d, int y, string dsc)
{

	month=m;
	day=d;
	year=y;
	description=dsc;

	CalculateDayOfYear();

	
	
	cout << m << d << y << endl;
	cout << DayOfYear;




}

void Date::SetDate(int m, int d, int y, string dsc)
{

	month=m;
	day=d;
	year=y;
	description=dsc;
	//CalculateDayOfYear();

}

void Date::CalculateDayOfYear()
{
	switch (month)
	{
	case 2:
		DayOfYear=day+31;
		break;
	case 3:
		DayOfYear=day+59;
		break;
	case 4:
		DayOfYear=day+90;
		break;
	case 5:
		DayOfYear=day+120;
		break;
	case 6:
		DayOfYear=day+151;
		break;
	case 7:
		DayOfYear=day+181;
		break;
	case 8:
		DayOfYear=day+212;
		break;
	case 9:
		DayOfYear=day+243;
		break;
	case 10:
		DayOfYear=day+273;
		break;
	case 11:
		DayOfYear=day+304;
		break;
	case 12:
		DayOfYear=day+334;
		break;
	default:
		DayOfYear=day;
		break;
	}

	//cout << DayOfYear;

}


int Date::GetDayOfYear()
{
	return DayOfYear;

}

string Date::GetFormattedDate()
{
	stringstream bdaydesc;
	string datedesc;
	datedesc=description;

	switch (month)
	{
	case 1:
		datedesc=datedesc + "January";
		break;
	case 2:
		datedesc=datedesc + "February";
		break;
	case 3:
		datedesc=datedesc + "March";
		break;
	case 4:
		datedesc=datedesc + "April";
		break;
	case 5:
		datedesc=datedesc + "May";
		break;
	case 6:
		datedesc=datedesc + "June";
		break;
	case 7:
		datedesc=datedesc + "July";
		break;
	case 8:
		datedesc=datedesc + "August";
		break;
	case 9:
		datedesc=datedesc + "September";
		break;
	case 10:
		datedesc=datedesc + "October";
		break;
	case 11:
		datedesc=datedesc + "November";
		break;
	case 12:
		datedesc=datedesc + "December";
		break;
	default:
		datedesc=datedesc;
		break;
	}

	bdaydesc << datedesc << " " << day << ", " << year;

	return bdaydesc.str();
}

//cppticket.cpp
CPP / C++ / C Code:
//Jerry Brinegar - BrinegarQ6
//CPPTicket.cpp


#include <iostream>
#include <string>
#include "Date.h"
#include "CPPTicket.h"

using namespace std;



CPPTicket::CPPTicket()

{
	numberOfPass=0;
	fare=0;
	reservation="Default Reservation";
	


}

CPPTicket::CPPTicket(string res, int n, Date bk, Date tr)
{
	reservation=res;
	numberOfPass=n;
	book=bk;
	travel=tr;


}



void CPPTicket::SetNumberOfPass(int n)
{
	cout << "The  number of passengers is: " << n << endl;


}

void CPPTicket::SetDates(Date bk, Date tr)
{
	bk.GetDay();
	bk.GetMonth();
	bk.GetYear();
	tr.GetDay();
	tr.GetMonth();
	tr.GetYear();

	
}

void CPPTicket::SetReservationName(string n)
{
	cout << n << " Reservation" << endl;
	cout << "-----------------" << endl;

}

void CPPTicket::CalculateFare()
{

	int bday=book.GetDayOfYear();
	int tday=travel.GetDayOfYear();

             //this is where I'm getting the same DayOfYear = 186

	cout << bday;
	cout << tday;

	
	int diff=tday-bday;
	//cout << diff;

	if (diff>30)
	{
		fare=numberOfPass*39;
	}
	else if (diff< 30 || diff>=14)
	{
		fare=numberOfPass*79;
	}
	else if (diff<14)
	{
		fare=numberOfPass*129;
	}
	cout << numberOfPass;

	cout << "The total fare is:  $" << fare << endl << endl << endl;
}


float CPPTicket::GetFare()
{
	CalculateFare();
	return fare;

}


//driver.cpp
CPP / C++ / C Code:
//Jerry Brinegar - BrinegarQ6
//Driver.cpp


#include <iostream>
#include <string>
#include "Date.h"
#include "CPPTicket.h"

using namespace std;

int main()
{
	float MyFare;
	CPPTicket MyTrip;

	Date bk;
	Date tr(8,15,2006, "Baca Party");

	MyTrip.SetReservationName("Baca");
	MyTrip.SetNumberOfPass(4);
	MyTrip.SetDates(bk,tr);

	cout.setf(ios::fixed);
	cout.precision(2);

	MyFare=MyTrip.GetFare();

	cout << "The total fare is: $" << MyFare << endl;

	//Date tr(7,7,2006, "Smith Party");

	MyTrip.SetReservationName("Smith");
	MyTrip.SetNumberOfPass(2);
	MyTrip.SetDates(bk,tr);

	MyFare=MyTrip.GetFare();

	cout << "The total fare is: $" << MyFare << endl;

	return 0;
}


Can someone steer me in the right direction?
  #2  
Old 10-Jul-2006, 21:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,712
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem with Overloaded Constructors and Class within Class


Quote:
Originally Posted by jdbrine

Can someone steer me in the right direction?


What does this do?

CPP / C++ / C Code:
void CPPTicket::SetDates(Date bk, Date tr)
{
    bk.GetDay();
    bk.GetMonth();
    bk.GetYear();
    tr.GetDay();
    tr.GetMonth();
    tr.GetYear();
}


Shouldn't it be setting the month, day, year, and DayOfYear members of the book and travel members of the CPPTicket object?

Regards,

Dave
  #3  
Old 10-Jul-2006, 22:14
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Problem with Overloaded Constructors and Class within Class


Cool....I got it. Thanks for the steer!!
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Card class help verticalvoid C++ Forum 2 29-Jun-2006 21:51
Class constructors kdsXchris C++ Forum 2 03-Jun-2006 09:00
hashing help saiz66 C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.