Question
?So i have this program, but it keeps saying that A, B, C and D are not declared. I believe they need to be objects
?So i have this program, but it keeps saying that A, B, C and D are not declared. I believe they need to be objects of a class? How do I fix this program to make it work?
#include
using namespace std;
class time
{
private:
int hours;
int minutes;
int seconds;
int check_time()
{
if(hours>=1 && hours<=24 && minutes>=0 && minutes<60 && seconds>=0 && seconds<60)
return 0;
else
return 1;
}
public:
time()
{
hours=1;
minutes=0;
seconds=0;
}
time(int h,int m,int s) // parameterized constructor
{
hours=h;
minutes=m;
seconds=s;
}
void setTime()
{
while(true)
{
cout<<"Enter Time in 24 hours format (Hours: Minutes: Seconds)"< cin>>hours; // get hours from user cin>>minutes; // get minutes cin>>seconds; // get seconds if(check_time()==1) // calls check_time() function to check input { cout<<"You have entered an invalid time value, hours should between 1 and 24"< cout<<"minutes and seconds should be between 0 and 60. Please try Again:"< } else { cout<<"Thank you...!!!"< break; // to exit from while loop } } } void getTime() // display time { cout< } friend time operator +(time t1,time t2); // declare friend function to overload + operator time addTime(time t1,time t2,time t3) // addTime() function to add three objects { time t4; t4=t1+t2+t3; return t4; } }; time operator +(time t1,time t2) // overload + operator { time t4; t4.hours=t1.hours+t2.hours; t4.minutes=t1.minutes+t2.minutes; t4.seconds=t1.seconds+t2.seconds; return t4; } int main() // main function { char ch; while(true) { time A(10,10,10); // creates object of time class and invokes parameterized constructor time B; // invokes default constructor time C; C.setTime(); // calls setTime() function cout<<"Your first time value is: "; A.getTime(); // calls getTime() function cout<<"Your second time value is: "; B.getTime(); cout<<"Your third time value is: "; C.getTime(); time D; D=D.addTime(A,B,C); // calls addTime() function to add three objects cout<<"Total time that has passed is: "; D.getTime(); cout<<"End of Program !"< cout<<"Do you want to try again? Press Y for yes or N for no"< cin>>ch; if(ch=='N' || ch=='n') // decides whether loop has to continue execution or not break; // exit from while loop } // end of while }// end of main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started