The code has already been written. Please describe what each line and each funct
ID: 3874023 • Letter: T
Question
The code has already been written. Please describe what each line and each function does. I have to explain to someone else and I have a limited knowledge of C++
I have attached the instruction, the answer which is the code and the output the code gave. i want an explanation on the code on what each line is performing. I need the code to be explained.
#include<iostream>
using namespace std;
class Converter
{
private:
int days, hours, minutes, seconds;
const int SECONDS_IN_7_DAYS = 604800; //60x60x24x7
public:
Converter()
{
days = 0;
hours = 0;
minutes = 0;
seconds = 0;
}
int getDays()
{
return days;
}
int getMinutes()
{
return minutes;
}
int getSeconds()
{
return seconds;
}
int getHours()
{
return hours;
}
void setSeocnds(int s)
{
if (s > SECONDS_IN_7_DAYS)
cout << "Seoconds are greater than 7 days. " << endl;
else
seconds = s;
}
void convertSeconds(int s)
{
setSeocnds(s);
if (seconds % 86400 == 0)
{
days = seconds / 86400;
cout << "Number of days are: " << days <<" Days"<< endl;
}
else if (seconds % 86400 != 0)
{
if (seconds < 86400)
{
if (seconds % 3600 == 0)
{
hours = seconds / 3600;
cout << "hours are : " << hours << " hours" << endl;
}
else
{
hours = seconds / 3600;
seconds = seconds % 3600;
minutes = seconds / 60;
seconds = seconds % 60;
if (hours == 0)
cout << minutes << " mins and " << seconds << " secs" << endl;
else if (minutes == 0)
cout << seconds << " secs " << endl;
else
cout << hours << " hours " << minutes << " minutes " << seconds << " secs " << endl;
}
}
if (seconds > 86400)
{
days = seconds/ 86400;
seconds = seconds % 86400;
hours = seconds / 3600;
seconds = seconds % 3600;
minutes = seconds / 60;
seconds = seconds % 60;
if (days == 0)
cout << hours << " hours " << minutes << " minutes " << seconds << " secs " << endl;
if (hours == 0)
cout<<days<<" Days " << minutes << " mins and " << seconds << " secs" << endl;
else if (minutes == 0)
cout << days << " Days " << hours << " Hours " << seconds << " secs " << endl;
else
cout << days << " Days " << hours << " Hours " << minutes << " mins " << seconds << " Secs " << endl;
}
}
}
};
void main()
{
Converter secConvert;
int sec;
x:
cout << "Enter Seconds to convert: ";
cin >> sec;
if (sec < 0)
goto x;
secConvert.convertSeconds(sec);
system("pause");//to hold the output screen
}
Explanation / Answer
#include<iostream> // C++ input/output streams are primarily defined by iostream
using namespace std; // std is an abbreviation of standard. std is the standard namespace. cout, // cin and a lot of other things are defined in it.
class Converter // Define a user defined class called Converter
{
private: // define private members of a class . That aare accessible only inside the // class and the member functions of the class .
int days, hours, minutes, seconds; // The private data members of the class .
const int SECONDS_IN_7_DAYS = 604800; //60x60x24x7 Seconds that are in a week
public: // The public members of a class that are accessible outside the class // through the classs objects and references .
Converter() // Defining a default constructor to the class . A constructor is a function with // the same name as the class that is used to provide legal initial values to // the data members of a class . It is invoked automatically when we create // an object of the class .
{
days = 0; // Assigning days value 0 .
hours = 0; // Assigning hours value 0 .
minutes = 0; // Assigning minutes value 0 .
seconds = 0; // Assigning seconds value 0 .
}
int getDays() // Defining a user defined function that returns the number of days in the // objects attributes , to the caller statement .
{
return days; // return the data value of days .
}
int getMinutes() // Defining a user defined function that returns the number of minutes in the // objects attributes , to the caller statement .
{
return minutes; // return the data value of minutes .
}
int getSeconds() // Defining a user defined function that returns the number of seconds in the // objects attributes , to the caller statement .
{
return seconds; // return the data value of seconds .
}
int getHours() // Defining a user defined function that returns the number of seconds in the // objects attributes , to the caller statement .
{
return hours; // return the data value of hours .
}
void setSeocnds(int s) // A member function to assign value to the seconds data member of an // object. We send the value to be assigned as a parameter to the function // (parameter s ).
{
if (s > SECONDS_IN_7_DAYS) // Checking if the seconds are greater than the seconds in a week .
// If true we will give an alert message that it is greater and to set with // another value .
cout << "Seoconds are greater than 7 days. " << endl;
else
seconds = s; // If false we will assign the value of s to seconds .
}
void convertSeconds(int s) // A member function to convert the number of seconds into days, // hours and minutes .
{
setSeocnds(s); // First set the seconds attribute to s .
if (seconds % 86400 == 0) // If the seconds is exactly a multiple of 86400 , that is the number of //seconds in a day .
{
days = seconds / 86400; // We simply calculate the number of days by dividing seconds by 86400.
// And the rest of the attributes hours and minutes are left 0 .
cout << "Number of days are: " << days <<" Days"<< endl; // Print the number of days on standard output screen .
}
else if (seconds % 86400 != 0) // If seconds is not exactly divisible by 86400 and a remainder exists .
{
if (seconds < 86400) // If the seconds is less than 86400 , that is less than a single day .
{
if (seconds % 3600 == 0) // If the seconds is exactly divisible by 3600 , that is seconds in an hour .
{
hours = seconds / 3600; // We simply calculate the number of hours and and assign the value to // hours
cout << "hours are : " << hours << " hours" << endl; // Print out the number of hours .
}
else // If the number of seconds is not exactly divisble by 3600 .
{
hours = seconds / 3600; // We find the number of hours by dividing seconds with 3600 , since it is // an integer operation , it has no fractional parts .
seconds = seconds % 3600; //The remainder of the seconds after dividing with 3600 is calculated .
minutes = seconds / 60; // We find the number of hours by dividing the remainder of the seconds // with 60 .
seconds = seconds % 60; // After we divide it by 60 , the remainder of the seconds is calculated . // This is the actaul seconds in normal time format .
if (hours == 0) // If the number of hours is equal to 0. We just need to print out minutes // and the seconds .
cout << minutes << " mins and " << seconds << " secs" << endl;
else if (minutes == 0) // Similarly ,if the number of minutes is equal to 0. We just need to print out // hours and the seconds .
cout << seconds << " secs " << endl;
else // If none of them are 0, we need to print all the three values .
cout << hours << " hours " << minutes << " minutes " << seconds << " secs " << endl;
}
}
if (seconds > 86400) // If seconds is greater than 86400 , it comprises of days also .
{
days = seconds/ 86400; // We find the number of days by dividing seconds with 86400 , since it is // an integer operation , it has no fractional parts .
seconds = seconds % 86400; //The remainder of the seconds after dividing with 86400 is calculated .
hours = seconds / 3600; // We find the number of hours by dividing seconds with 3600 , since it is // an integer operation , it has no fractional parts .
seconds = seconds % 3600; //The remainder of the seconds after dividing with 3600 is calculated .
minutes = seconds / 60; // We find the number of hours by dividing the remainder of the seconds // with 60 .
seconds = seconds % 60; // After we divide it by 60 , the remainder of the seconds is calculated . // This is the actaul seconds in normal time format .
if (days == 0) // If the number of days is equal to 0. We just need to print out minutes, //hours and the seconds .
cout << hours << " hours " << minutes << " minutes " << seconds << " secs " << endl;
if (hours == 0) // If the number of hours is equal to 0. We just need to print out days, // minutes and the seconds .
cout<<days<<" Days " << minutes << " mins and " << seconds << " secs" << endl;
else if (minutes == 0) // If the number of minutes is equal to 0. We just need to print out days, // hours and the seconds .
cout << days << " Days " << hours << " Hours " << seconds << " secs " << endl;
else // If none of them are 0, we need to print all the four values .
cout << days << " Days " << hours << " Hours " << minutes << " mins " << seconds << " Secs " << endl;
}
}
}
};
void main()
{
Converter secConvert; // Create an object of type Converter , this implicilty calls the default // constructor .
int sec; // A variable sec to accept input in seconds from the user .
x: // Label to jump back to this location .
cout << "Enter Seconds to convert: "; // Asking user to enter the input in seconds .
cin >> sec; // Accepting the value into sec variable .
if (sec < 0) // Since the seconds cant be a negative value , if a value less than 0 is // given as input . We ask the user to inout a value again .
goto x; // If so go back to label x .
secConvert.convertSeconds(sec); // We use the member function of the object to convert seconds into days , // hours , minutes and seconds . This is the call to a function with sec as // parameter .
system("pause"); //to hold the output screen
}
Overview
We create a class with 4 data member : days, hours , minutes and seconds . Also 4 methods to return the values of these 4 data members . A constructor is used to assign all these with value 0 when an object is created . We use a set function to assign the values to a Seconds dat member . We define convertSeconds() , a member function to convert the given value of seconds into days , hours , minutes and seconds using various arithmetic operations , and printing it out on standard output screen .