Question

please help! i dont know why there are bugs and how to fix this.. (in C++) please help!

the problem:

Create a class called clockType for a clock type with fields hr, min, and sec. Your code should support the following set of

my code:

#include <iostream> #include clockType.h using namespace std; int main() clockType cl clockType c2 (10, 59) clockType c3 (5clockType:: clockType ) hr-0; min 0 sec-0; clockType:: clockType (int hour, int minute, int second) hr- hour; min minut e ; s#ifndef CLOCKTYPE H #de fine CLOCKTYPE H #include <iostream> #include <iomanip using namespace std; class clockType public: c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

The below is the required source code for the given problem in C++

Code:-

/*clockType.h*/ #ifndef CLOCKTYPE_H #define CLOCKTYPE_H #include<iostream> using namespace std; class clockType { private: int hr, min, sec; public: clockType(int hr_ = 0, int min_ = 0, int sec_ = 0) : hr(hr_), min(min_), sec(sec_) {} friend ostream& operator << (ostream&, const clockType&); clockType operator + (const clockType&); clockType operator + (const int&); }; #endif /*clockType.cpp*/ #include<iostream> #include "clockType.h" using namespace std; /*Defining friend operator << function*/ ostream& operator << (ostream &o, const clockType &obj) { o << obj.hr << ":" << obj.min << ":" << obj.sec << endl; return o; } /*+ operator overloading to add two clockType objects*/ clockType clockType::operator + (const clockType &b) { clockType c; c.sec = this->sec + b.sec; if(c.sec > 60) { c.min++; c.sec = c.sec % 60; } c.min = this->min + b.min; if(c.min >= 60) { c.hr++; c.min = c.min % 60; } c.hr = this->hr + b.hr; return c; } /*+ operator overloading to add clockType object with int variable*/ clockType clockType::operator + (const int &b) { clockType c; c.sec = this->sec + b; if(c.sec > 60) { c.min++; c.sec = c.sec % 60; } if(c.min >= 60) { c.hr++; c.min = c.min % 60; } return c; } /*lab1-cmpe126.cpp*/ #include <iostream> #include "clockType.h" using namespace std; int main() { clockType c1; clockType c2(10, 59); clockType c3(5, 10, 20); int sec = 10; cout << c1; cout << c2 << c3; cout << c1 + c2; c1 = c1 + sec; return 0; } 

Sample Output:-

C\Users\Shubham\Desktoplab1-cmpe126.exe 0:0:0 10:59:0 5:10:20 10:59:0

If you find difficulty to understand the code, please let me know then I will provide another code. Hope it will helps. Please give Thumbs Up!! Thank you for posting the question, All the best.

Add a comment
Know the answer?
Add Answer to:
please help! i dont know why there are bugs and how to fix this.. (in C++)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a program that needs comments added to it: #include <iostream> #include <stdio.h> #include <conio.h>...

    I have a program that needs comments added to it: #include <iostream> #include <stdio.h> #include <conio.h> #include <windows.h> using namespace std; int main() { int m, s,h; cout << "A COUNTDOWN TIMER " << endl; cout << "enter time in hours here" << endl; cin >> h; cout << "enter time in minutes here " << endl; cin >> m; cout << "enter time in seconds here" << endl; cin >> s; cout << "Press any key to start" <<...

  • (1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix)...

    (1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix) as friend functions.         (2)The program should be in Separating Class Specification, Implementation(complex.h,complex.cpp), and Client Code(Task2.cpp).             (3) Run the program and capture screenshots of output. Declaration and implementation of complex class, overloading operator +, -, != , /=, --(prefix), --(postfix) as friend function */ #include "Complex.h" #include <iostream> using namespace std; int main(){       Complex c1(5, 4), c2(2, 10), c3;       cout <<...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • How can I modify the program below with this prompt asking "How many minutes from now...

    How can I modify the program below with this prompt asking "How many minutes from now do you expect to be home?", and output a sentence saying "You will get home at HH:MM". ?? It's very URGENT!! Thank you #include <iostream> #include <ctime> using namespace std; int main) time t t struct tm *now t-time (e) now-localtime(&t); int hour = now->tm.hour; // retrieve current hour int min = now->tm..min; // retrieve current min // get current time // adjust for...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0)...

    (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT