9 Replies - 746 Views - Last Post: 09 October 2012 - 03:28 PM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 08:39 AM

1 concern & a problem occur with the following program, which convert military time to standard time (e.g. 0640 -> 6:40:00 AM)

In line 20 of MilTime, it tells me the arguments in the following, : Time(h, m, s)
that h, m, s are undefined. I did a similar assignment and no such problems occurred.
One can see this in the "full code" text box in the 1st post of the following: http://www.dreaminco...1&#entry1654950

Another problem stems from Line 49 of MilTime class. The set precision refuses to create at least 2 digits for minute and seconds. E.g., program display 6:4:0 PM, instead of 6:04:00. I tried setw, but that incorrect though, since it only adds width.


MilTime <<Derived Class>>
#include <iostream>
#include <iomanip>
#include "Time.h" 
//#include "Date.h" 
//#include "DateTime.h" 
using namespace std; 

class MilTime : public Time
{ 
private:
	int milHours;
	int milSeconds;
public:
	MilTime() : Time()
	{
		milHours = 0; 
		milSeconds = 0;
	}

	MilTime(int mh, int ms)// :	Time(h, m, s)//,
	//DateTime(dy, mon, yr, hr, mt, se),
	//Date(dy, mon, yr)
	{
		milHours = mh;
		milSeconds = ms;
		setTime(milHours, milSeconds);
	}

	// Accepts arguments to be stored in the milHour and milSeconds variables.
	void setTime(int mh, int ms)
	{

		hour = mh / 100;		// e.g., 2459/100 = 24.59 = 24
		min = mh % 100;		// 2459/100 = 24.59 = .59 * 100 = 59
		sec = ms;
	}

	// Returns the hour in military format.
	int getHour()
	{
		return milHours;
	}

	// Returns the hour in standard format.
	void getStandHr()
	{
		cout << setprecision(2) << fixed; 
		cout << (( hour == 0 || hour == 12 ) ? 12 : (hour % 12)) <<				// 0 or 12 will output 12... 1,2,3,4...11,12,13,14,...23 
		":" << setprecision (2) << min << ":" << setprecision(2) << sec << (hour < 12 ? " AM" : " PM" );			    //outputs e.g. 11/12 = 0.96667 * 12 = 11,,,23 % 12 = 1.91667 - 1 = .91667 * 12 = 11
	}
};
#endif



Time.header <<Base Class>>
Spoiler


Driver
// This program demonstrates a class with multiple inheritance. 
#include <iostream> 
#include "DateTime.h"
#include "MilTime.h"
using namespace std; 

int main() 
{
	/*
	// Define a DateTime object and use the default constructor to initialize it. 
	DateTime emptyDay; 
	
	// Display the object's date and time. 
	cout << emptyDay.getDateTime() << endl; 

	// Define a DateTime object and initialize it with the date 2/4/60 and the time 5 32:27. 
	DateTime pastDay(2, 4, 60, 5, 32, 27); 

	DateTime secDay(2, 5, 60, 22, 31, 30);

	// Display the object'S date and time. 
	cout << pastDay.getDateTime() << endl; 

	cout << secDay.getDateTime() << endl;*/

	MilTime mt1;

	cout << "\nPassing 2359..\n";
	mt1.setTime(2359, 00);		//@ line 22 of MT class. Pass 2359 h/m, 00 for sec

	mt1.getStandHr();

	cout << endl;

	int num;
	do{
	cout << "Enter a military time to convert to standard time: ";
	cin >> num;
	} while (num < 0 || num > 2359);
	
	MilTime mt2(num, 00);
	mt2.getStandHr();

	cout << endl;


	system("pause");
	return 0; 
}

This post has been edited by mgrex: 09 October 2012 - 09:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Working with a derived class constructor syntax, and Formatting?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 08:44 AM

Please post the complete error messages exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 09:08 AM

Quote

1>c:\path\ch 15\15_04_timeformat\miltime.h(22): error C2065: 'h' : undeclared identifier
1>c:\path\ch 15\15_04_timeformat\miltime.h(22): error C2065: 'm' : undeclared identifier
1>c:\path\ch 15\15_04_timeformat\miltime.h(22): error C2065: 's' : undeclared identifier


It's due to line 20 of the programming code in the 1st post. Keep in mind the program functions fine without it though, but in future scenarios I may be required to include that syntax.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 09:30 AM

Okay so if you uncomment the following lines you have your errors?
	MilTime(int mh, int ms) :	Time(h, m, s),
	DateTime(dy, mon, yr, hr, mt, se),
	Date(dy, mon, yr)

To use this initialization list the class member variable Time, DateTime, and Date must exist in the class. Where are these variables defined in your MilTime class? The only variables I see in this class are milHours and milSeconds.

class MilTime : public Time
{ 
private:
	int milHours;
	int milSeconds;
public:
	MilTime() : Time()
	{
		milHours = 0; 
		milSeconds = 0;
	}
// This is how the initialization list works:
	MilTime(int mh, int ms) : milHours(mh), milSeconds(ms)
	{
                // The following two lines are not needed because you set the values above.
		// milHours = mh; 
		// milSeconds = ms;
                // I doubt that you need this function call.
		setTime(milHours, milSeconds);
	}
....
};


You may want to review how classes work and study this link for Initialization lists.


Jim

This post has been edited by jimblumberg: 09 October 2012 - 09:32 AM

Was This Post Helpful? 0
  • +
  • -

#5 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 02:20 PM

I intended to only use Time as the base class hence:
MilTime(int mh, int ms) : Time(h, m, s)

As the constructor syntax... Date and DateTime are meant to be ignored...

For the program in this post: http://www.dreaminco...1&#entry1654950

No such error occurred with a similar code. I'd like to know why there was an error for the MilTime derive class.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 02:32 PM

Where have you defined the variables h, m, s???

Jim
Was This Post Helpful? 1
  • +
  • -

#7 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 02:44 PM

In the Time constructor

	Time(int h, int m, int s) 
		{	hour = h; 
			min = m; 
			sec = s;		} 


Just similar to this code, Line 18 and Line 64.

#include <iostream>
#include <string>
using namespace std;

class Employee
{
public:
	string name;
	int number;
	int date;

public:
	Employee()
	{	name = " ";
		number = 0;	
		date = 0;	}

	Employee(string na, int nu, int d)
	{	name = na;
		number = nu;
		date = d;	
		//set(name, number, date);	
	}

	//copy constructor
	/*Employee(const Employee &em)
	{   name = em.name;
		number = em.number;
		date = em.date;		}*/

	//mutators
	void setName(string na)
	{	name = na;		}

	void setNum(int nu)
	{	number = nu;	}

	void setDate(int d)
	{	date = d;		}

	string getName() const
	{	return name;	}

	int geNumber() const
	{	return number;	}

	int getDate() const
	{	return date;	}
};


class ProductionWorker : public Employee
{
private:
	int shift;
	double rate;

public:
	ProductionWorker() : Employee()
	{	shift = 1;
		rate = 0.0;	}

	ProductionWorker(string na, int nu, int d, int s, double r)		
		:	Employee(na, nu, d) //, shift(s), rate(r) {}
	{	shift = s; 
		rate = r;	}

	ProductionWorker (const Employee &em, int s, double r) : Employee (em)
	{
		shift = s;
		rate = r;
	}


	void setShift(int s)
	{	shift = s;	}

	void setRate(int r)
	{	rate = r;	}

	string getShift() const
	{	string shiftType;

		if (shift == 1)
			shiftType = "Day";
		if (shift == 2)
			shiftType = "Night";

		return shiftType;	}

	double getRate() const
	{	return rate;	}

	double payment(double h) const	//h = hoursworked
	{	return h * rate;	}
};

int main()
{
	Employee emp1("John Doe", 2, 2011);
	ProductionWorker pw1(emp1, 2, 37);		//employee, shift type, work hours
	//ProductionWorker pw1("Frank", 5, 2012, 2, 37);

	// Print out name and pay (based on 40 hours work).
	cout << "Name: " << pw1.getName() << endl;
	cout << "Pay: " << pw1.payment(40.0) << endl;		//40 hours worked.
	cout << "Shift: " << pw1.getShift() <<endl;
	system("pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 02:48 PM

Where in your milTime constructor have you defined these variables. If you want to use these variables to initialize your Time class you must first pass these variables through your milTime class constructor.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 02:55 PM

Ah, thanks for pointing out my careless mistake :).

Now the question is about formatting?

Where could I read about how to format the time output as say:

06:04:00 AM
Instead of 6:4:0 AM?

I'm hoping I don't have to use an if statement.

This post has been edited by mgrex: 09 October 2012 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Working with a derived class constructor syntax, and Formatting?

Posted 09 October 2012 - 03:28 PM

Look at the functions in the iomanip header file, things like setfill(), setw(), etc.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1