Question

I need help implementing 3 function prototypes with using operator overloading in a class. I have...

I need help implementing 3 function prototypes with using operator overloading in a class. I have bolded the section I need help with since the cpp file is bigger than I expected. Any comments throughout would be extremely helpful.

Time.cpp file

#include "Time.h"

#include

#include

#include

// The class name is Time. This defines a class for keeping time in hours,

minutes, and AM/PM indicator.

// You should create 3 private member variables for this class. An integer

variable for the hours,

// an integer variable for the minutes, and a char variable for the AM/PM

indicator. The hours variable

// must use dynamic memory allocation. The hours range from 1 to 12, the

minutes range from 0 to 59,

// and the AM / PM indicator is either 'A' or 'P'.

// Create a default constructor that will initialize the time of an object

to 12:00 AM.

// Use the initializer list for at least one of the variables.

Time::Time() : minutes(0), AM_PM('A')

{

hours = new int;

*hours = 12;

}

// Create a constructor with 3 input parameters. The input parameters are

an int for the

// hours, an int for the minutes, and a char for the AM indicator. The

input paramters must be

// boundary checked. The hours range from 1 to 12. If the input is out of

range then default

// to 12. The minutes are from 0 to 59. If the minutes are out of range

then default to 0.

// The AM indicator is either 'A' or 'P'. If the AM indicator is out of

range then default to 'A'.

// You should use the setTime() public function for this constructor.

Time::Time(int h, int m, char AP)

{

hours = new int;

setTime(h, m, AP);

}

// Copy constructor implementation

// The copy constructor must use the initializer list for at least one

member variable

// The copy constructor will copy the values of all member functions from

the input

// object without creating any shallow copies

Time::Time(const Time &org) : minutes(org.minutes), AM_PM(org.AM_PM)

{

hours = new int;

*hours = *org.hours;

}

// Implement the destructor for this class

Time::~Time()

{

delete hours;

}

// Implement the getHours() public member function.

// This function has no input parameters.

// This function is a const function.

// This function returns an int as the hours of the object.

int Time::getHours() const

{

return *hours;

}

// Implement the getMinutes() public member function.

// This function has no input parameters.

// This function is a const function.

// This function returns an int as the minutes of the object.

int Time::getMinutes() const

{

return minutes;

}

// Implement the getAMPM() public member function.

// This function has no input parameters.

// This function is a const function.

// This function returns a char as the AM or PM of the object.

char Time::getAMPM() const

{

return AM_PM;

}

// Implement the set() private member function.

// This function has 3 input input parameters.

// This function returns a void.

// The input parameters are an int for the

// hours, an int for the minutes, and a char for the AM indicator. The

input paramters must be

// boundary checked. The hours range from 1 to 12. If the input is out of

range then default

// to 12. The minutes are from 0 to 59. If the minutes are out of range

then default to 0.

// The AM indicator is either 'A' or 'P'. If the AM indicator is out of

range then default to 'A'.

void Time::setTime(int h, int m, char AP)

{

if (h >= 1 && h <= 12)

{

*hours = h;

}

else

{

*hours = 12;

}

if (m >= 0 && m <= 59)

{

minutes = m;

}

else

{

minutes = 0;

}

if (AP == 'A' || AP == 'P')

{

AM_PM = AP;

}

else

{

AM_PM = 'A';

}

}

// Implement the assignment overloaded operator member function for the

Time class. Ensure no

// shallow copies are made. Check for self-assignment. After the

assignment, the lefthand side

// object should contain the same values as the righthand side object.

Ensure the return value

// is correct.

{

}

// Implement an overloaded operator function that implements the equality

(==)

// operator. This function is a member function of the Time class. The

input parameter

// will be a Time object and declared as a const reference variable. This

member function

// will be declared as a const member function. This function will compare

// two Time objects. If the hours, minutes and AM/PM are the same between

the two

// objects then return an integer 1, otherwise return an integer 0.

{

}

// Implement an overloaded operator function that implements the addition

(+)

// operator. This function is a non-member and non-friend function. This

function will have two

// input parameters. The first input parameter is a const reference

variable of type Time.

// The second input parameters is an integer value.

// The second input parameter will represent minutes to add to the Time

// object (the first input parameter). The second input parameter will

range from 0 to 59.

// If the second input parameter is out of range then default it to 0.

// This function will return by value a Time object with the appropriate

minutes added.

{

}

// Implement the friend function getTimeString().

// The getTimeString() has 2 input input parameters.

// The first input paramenter is a const reference variable of type Time.

// The second input paramenter is a reference variable of type

std::ostream

// This function returns a std::string type.

// This function will return the time of the first input paramenter object

// in string format as follows: hours:minutes AM

// There must be a leading zero for hours and minutes if there is a single

digit.

// Example 5 hours, 23 minutes and PM will be formatted as follows "05:23

PM"

// Example 4 hours, 4 minutes and AM will be formatted as follows "04:04

PM"

std::string getTimeString(const Time & t, std::ostream & out)

{

std::string st;

st = (*(t.hours) < 10 ? "0" : "") + std::to_string(*(t.hours)) + ":"

+ (t.minutes < 10 ? "0" : "") + std::to_string(t.minutes) + " " + t.AM_PM

+ "M";

return st;

}

// The non-member (and non-friend) function countZeros() has 4 input

parameters.

// The first input parameter h is an integer and represents the hours for

the time.

// The second input parameter m is an integer and represents the minutes

for the time.

// The third input parameter AP is a char and represents the AM or PM

(either 'A' or 'P').

// The fourth input parameter zeroCount is an integer reference variable.

// The function countZeros should create a Time object using the first 3

input parameters.

// Then the function countZeros will use the getTimeString function to get

the character

// string of the time that was created. Then the function countZeros will

count the number

// of '0' (zero) characters that are in the character string. The count is

saved in the

// reference variable zeroCount. The countZeros function prototype will be

placed in the

// Time.h file and is a non-member function and is not a friend function.

void countZeros(int h, int m, char AP, int & zeroCount)

{

zeroCount = 0; // initialize zeroCount

Time t(h, m, AP); // create the Time object

std::string str = getTimeString(t, std::cout);

for (unsigned i = 0; i < str.length(); i++)

{

if (str[i] == '0')

{

zeroCount++;

}

}

}

Time.h file

#ifndef TIME_H

#define TIME_H

#include

#include

// the Time type

class Time

{

public:

// Constructors

Time();

Time(int hours, int minutes, char AM_PM);

Time(const Time &org); // copy constructor

// Destructor

~Time();

// accessors

int getHours() const;

int getMinutes() const;

char getAMPM() const;

friend std::string getTimeString(const Time & t, std::ostream &out);

// mutators

void setTime(int hours, int minutes, char AM_PM);

// overloaded operators member functions go here

private:

// member variables

int *hours;

int minutes;

char AM_PM;

};

void countZeros(int h, int m, char AP, int & zeroCount);

// non-member operator overloads go here

#endif

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

// Time.h

#ifndef TIME_H

#define TIME_H

#include <string>

#include <iostream>

// the Time type

class Time

{

public:

               // Constructors

               Time();

               Time(int hours, int minutes, char AM_PM);

               Time(const Time &org);

               // Destructor

               ~Time();

               // accessors

               int getHours() const;

               int getMinutes() const;

               char getAMPM() const;

               // mutators

               void setTime(int hours, int minutes, char AM_PM);

               friend std::string getTimeString(const Time &time);

               Time& operator=(const Time &other);

               int operator==(const Time &other);

private:

               // member variables

               int *hours;

               int minutes;

               char AM_PM;

};

void countZeros(int h, int m, char AP,int &zeroCount);

Time operator+(const Time &other, int minutes);

#endif

//end of Time.h

// Time.cpp

#include "Time.h"

#include <new>

#include <string>

#include <iostream>

// The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator.

// You should create 3 private member variables for this class. An integer variable for the hours,

// an integer variable for the minutes, and a char variable for the AM/PM indicator. The hours variable

// must use dynamic memory allocation. The hours range from 1 to 12, the minutes range from 0 to 59,

// and the AM / PM indicator is either 'A' or 'P'.

// Create a default constructor that will initialize the time of an object to 12:00 AM.

// Use the initializer list for at least one of the variables.

Time::Time() : minutes(0), AM_PM('A')

{

   hours = new int;

   *hours = 12;

}

// Create a constructor with 3 input parameters. The input parameters are an int for the

// hours, an int for the minutes, and a char for the AM indicator. The input paramters must be

// boundary checked. The hours range from 1 to 12. If the input is out of range then default

// to 12. The minutes are from 0 to 59. If the minutes are out of range then default to 0.

// The AM indicator is either 'A' or 'P'. If the AM indicator is out of range then default to 'A'.

// You should use the setTime() public function for this constructor.

Time::Time(int h, int m, char AP)

{

   hours = new int;

   setTime(h,m,AP);

}

// Copy constructor implementation

// The copy constructor must use the initializer list for at least one member variable

// The copy constructor will copy the values of all member functions from the input

// object without creating any shallow copies

Time::Time(const Time &org) : minutes(org.minutes), AM_PM(org.AM_PM)

{

               hours = new int;

               *hours = *(org.hours);

}

// Implement the destructor for this class

Time::~Time()

{

   delete hours;

}

// Implement the getHours() public member function.

// This function has no input parameters.

// This function is a const function.

// This function returns an int as the hours of the object.

int Time::getHours() const

{

   return *hours;

}

// Implement the getMinutes() public member function.

// This function has no input parameters.

// This function is a const function.

// This function returns an int as the minutes of the object.

int Time::getMinutes() const

{

   return minutes;

}

// Implement the getAMPM() public member function.

// This function has no input parameters.

// This function is a const function.

// This function returns a char as the AM or PM of the object.

char Time::getAMPM() const

{

   return AM_PM;

}

// Implement the set() private member function.

// This function has 3 input input parameters.

// This function returns a void.

// The input parameters are an int for the

// hours, an int for the minutes, and a char for the AM indicator. The input paramters must be

// boundary checked. The hours range from 1 to 12. If the input is out of range then default

// to 12. The minutes are from 0 to 59. If the minutes are out of range then default to 0.

// The AM indicator is either 'A' or 'P'. If the AM indicator is out of range then default to 'A'.

void Time::setTime(int h, int m, char AP)

{

               if (h >= 1 && h <= 12)

                  {

                      *hours = h;

                  }

                  else

                  {

                      *hours = 12;

                  }

                  if (m >= 0 && m <= 59)

                  {

                      minutes = m;

                  }

                  else

                  {

                      minutes = 0;

                  }

                  if (AP == 'A' || AP == 'P')

                  {

                      AM_PM = AP;

                  }

                  else

                  {

                      AM_PM = 'A';

                  }

}

// Implement the assignment overloaded operator member function for the

//Time class. Ensure no

// shallow copies are made. Check for self-assignment. After the

//assignment, the lefthand side

// object should contain the same values as the righthand side object.

//Ensure the return value

// is correct.

Time& Time:: operator=(const Time &other)

{

               if(this != &other) // check if self-assignment

               {

                              this->minutes = other.minutes;

                              this->AM_PM = AM_PM;

                              *(this->hours) = *(other.hours);

               }

               return *this;

}

// Implement an overloaded operator function that implements the equality

//(==)

// operator. This function is a member function of the Time class. The

//input parameter

// will be a Time object and declared as a const reference variable. This

//member function

// will be declared as a const member function. This function will compare

// two Time objects. If the hours, minutes and AM/PM are the same between

//the two

// objects then return an integer 1, otherwise return an integer 0.

int Time::operator==(const Time &other)

{

               // check if hours, minutes and AP are same or not

               if((*hours == *(other.hours)) && (minutes == other.minutes ) && (AM_PM == other.AM_PM))

                              return 1;

               return 0;

}

// Implement an overloaded operator function that implements the addition(+)

// operator. This function is a non-member and non-friend function. This

//function will have two

// input parameters. The first input parameter is a const reference

//variable of type Time.

// The second input parameters is an integer value.

// The second input parameter will represent minutes to add to the Time

// object (the first input parameter). The second input parameter will

//range from 0 to 59.

// If the second input parameter is out of range then default it to 0.

// This function will return by value a Time object with the appropriate

//minutes added.

Time operator+(const Time &other, int add_minutes)

{

               // check the range of add_minutes

               if(add_minutes < 0 || add_minutes >59)

                              add_minutes = 0;

               // get the hours, minutes and AP from other object

               int hours = other.getHours();

               int minutes = other.getMinutes();

               char AP = other.getAMPM();

               minutes += add_minutes; // add the minutes to other minutes

               if(minutes > 59) // if minutes is greater than 59

               {

                              hours += 1; // add 1 to hour

                              minutes = minutes%60; // get the minutes left after adding 1 to hour

                              if(hours == 12) // if hour is 12, change AM to PM, or PM to AM

                              {

                                             if(AP == 'A')

                                                            AP = 'P';

                                             else

                                                            AP = 'A';

                              }else if(hours > 12)

hours -= 12;

               }

               Time new_time(hours,minutes,AP); // create new object

               return new_time; // return the new time

}

// Implement the friend function getTimeString().

// The getTimeString() has 1 input parameter.

// The first input paramenter is a const reference variable of type Time.

// This function returns a std::string type.

// This function will return the time of the first input paramenter object

// in string format as follows: hours:minutes AM

// You cannot use the getHours(), getMinutes(), or getAMPM() member functions for this function.

// There must be a leading zero for hours and minutes if there is a single digit.

// Example 5 hours, 23 minutes and PM will be formatted as follows "05:23 PM"

// Example 4 hours, 4 minutes and AM will be formatted as follows "04:04 AM"

std::string getTimeString(const Time &time)

{

               std::string timeString = "";

               if(*(time.hours) < 10) // check if hours < 0, then prepend a 0

                              timeString += '0';

               timeString += std::to_string(*(time.hours)) + ":"; // to_string(int) converts integer value to string

               if(time.minutes < 10) // check if minutes < 0, then prepend a 0

                              timeString += '0';

               timeString += std::to_string(time.minutes) + " "+time.AM_PM+"M";

               return timeString;

}

// The non-member (and non-friend) function countZeros() has 4 input parameters.

// The first input parameter h is an integer and represents the hours for the time.

// The second input parameter m is an integer and represents the minutes for the time.

// The third input parameter AP is a char and represents the AM or PM (either 'A' or 'P').

// The fourth input parameter zeroCount is an integer reference variable.

// The function countZeros should create a Time object using the first 3 input parameters.

// Then the function countZeros will use the getTimeString function to get the character

// string of the time that was created. Then the function countZeros will count the number

// of '0' (zero) characters that are in the character string. The count is saved in the

// reference variable zeroCount. The countZeros function prototype will be placed in the

// Time.h file and is a non-member function and is not a friend function.

void countZeros(int h, int m, char AP,int &zeroCount)

{

               Time t(h,m,AP);

               std::string timeString = getTimeString(t); // get the time string

               // loop over the string

               for(size_t i=0;i<timeString.length();i++)

               {

                              if(timeString.at(i) == '0') // if the char at ith index is 0, increment zeroCount

                                             zeroCount++;

               }

}

//end of Time.cpp

//C++ program to test the Time class

#include "Time.h"

#include <iostream>

#include <string>

using namespace std;

int main()

{

               Time t1, t2(11,23,'P'),t3(t2),t4(4,4,'A');

               cout<<"Time 1 : "<<getTimeString(t1)<<endl;

               cout<<"Time 2 : "<<getTimeString(t2)<<endl;

               cout<<"Time 3 : "<<getTimeString(t3)<<endl;

               cout<<"Time 4 : "<<getTimeString(t4)<<endl;

               t1 = t4;

               cout<<"Updated Time 1 "<<getTimeString(t1)<<endl;

               if((t1 == t4))

                              cout<<"Time 1 and Time 4 are same"<<endl;

               else

                              cout<<"Time 1 and Time 4 are not same"<<endl;

               Time new_time = t2 + 59;

               cout<<"New Time : "<<getTimeString(new_time)<<endl;

               return 0;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
I need help implementing 3 function prototypes with using operator overloading in a class. I have...
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
  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • The function should have a class name CPizza that will have three constructors (default, type, and...

    The function should have a class name CPizza that will have three constructors (default, type, and copyl, public functions to use and implement are noted for you in the starter kit, and private member variables (all which are allocated dynamically). The private member variables include the three different sizes of pizza (Large, Medium, and Small), cost, a bool delivery, name, and delivery fee. The prices of the pizzas' are $20. $15, and $10 for large, medium, and small respectively. The...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • choices: supporting function default constructor friend function static member class getter setter attribute assignment operator Identify...

    choices: supporting function default constructor friend function static member class getter setter attribute assignment operator Identify the parts of the following class definition: class Student { string name; const int ID; float gpa; const char gender; public: Student(); Student& operator (const Student&); string getName() const {return name; } friend ostream& operator<<(ostream&, const Student); }; void outputStudent (const Student&); name [Choose Student) [Choose operator Choose) outputStudent Choose operator<< Choose) getNamel) [Choose

  • Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define...

    Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define TREE_H #include <iostream> #include <cstdlib> // necessary in order to use NULL class TreeNode { public: TreeNode() : left(NULL), right(NULL) {}    TreeNode* left; TreeNode* right; int value; }; class Tree { public: // Default constructor Tree();    // Copy constructor Tree(const Tree& other);    //Destructor ~Tree();    // overloaded Assignment Operator Tree& operator=(const Tree& other);    // Similar to insert function we discussed...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • I need help implementing Student(const char initId [], double gpa) without using pointers. I don't know...

    I need help implementing Student(const char initId [], double gpa) without using pointers. I don't know how to initialize this with the passed in gpa value. Using C++, implement the member function specified in student.h in the implementation file, student.cpp Student(const char initId[], double gpa) { } This function will initialize a newly created student object with the passed in value. Helpful info: From student.h class Student    {    public:           Student(const char initId[], double gpa);           bool isLessThanByID(const...

  • c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe...

    c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe De Silva Design and Implement a Program that will allow all aspects of the Class BookType to work properly as defined below: class bookType { public:    void setBookTitle(string s);            //sets the bookTitle to s    void setBookISBN(string ISBN);            //sets the private member bookISBN to the parameter    void setBookPrice(double cost);            //sets the private member bookPrice to cost    void setCopiesInStock(int noOfCopies);            //sets the private member copiesInStock to...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

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