Question

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 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

// 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 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"


// 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.

Time.h File -

#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);

// Destructor

~Time();

// accessors

int getHours() const;

int getMinutes() const;

char getAMPM() const;

// mutators

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

private:

// member variables

int *hours;

int minutes;

char AM_PM;

};

#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 &time);

               // 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);

private:

               // member variables

               int *hours;

               int minutes;

               char AM_PM;

};

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

#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 &time) : minutes(time.minutes), AM_PM(time.AM_PM)

{

               hours = new int;

               *hours = *(time.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 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

// main.cpp : C++ program to test the Time class

#include "Time.h"

#include <iostream>

#include <string>

using namespace std;

int main()

{

               Time t1, t2(5,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;

               int zeroCount = 0;

               countZeros(t4.getHours(),t4.getMinutes(),t4.getAMPM(),zeroCount);

               cout<<"Zeros count in Time 4 : "<<zeroCount<<endl;

               return 0;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
I need help implementing class string functions, any help would be appreciated, also any comments throughout...
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
  • 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...

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • 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...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • 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...

  • In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now...

    In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now modify the Rental and RentalDemo classes as follows: Modify the Rental class to include an integer field that holds an equipment type. Add a final String array that holds names of the types of equipment that Sammy's rents—personal watercraft, pontoon boat, rowboat, canoe, kayak, beach chair, umbrella, and other. Include get and set methods for the integer equipment type field. If the argument passed...

  • 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...

  • Need some assistance of reorganizing this whole program. I have the right code for everything I...

    Need some assistance of reorganizing this whole program. I have the right code for everything I just need help on putting all the codes in the right spot so it can come out to the correct output. output is supposed to look like this: 1 \\ user inputs choice to convert 12 to 24 8 \\ user inputs 8 for hours 30 \\ user inputs 30 for minutes 20 \\ user inputs 20 for seconds AM \\ user inputs AM...

  • Use c++ to code: Problem 1 Implement a class Clock whose get hours and get_minutes member functions return the current...

    Use c++ to code: Problem 1 Implement a class Clock whose get hours and get_minutes member functions return the current time at your location. To get the current time, use the following code, which requires that you include the <ctime>header time_t current_time time (e); ta local time - localtime(current, tine)i int hours -local_time->tm hour int minutes-local time->tm_min; Also provide a get_time member function that returns a string with the hours and minutes by calling the get_hours and get_minutes functions. Provide...

  • 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...

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