All pastes #209504 Raw Edit

Miscellany

public cpp v1 · immutable
#209504 ·published 2006-10-19 05:56 UTC
rendered paste body
#include "event.h"#include <iostream>using std::cin;using std::cout;using std::endl;#include <fstream>using std::ostream;#include <cstring>#include <string.h>event::event(void){	source = 0;          //  long	desc=0;       //  char	day = 0;             //  int |day, month, and year when the event	month = 0;           //      will happen	year = 0;}event::event(long s, const char de[],int d, int m, int y){	source = s;        //  long	size_t x = strlen(de); desc = new char[x]; strncpy(desc, de, x - 1); desc[x - 1] = '\0';	setDay(d);        //  int |day, month, and year when the event	setMonth(m);      //      will happen	setYear(y);}event::event(const event & input){	source = input.getSource();	setDay(input.getDay());	setMonth(input.getMonth());	setYear(input.getYear());    size_t x = strlen(input.desc); 	desc = new char[x]; 	strncpy(desc, input.desc, x - 1); 	desc[x - 1] = '\0';    }void event::keyBoardInput(void){	cout << "Please enter description of the event: ";	cin >> desc;}         	long event::getSource(void) const {	return source;}void event::setSource(long s) {	source = s;}void event::setDate(int d, int m, int y) {	d = getDay();	m = getMonth();	y = getYear();}void event::copyTo( event & b) const {	b.setSource(source);	b.setDate(day,month,year);	b.desc = desc;}void event::incrementDate(void) {	if(getDay() >= 30 && getMonth() >= 12)	{		setMonth(1);		setDay(1);		setYear(getYear() + 1);	}	else if(getDay() >= 30)	{		setDay(1);		setMonth(getMonth()+1);	}	else		setDay(getDay()+1);}void event::print(ostream &out, int dateFormat) const {	out << "Organizer ID:         " << source << endl;	out << "Event description:    " << desc << endl;	if(dateFormat == 1)	{ 		 out << "Date of event(M/D/Y): " << getMonth() << "/" << getDay() << "/" << getYear() << endl; 	}	if(dateFormat == 2)	{		out << "Date of event:(D/M/Y): " << getDay() << "/" << getMonth() << "/" << getYear() << endl; 	}}event::~event() {	delete(desc);}//Privatepartsvoid event::setMonth(int m){    month = m;	}void event::setDay(int d){    day = d;}void event::setYear(int y){    year = y;}int  event::getMonth(void) const {    return month;}int  event::getDay(void)   const {    return day;}int  event::getYear(void)  const {    return year;}//-------------------------event.h//   The Designer decides upon the following data and actions (i.e. Functions)//       and places the class  in the file event.h  #include <iostream>#include <fstream>#ifndef EVENT_H#define EVENT_Hclass event {	 public:		event(void);									// default constructor		event(long, const char [],int d, int m, int y); // 5 argument constructor		event(const event &);              				// copy constructor		void keyBoardInput(void);         				// Assume no blanks in the desc		long getSource(void) const;        				// return source		void setSource(long);             				// change source		void setDate(int d, int m, int y);				// set day, month, year		void copyTo( event & ) const;      				// copy invoking instance to parameter		void incrementDate(void);          				// advance the date by ONE day You can assume 30 days in each month		void print(std::ostream &, int dateFormat) const;	// print all fields														// dateFormat == 1   month/day/year														// dateFormat == 2   day/month/year		~event();					          			// destructor - indicate the address of the variable that is leaving	 private:		//  private functions - only the implementer can use these functions		void setMonth(int);		void setDay(int);		void setYear(int);		int  getMonth(void) const;		int  getDay(void)   const;		int  getYear(void)  const;		// data		long source;          //  id of the person scheduling the event		char *desc;           //  description of the event		int  day;             //  day, month, and year when the event		int  month;           //      will happen		int  year;	 };#endif//-----------------------------------------------------dhrw2.cpp (main file)// CIS 235 Revised  -  Homework 2 // Given the class definition for an event//  -- Write the member functions for the class event//  -- Write the 3 NONMEMBER functions: makeEvent, getSource, and advanceDate//  -- Write a main program to test all member functions for the class and the 3 nonmember functions: makeEvent, getSource and advanceDate#include <fstream>using std::ostream;#include <iostream>using std::endl;using std::cin;using std::cout;#include <cstring>     // Needed for string functions#include "event.h"#include <string>using std::string;/*The implementer will write all of the member functions and place these functions in a separate file "event.cpp"  The Client, i.e. User, will create the file drhw2.cpp  and prototype, write, and use the following 3 functions */event makeEvent(long, const char [], int d, int m, int y);long getSource(const event &);   event advanceDate( const event &, int numberOfDays); void main()//  The client will test the 3 function above along with all of the member functions{	event a,b;	long source;	string desc;	int d, m, y;	ostream *x = &cout; //extensible for file output	cout << "Please input your ID " << endl;	cin >> source;	cout << "Please input the Description " << endl;	cin >> desc;	cout << "Please input the day followed by the month followed by the year" << endl;	cin >> d >> m >> y;	 a = makeEvent(source, desc.c_str(), d, m, y);	cout << "Your source is " << getSource(a) << endl;	cout << "Current date: " << d << "/" << m << "/" << y << endl;	b = advanceDate(a,10);	cout << "Event with advanced date:\n";	b.print(*x,2);	event c,f;	long s1,s2;	int d1,d2,m1,m2,y1,y2;	cout << "Testing member functions.\n";	cout << "Enter source and date of event 1:\n";	cin >> s1 >> d1 >> m1 >> y1;	cout << "Enter source and date of event 2:\n";	cin >> s2 >> d2 >> m2 >> y2;	c.setSource(s1);	f.setSource(s2);	c.setDate(d1,m1,y1);	f.setDate(d2,m2,y2);	c.keyBoardInput();	f.keyBoardInput();	c.print(*x,1);	f.print(*x,1);	c.copyTo(f);	c.print(*x,2);	f.print(*x,2);	f.incrementDate();	c.print(*x,1);	f.print(*x,1);	system("Pause");	return;}//  Client places the code for makeEvent, getSource and advanceDate hereevent makeEvent(long s, const char n[], int d, int m, int y){	event a(s,n,d,m,y);	return a;}long getSource(const event & e)   // will return the source in the parameter{	long whattheboat;	whattheboat = e.getSource();	return whattheboat;}event advanceDate( const event & e, int numberOfDays){//  The following function will return an event with the same source and desc//     as the parameter event.  The day, month, and year will reflect numberOfDays//     into the future.	event whatthebert(e);	for(int count=0; count <= numberOfDays; count++){		whatthebert.incrementDate();	}	return whatthebert;}