Question

C++ class Time { public: Time(); Time(int hrs, int min, int sec); private: int hours; int...

C++

class Time { public: Time(); Time(int hrs, int min, int sec); private: int hours; int minutes; int seconds; };

1. Write the class definition for ExtendedTime which derives from Time via public inheritance. Add private data member time_zone which is an enum type TimeZone (enum TimeZone { PACIFIC, MOUNTAIN, CENTRAL, EASTERN } ). Add a default and alternate constructors to ExtendedTime to initialize all class (including the base class) data; use the base member initialization list. NOTE: This implies implementing default, alternate constructors for class Time as well.

2. Class Invoice contains a private data member purchase_time of type Time. Write default, alternate constructors for Invoice which initializes purchase_time; use the base member initialization list.

3. We would like to be able to print out dates for class ExtendedTime seamlessly to the console via COUT. Add an overloaded insertion stream operator << to ExtendedTime. Do the same for the Invoice class. You will need to use a friend. HW03EC – Base Init List, Overloading & Friends [+10%]

4. Instantiate ExtendedTime, Invoice objs in client code (passing data to alternate constructors). Print out class data to the console via <<.

C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Short Summary:

  • Implemented all the necessary functions as described and shown the output
  • Follow the program inline comments to understand the flow

Source Code:

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

//enum - TimeZone
enum TimeZone { PACIFIC, MOUNTAIN, CENTRAL, EASTERN };

//Declare the classes first to use it in Time class
class ExtendedTime;
class Invoice;

/***
* Base class: Time
*/
class Time {
  
private:
//private member varibales
int hours;
int minutes;
int seconds;

public:
/**
* Default constructor
* Defaults all the member instances to ZERO
*/
Time()
{
hours = 0;
minutes = 0;
seconds = 0;
}
  
/**
* Alternate constructor
* Assign the member instance values to corresponding parameter values
*/
Time(int hrs, int min, int sec)
{
hours = hrs;
minutes = min;
seconds = sec;
}
  
// friend functions for operators << for invoice and extendedtime classes
friend ostream& operator <<(ostream& ,const Invoice&);
friend ostream& operator <<(ostream& ,const ExtendedTime&);
};


/**
* ExtendedTime - derives from Time via public inheritance
*/
class ExtendedTime : public Time
{
private:
//private member varibales
//Zone - type of enum TimeZone
TimeZone zone;

public:

/**
* Default constructor
* uses the base member initialization list
* Calls parent class default constructor
* And Defaults timezone to EASTERN
*/
ExtendedTime():Time()
{
zone = TimeZone::EASTERN;
}

/**
* Alternate constructor
* uses the base member initialization list
* Calls parent class parameterized constructor
* And assigns TimeZone
*/
ExtendedTime(int hrs, int min, int sec, TimeZone tzone):Time(hrs, min, sec)
{
zone = tzone;
}

/**
* Operator << Overloading
* Formats anf prints the time and TimeZone
* hh:mm:ss TimeZone
* cout << setw (2) << setfill ('0') << minute << "\n";
*/
friend ostream &operator<<(ostream &os, const ExtendedTime &et)
{
//Find the timezone string
string strZone;
switch(et.zone)
{
case 0:
strZone = "PST";
break;
case 1:
strZone = "MST";
break;
case 2:
strZone = "CST";
break;
case 3:
strZone = "EST";
break;
}
//Format the string
os << setfill('0') << setw(2) << et.hours << ':'
<< setw(2) << et.minutes << ':' << setw(2) << et.seconds << " " << strZone;
return os;
}
};

/**
* Class: Invoice
* Contains a private data member purchase_time of type Time
*/
class Invoice
{
private:
//private member varibales
//Zone - type of enum TimeZone
Time purchase_time;

public:
  
/**
* Default constructor
* uses the base member initialization list
* Calls parent class default constructor
*/
Invoice()
{
purchase_time = Time();
}
  
/**
* Alternate constructor
* uses the base member initialization list
* Calls parent class parameterized constructor
*/
Invoice(int hrs, int min, int sec)
{
purchase_time = Time(hrs, min, sec);
}

/**
* Operator << Overloading
* Formats anf prints the time and TimeZone
* hh:mm:ss
* cout << setw (2) << setfill ('0') << minute << "\n";
*/
friend ostream& operator <<(ostream& os, const Invoice& invoice)
{
// format Invoice Time: hh:mm:ss
//Format the string
os << setfill('0') << setw(2) << invoice.purchase_time.hours << ':'
<< setw(2) << invoice.purchase_time.minutes << ':' << setw(2) << invoice.purchase_time.seconds;
return os;
}
};


int main(int argc, char const *argv[])
{
//ExtendedTime using Alternate constructor
cout << "ExtendedTime using Alternate constructor" << endl;
ExtendedTime et2(1, 2, 30, TimeZone::PACIFIC);
//print using operator overload
cout << et2 << endl;

//Invoice using Alternate constructor
cout << "\nInvoice using Alternate constructor" << endl;
Invoice invoice2(4, 56, 5);
//print using operator overload
cout << invoice2 << endl;
  
return 0;
}

Refer the following screenshots for code indentation:

Sample Run:

Feel free to rate the answer and comment your questions if you have any.

Hit the like button and appreciate our time.

Happy Studying!!

Add a comment
Know the answer?
Add Answer to:
C++ class Time { public: Time(); Time(int hrs, int min, int sec); private: int hours; int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • C++ Please! Given this definition of a Time class: class Time { private: int hour, min,...

    C++ Please! Given this definition of a Time class: class Time { private: int hour, min, sec; public: Time() { hour = 0; min = 0; sec = 0; } Time(int h, int m, int s) {      hour = h; min = m; sec = s; } int getHour() const {     return hour; } int getMin() const {     return minute; } int getSec() const {     return sec; } }; Derive a class MilTime from the Time...

  • Overloading Design a UML diagram for a TimeClock class with the following private data members: •...

    Overloading Design a UML diagram for a TimeClock class with the following private data members: • float days • float hours The class should also have the following public member methods: • Constructor with a default that sets the data values to 0 • getDays to return the private data member days • getHours to return the private data member hours • setDays to set the private data member days • setHours to set the private data member hours •...

  • Question 19 Given the following class: public class Swapper ( private int x; private String y...

    Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) {...

    public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int getHead() { return head; } public ConsCell getTail() { return tail; } public void setTail(ConsCell t) { tail = t; } } public class IntList { private ConsCell start; public IntList (ConsCell s) { start = s; } public IntList cons(int h) { return new IntList(new ConsCell(h, start)); } public int length() { int len...

  • D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime...

    D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime (int, int, int): void get Time (inte, int&, int&) const: void printTime() const: void increment seconds(): int +incrementMinutes(): int +increment Hours(): int -equalTime (const clockType.) const: bool Consider the UML class diagram shown in the accompanying figure. According to the UML class diagram, how many private members are in the class? none zero two three D Question 4 4 pts Consider the following class...

  • C++ write a class “Dog” with a private int field called months and two public member...

    C++ write a class “Dog” with a private int field called months and two public member functions setAge and GetStage. setAge takes as argument monthsToSet and sets the member field months. setAge returns void. getStage is a const member function that takes no argument and returns a string “Puppy” if the dog is less than 9 months old, “Adolescence” if more than 9 and less than 13 months, “Adulthood” if more than 13 and less than 60 months and “Senior”...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • Class BTree { private: int size; Node* root; public: // constructors BTree() : root(nullptr), siz...

    class BTree { private: int size; Node* root; public: // constructors BTree() : root(nullptr), size(0) {} // default constructor BTree(const BTree& other); // copy constructor BTree(BTree&& other); // move constructor BTree& operator=(const BTree& other); BTree& operator=(BTree&& other);    }; Implement the following function BTree& BTree::operator=(BTree&& other) { if (this != &other) { } return *this; } Can you implement the move assigment and the move constructor

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT