Wednesday 6 July 2016

Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. 

Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. 

A main() program should create two initialized time objects (should they be const?) and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. 

Finally it should display the value of this third variable. Make appropriate member functions const....


#include <iostream>
#include <conio.h>

using namespace std;

class time{
private:
int hours,minutes,seconds;
public:
time(){
hours = minutes = seconds = 0;
}
time(int h, int m, int s){
hours = h;
minutes = m;
seconds = s;
}
void showTime() const {
cout << hours << ':' << minutes << ':' << seconds;
}
void addTime(time x, time y){
seconds = x.seconds + y.seconds;
if(seconds>59){
seconds-=60;
minutes++;
}
minutes += x.minutes + y.minutes;
if(minutes>59){
minutes-=60;
hours++;
}
hours+=x.hours+y.hours;
}
};

int main(){
const time a(2,23,45), b(4,25,15);
time c;
c.addTime(a,b);
c.showTime();
}

20 comments:

  1. Create a class Fraction. Member data is the fraction’s numerator and denominator. Member functions should accept input from the user in the form 3/5, and output the fraction’s value in the same format. Overload operators for addition, subtraction, multiplication, and division to add two fractions. Also overload the == and != comparison operators, and use them to exit from the loop if the user enters 0/1, 0/1 for the values of the two input fractions. You may want to modify the lowterms() function so that it returns the value of its argument reduced to lowest terms. This makes it more useful in the arithmetic functions, where it can be applied just before the answer is returned.

    Gave me the solution

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. https://www.chegg.com/homework-help/questions-and-answers/c-question-operator-overloading-client-program-include-include-using-namespace-std-void-ba-q6453387

      Delete
  2. Very Helpful

    ReplyDelete
  3. Create an employee class, basing it on Exercise 4 of Chapter 4. The member data should comprise an int for storing the employee number and a float for storing the employee’s compensation. Member functions should allow the user to enter this data and display it. Write a main() that allows the user to enter data for three employees and display it.

    ReplyDelete
    Replies
    1. #include
      using namespace std;
      class employee{
      public:
      int employee_number;
      float employee_salary;
      public:
      void enter_function()
      {
      cout<<"Enter number of employee:"<>employee_number;
      cout<<"Enter salary of employee:"<>employee_salary;
      }
      void diplay()
      {

      cout<<"Employee number:"<<employee_number<<endl;
      cout<<"Employee salary:"<<employee_salary<<endl;
      }

      };
      int main()
      {
      employee emp;
      for(int i=1; i<=3; i++)
      {
      emp.enter_function();

      }
      cout<<"******Record of employees!!******"<<endl;
      for(int i=1; i<=3; i++)
      {

      emp.diplay();
      }
      }

      Delete
  4. Create a class named ’Rectangle’ with two data members- length and breadth and a method
    to calculate the area which is ’length*breadth’. The class has three constructors which are: 1 -
    Having no parameter - values of both length and breadth are assigned zero. 2 - Having two
    numbers as parameters - the two numbers are assigned as length and breadth respectively. 3
    - Having one number as parameter - both length and breadth are assigned that number. Now,
    create objects of the ’Rectangle’ class having none, one and two parameters and print their
    areas

    ReplyDelete
  5. How to do with the friend function?

    ReplyDelete
  6. Can anyone help converting this code to python(oop)
    Please

    ReplyDelete
  7. algorithm for this code?

    ReplyDelete
  8. Create a time class having data members hour, minute and second. Make a constructor to set the members zero, make a display function that outputs the time in given format: 00:00:00. Make a function called ticktick() that increments the time in such a way that hour do not change till the minutes and seconds ends at 60. (01:01:01…. 01:59:59 -> 02:00:00). Design the main function, create an object and call the functions accordingly.

    Lab Grading Sheet :
    Task Max Marks Obtained Marks Comments(if any)
    1. 04
    2. 03
    3. 03
    Total 10 Signature

    Note : Attempt all tasks and get them checked by your Lab Instructor.

    ReplyDelete
  9. Add to the time class hat has separate int member data for hours, minutes, and Seconds the ability to subtract two time values using the overloaded (-) operator, and to multiply a time value by a number of type float, using the overloaded (*) operator.

    ReplyDelete
  10. It is more well than other nesty

    ReplyDelete