Question

In: Computer Science

Question: Use Eclipse to create a clockType with hr, min, sec as private members. You shall...

Question:

Use Eclipse to create a clockType with hr, min, sec as private members.

You shall have 3 files: clock.h clock.cpp and lab1main.cpp

lab1main.cpp shall support the following statements:

clockType c1(15, 45, 30), c2(3, 20); // hour, min, sec

cout << c1; // add whatever to beautify it

cout << c2;

cout << c1+c2;

c2 = c1+c1;
cout << c2;

Need help please!

Solutions

Expert Solution

//clockType.h

#include<iostream>
using namespace std;

class clockType{
   private:
       int hour, min, sec;
      
   public:
       clockType(int ,int ,int );
       clockType operator+(const clockType &clock);
       friend ostream & operator << (ostream &out, const clockType &clock);
friend istream & operator >> (istream &in, clockType &clock);
};

//clockType.cpp

#include"clockType.h"

clockType::clockType(int h=0,int m=0,int s=0){
   hour = h;
   min = m;
   sec=s;
}
clockType clockType::operator+(const clockType &clock){
   clockType c;
   int s = this->sec+clock.sec;
   int m = this->min+clock.min+s/60;
   int hr = this->hour+clock.hour+m/60;
  
   c.hour = hr%24;
   c.min = m%60;
   c.sec = s%60;
   return c;
}
ostream & operator << (ostream &out, const clockType &clock){
   out<<clock.hour<<":"<<clock.min<<":"<<clock.sec<<"\n";
   return out;
}
istream & operator >> (istream &in, clockType &clock){
   cout<<"Enter hour : ";
   in>>clock.hour;
   cout<<"Enter minute : ";
   in>>clock.min;
   cout<<"Enter second : ";
   in>>clock.sec;
  
   return in;
}

//lab1main.cpp

#include"clockType.cpp"

int main(){
   clockType c1(15, 45, 30), c2(3, 20);
   cout << c1;
   cout << c2;
   cout << c1+c2;
   c2 = c1+c1;
   cout << c2;
   return 0;
}

//sample output


Related Solutions

PLZ USE JAVA ECLIPSE AND EXPLAIN Create a GUI which works as an accumulator:  There...
PLZ USE JAVA ECLIPSE AND EXPLAIN Create a GUI which works as an accumulator:  There is a textfield A which allows user to enter a number  There is also another textfield B with value start with 0.  When a user is done with entering the number in textfield A and press enter, calculate the sum of the number in textfield B and the number user just entered in textfield A, and display the updated number in textfield...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours...
Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type double. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type double. For each class, provide its getter and setter functions, a default constructor, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for the Services class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class for working with complex numbers. Only 2 private float data members are needed,...
Create a class for working with complex numbers. Only 2 private float data members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class:a. A default constructor that uses default arguments in case no initializers are included in the main.b. Add two complex numbers and store the sum.c. Subtract two complex numbers and store the difference.d. Multiply two complex numbers and store the product.e. Print...
create a class in C++ having the following 4 private members: one double variable representing the...
create a class in C++ having the following 4 private members: one double variable representing the balance of a bank account on string variable representing the bank account number a function to deposit money from the bank account a function to withdraw money from the bank account the following 2 public members: -two wrapper functions. - one parameterless constructor - one constructor initializing the account balance and account number - sample: #include <iostream> using namespace std; class account { public:...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Create a "Date" class that contains: three private data members: month day year (I leave it...
Create a "Date" class that contains: three private data members: month day year (I leave it to you to decide the type) "setters" and "getters" for each of the data (6 functions in total) One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive. one default constructor (no arguments) one constructor with three arguments: month, day,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT