Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,921 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,584 people online right now. Registration is fast and FREE... Join Now!




Take a look basic class stuff need help

 
Reply to this topicStart new topic

Take a look basic class stuff need help

spooky3333
18 May, 2008 - 07:59 PM
Post #1

New D.I.C Head
*

Joined: 11 May, 2008
Posts: 10

CODE


#include "stdafx.h"
#include <iostream>  
#include <string>  
  
using namespace std;  
  

  
class Time  
{  
public:  
    
    void setTime(int,int);  
    void getTime(int&,int&)const;  
    void incrementHours();  
    void incrementMinutes();  
    void printTime()const;  
  
private:  
    int hr;
    int min;  
};  
  
class extTime : public Time  
{  
public:  
    extTime();  
    string getTimeZone();  
    void printTime()const;  
  
private:  
    string timeZone;  
};  
  
  
  
void Time::setTime(int hours,int minutes)
{
    if (0 <= hours && hours < 24)
        hr = hours;
    else
        hr = 0;
    if (0 <= minutes && minutes <60)
        min = minutes;
    else
        min = 0;
}
void Time::getTime(int& hours,int& minutes) const
{
    hours = hr;
    minutes = min;
}
  
void Time::incrementHours()  
{  
    ++hr;
    if (hr > 23)
            hr = 0;
      
}  
  
void Time::incrementMinutes()  
{  
    ++min;
    if (min > 59)
        {
            min = 0;
            incrementHours();
        }

    
}  
  
void Time::printTime() const
{  
     cout << endl << "The time is " << hr << ":" << min << "." << endl;  
}  
  
extTime::extTime():timeZone("abc")  
{  
  
}  
  
string extTime::getTimeZone()  
{  
    cout << endl << "Please enter the time zone you live in:your coices are Eastern,Central,Mountain," << endl << "or Pacific:";
    cin >> timeZone;  
       return timeZone;  
}  
  
void extTime::printTime() const
{  
     Time::printTime();
    cout << "Your Time Zone = " << timeZone << endl;;
  
}  
  
int main()  
{  
    Time theTime;
    extTime theExtTime;  
  
    theTime.setTime(4, 4);  
    theTime.incrementHours();
    theExtTime.getTimeZone();
    
    theExtTime.printTime();  
    
    

    
      
    
    return 0;  
}


I need help fixing my code it dosent seem to print the time right and I need to know how to get it to increment the hour and minute right...can anyone help me?
User is offlineProfile CardPM
+Quote Post

babasmith
RE: Take A Look Basic Class Stuff Need Help
19 May, 2008 - 11:19 PM
Post #2

New D.I.C Head
*

Joined: 2 Apr, 2006
Posts: 34


My Contributions
What you're doing in the main function is printing the time of theExtTime. The problem is that the fields of this object haven't been initialized. The ones that were initialized are those of theTime, which you set and increment but not print.

Try this in main ():

CODE

theExtTime.setTime (4, 4);
theExtTime.incrementHours ();


here, the fields of theExtTime are set, incremented and printed.

Note that although Time and extTime are related by
CODE
class extTime : public Time
theTime and theExtTime are totally different objects
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:08AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month