Solved

Time class Addition

Posted on 2004-04-06
3
259 Views
Last Modified: 2010-04-01
Need help in the correction of this code for addition Time class.  When I try to compile, compiler states Time, time1, time2, sumTime undeclared.  Line 8-10 on add_time_3.cpp.  Looking for corrected working code.  Please advise.  Del

#ifndef TIME3_H
#define TIME3_H

class Time {

public:

   Time();
   void inputTime();
   void normTime();
   Time addTime(const Time &);
   void printTime();

private:

   int week;
   int day;
   int hour;
   int minute;

};

#endif

// time3.cpp

#include <iostream>

using namespace std;

#include "time3.h"

void Time::inputTime() {

                char temp[15];
            cout << "Enter time value: " << endl;
                cin >> week >> temp >> day >> temp >> hour >>temp >> minute >> temp;

}

void Time::normTime() {

                while(minute >= 60) {
                           hour ++;
               minute = minute - 60;

               }

                while(hour >= 24) {
                           day ++;
                hour = hour - 24;

                }

                while(day >= 7) {
                          week ++;
                day = day - 7;

                }

}

Time Time::addTime(const Time &time) {
            
            Time result;
                result.week = week + time.week;
                result.day = day + time.day;
                result.hour = hour + time.hour;
                result.minute = minute + time.minute;

            return result;

}

void Time::printTime() {

                cout << week << " Weeks " << day << " Days " << hour << " Hours " << minute
                       << " Minutes" << endl;

} // end of time3.cpp

// add_time_3.cpp

#include "time3.h"

int main()
{
      Time.time1, time2, sumTime;
      time1.inputTime();
      //time1.normTime();
      //time1.printTime();

      time2.inputTime();
      //time2.normTime();
      //time2.printTime();
      
      sumTime = time1.addTime(time2);
      sumTime.normTime();
      sumTime.printTime();
      
      return 0;

} // end of main

#endif






















































0
Comment
Question by:edelossantos
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 500 total points
Comment Utility
>Time.time1, time2, sumTime;

Removed the . between Time and time1 like

Time time1, time2, sumTime;
0
 

Author Comment

by:edelossantos
Comment Utility
I Removed the . between Time and time1 like

Time time1, time2, sumTime;

Now I am getting a user bin unresolved, Time::Time(void)
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
You need to define the constructor in time3.cpp as follows.

Time::Time()
{
}
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now