Hi, when putting together and running the c++ program below, I am seeing a lot o
ID: 3638339 • Letter: H
Question
Hi, when putting together and running the c++ program below, I am seeing a lot of errors. I am looking to declare a class named Time having integer data members named hours, minutes and seconds. Include a type conversion construct that converts a long integer representing the elapsed seconds from midnight into an equivalent representation as hours, minutes and seconds. A good sample of this is: a long integer (37623) would convert to the time 6:45:23. However, make use of military time by way of 2:40 is converted to 14:40 etc etc. The relationship between time representations is as follows. Any assistance is much needed. Elapsed seconds=hours*3600+minutes*60+seconds#include <iostream>
using namespace std;
int main()
{
class Time
{
int hrs,mins,secs;
public:
Time(){hrs=mins=secs=0;}
void display()
{
cout<<"hrs = "<<hrs<<" mins= "<<mins<<" secs= "<<secs<<endl;
}
Time operator=(Time& obj)
{
if(this != &obj)
{
hrs=obj.hrs;
mins=obj.mins;
secs=obj.secs;
}
}
};
int main()
{
int seconds=35800;
int hours = seconds/3600;
int tempminutes = seconds%3600;
int minutes = tempminutes/60;
seconds = tempminutes%60;
cout<<hours<<" "<<minutes<<" "<<seconds<<endl;
}
Explanation / Answer
You have declared the class within the function named main. This is an error as you cannot declare class inside a function. the code should be something like this #include .. using namespace std; class Time{ //variable declarations //Functions }; int main(){ Time t; // t.functionName(parameters...); }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.