Solved

Time class Addition

Posted on 2004-04-06
3
280 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
ID: 10770498
>Time.time1, time2, sumTime;

Removed the . between Time and time1 like

Time time1, time2, sumTime;
0
 

Author Comment

by:edelossantos
ID: 10770708
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
ID: 10770805
You need to define the constructor in time3.cpp as follows.

Time::Time()
{
}
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 67
Unresolved External Symbols 3 80
Arduino EDI - Programming language 3 90
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 25
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

813 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

10 Experts available now in Live!

Get 1:1 Help Now