Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Go Tools Window Help CSE30leb.pdf (page 1 of 3)- Q Search Getting started Create

ID: 3678642 • Letter: G

Question

Go Tools Window Help CSE30leb.pdf (page 1 of 3)- Q Search Getting started Create a new directory in your main development directory (probably on Desktop/CSE30) called Lab08. Try to use the terminal on your own, without getting help from the TA to setup the new directories (try to learn/remember the terminal commands). The g++ syntax to compile classes is slightly different than for a single program comprised of a main (and potential functions): g++ class1.h class1.cpp class2.h class2.cpp mainSource.cpp -o executable where: . g++ is the compiler (installed on your Linux system) of C++ source files, . mainSource.cpp is the source file for your main program (main function), class1.h is the class declaration for your 1st class, class1.cpp is your 1st class definition, · class2·h is the class declaration for your 2nd class, · class2.cpp is your 2nd class definition (and so on..), -o tells the compiler that you want to give the executable its owri name, and executable is the name you want to give your program. As an example, if we have a main source file called main.cpp, the class declaration called Time.h, the class definition called Time.cpp, and want to create an executable called aTestProgram, you would type g++ Time.h Time.cpp main.cpp -o aTestProgram

Explanation / Answer

######### Time.h############

class Time{
   private:
       int seconds;
       int minutes;
       int hours;
      
   public:
       Time();
       Time(int sec, int min, int hrs);
       ~Time();
       void setSeconds(int sec);
       void setMinutes(int min);
       void setHours(int hrs);
       int getSeconds();
       int getMinutes();
       int getHours();
   };


############## Time.cpp #################

#include "Time.h"

Time::Time(){
   seconds = 0;
   minutes = 0;
   hours = 0;
   }

Time::Time(int sec, int min, int hrs){
   seconds = sec;
   minutes = min;
   hours = hrs;
   }

Time::~Time(){
  
   }
  
void Time::setSeconds(int sec){
   seconds = sec;
   }
  
void Time::setMinutes(int min){
   minutes = min;
   }
  
void Time::setHours(int hrs){
   hours = hrs;
   }
  
int Time::getSeconds(){
   return seconds;
   }
  
int Time::getMinutes(){
   return minutes;
   }
  
int Time::getHours(){
   return hours;
   }


################ main.cpp ########################

#include<iostream>
#include <sstream>
#include <string>
#include "Time.h"

using namespace std;

bool isHourValid(int hrs){
   if(hrs>=0 && hrs<=23)
       return true;
   return false;
   }

bool isMinuteValid(int min){
   if(min>=0 && min<=59)
       return true;
   return false;
   }
  
bool isSecondValid(int sec){
   if(sec>=0 && sec<=59)
       return true;
   return false;
   }
  
bool validateTime(int t[]){
  
   return isHourValid(t[0]) && isMinuteValid(t[1]) && isSecondValid(t[2]);
   }  
void convertStringToInt(string s, int t[]){
   string delimiter = ":";
   int i=0;
   size_t pos = 0;
   string token;
   while ((pos = s.find(delimiter)) != string::npos) {
       token = s.substr(0, pos);
       stringstream convert(token);
       convert>>t[i++];
       s.erase(0, pos + delimiter.length());
   }
   stringstream convertSec(s);
   convertSec>>t[i];
  
   }
int main(){
  
   string s ;
  
   // start time
   cout<<"Enter the start time for the lecture (format is HH:MM:SS): ";
   cin>>s;

   // spliting string with :
   int start[3],end[3];
  
   convertStringToInt(s, start);
   // creating Time Object
   Time srt(start[2], start[1], start[0]);
   // end time
   cout<<"Enter the end time for the lecture (format is HH:MM:SS): ";
   cin>>s;
   convertStringToInt(s, end);
   // creating Time Object
   Time ed(end[2], end[1], end[0]);
  
   if(! validateTime(start))
       cout<<"Start time is invalid"<<endl;
   else if(! validateTime(end))
       cout<<"End time is invalid"<<endl;
   else
       cout<<"The lecture starts at "<<srt.getHours()<<":"<<srt.getMinutes()<<":"<<srt.getSeconds()<<
               " and ends at "<<ed.getHours()<<":"<<ed.getMinutes()<<":"<<ed.getSeconds()<<endl;
   }
  
    /*

Output:

:~/Desktop/MyCode/17th March$ g++ Time.h Time.cpp main.cpp
:~/Desktop/MyCode/17th March$ ./a.out
Enter the start time for the lecture (format is HH:MM:SS): 23:59:59
Enter the end time for the lecture (format is HH:MM:SS): 23:85:00
End time is invalid

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote