In: Computer Science
C++ program homework question 1
1. Create and implement a class called clockType with the following data and methods (60 Points.):
Derive a new class called extClockType from the class clockType by adding a member variable to store the time zone. Add the necessary member functions and constructors and implement them
Please find the code , output below.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// Class declaration
class clockType {
public:
int hours, minutes, seconds;
clockType (int hours1, int minutes1, int seconds1){
hours=hours1;
minutes=minutes1;
seconds=seconds1;
}
void sethours(int hours1){
hours=hours1;
}
void setminutes(int minutes1){
minutes=minutes1;
}
void setseconds(int seconds1){
seconds=seconds1;
}
int gethouors(){
return hours;
}
int getminutes(){
return minutes;
}
int getseconds(){
return seconds;
}
// Printing the output
void printTime(){
if(hours<10 && minutes>=10 && seconds
>=10){
cout<<"The time is 0"<<hours<<" : "<<
minutes<< " : "<<seconds<<endl;
}
else if (hours>=10 && minutes < 10 &&
seconds>=10){
cout<<"The time is "<<hours<<" : 0"<<
minutes<< " : "<<seconds<<endl;
}
else if (hours>=10 && minutes >= 10 &&
seconds <10){
cout<<"The time is "<<hours<<" : "<<
minutes<< " : 0"<<seconds<<endl;
}
else if (hours>=10 && minutes < 10 && seconds
<10){
cout<<"The time is "<<hours<<" : 0"<<
minutes<< " : 0"<<seconds<<endl;
}
else if (hours <10 && minutes >= 10 &&
seconds <10){
cout<<"The time is 0"<<hours<<" : "<<
minutes<< " : 0"<<seconds<<endl;
}
else if (hours <10 && minutes < 10 && seconds
>=10){
cout<<"The time is 0"<<hours<<" : 0"<<
minutes<< " : "<<seconds<<endl;
}
else if (hours<10 && minutes < 10 && seconds
<10){
cout<<"The time is 0"<<hours<<" : 0"<<
minutes<< " : 0"<<seconds<<endl;
}
else{
cout<<"The time is "<<hours<<" : "<<
minutes<< " : "<<seconds<<endl;
}
}
};
// Child class
class extClockType : public clockType {
public:
vector<string> time_zone;
extClockType(int hours1, int minutes1, int seconds1,string
timezone): clockType(hours1,minutes1,seconds1){
time_zone.push_back(timezone);
}
void printTimezone(){
cout<<"\nThe timezone is "<<time_zone[0];
}
};
int main()
{
// Getting input from user
int hours, minutes, seconds;
cout<<"Enter the hours : ";
cin>>hours;
if(hours<0 || hours>24){
cout<< "Invalid hours !!!";
exit(0);
}
cout<<"Enter the minutes : ";
cin>>minutes;
if(minutes<0 || minutes>60){
cout<< "Invalid minutes !!!";
exit(0);
}
cout<<"Enter the seconds : ";
cin>>seconds;
if(seconds<0 || seconds>60){
cout<< "Invalid seconds !!!";
exit(0);
}
// Creating object of clockType class
clockType ct(hours,minutes,seconds);
//ct.printTime();
extClockType ect(hours,minutes,seconds,"Pacific");
// Calling the function
ect.printTime();
ect.printTimezone();
return 0;
}