Avatar of Chris Jones
Chris JonesFlag for United States of America

asked on 

i need some help with my nheritance program

i have some code it has been giveing me problems
class extClockType: public clockType
{
  public:
	int getTimeZone();
	void setTimeZone(int t);
	void setTime(int hours, int minutes, int seconds, int t);
	void getTime(int& hours, int& minutes, int& seconds, int& t) const;
	void printTime() const;
	extClockType();
	extClockType(int hours, int minutes, int seconds, int t);
  private:
	int timeZone;
};
 
 
#include <iostream>
#include "clockType.h"
#include "extClockType.h"
 
using namespace std;
 
 
 
int extClockType::getTimeZone()
{
	return timeZone;
}
 
void extClockType::setTimeZone(int t)
{
	timeZone = t;
}
 
void extClockType::setTime(int hours, int minutes, int seconds, int t)
{
	clockType::setTime(hours, minutes, seconds);
	timeZone = t;
}
 
void extClockType::getTime(int& hours, int& minutes, int& seconds, int& t) const
{
	clockType::getTime(hours, minutes, seconds);
	t = timeZone;
}
 
void extClockType::printTime() const
{
	clockType::printTime();
	cout << "The timezone is: " << timeZone << endl;
	if(timeZone >= 0)
    cout << "+" << timeZone;
}
 
extClockType::extClockType() 
{
	timeZone = 0;	
}
 
extClockType::extClockType(int hours, int minutes, int seconds, int t): clockType(hours, minutes, seconds)
{
	if(timeZone > 12 || timeZone < -12)
    timeZone = 0;
  else
    timeZone = t;
}
-----------------------------------
class clockType
{
public:
    void setTime(int hours, int minutes, int seconds); 
    void getTime(int& hours, int& minutes, int& seconds) const;
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType& otherClock) const;
    clockType(int hours, int minutes, int seconds);
    clockType();
private:
    int hr;
    int min;
    int sec;
};
 
#include <iostream>
#include "clockType.h"
 
using namespace std;
 
void clockType::setTime(int hours, int minutes, int seconds)
{
	if (0 <= hours && hours < 24)
	    hr = hours;
	else 
	    hr = 0;
 
	if (0 <= minutes && minutes < 60)
	    min = minutes;
	else 
	    min = 0;
 
	if (0 <= seconds && seconds < 60)
	    sec = seconds;
	else 
	    sec = 0;
}
 
void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
	hours = hr;
	minutes = min;
	seconds = sec;
}
 
void clockType::incrementHours()
{
	hr++;
	if(hr > 23)
 	   hr = 0;
}
 
void clockType::incrementMinutes()
{
	min++;
	if (min > 59)
	{
	    min = 0;
	    incrementHours();
	}
}
 
void clockType::incrementSeconds()
{
	sec++;
 
	if (sec > 59)
	{
	    sec = 0;
	    incrementMinutes();
	}
}
 
void clockType::printTime() const
{
	if (hr < 10)
	    cout << "0";
	cout << hr << ":";
 
	if (min < 10)
	    cout << "0";
	cout << min << ":";
 
	if (sec < 10)
	   cout << "0";
	cout << sec;
}
 
bool clockType::equalTime(const clockType& otherClock) const
{
	return (hr == otherClock.hr 
		    && min == otherClock.min 
		    && sec == otherClock.sec);
}
 
clockType::clockType(int hours, int minutes, int seconds)
{
	if (0 <= hours && hours < 24)
		hr = hours;
	else
		hr = 0;
 
	if (0 <= minutes && minutes < 60)
		min = minutes;
	else
		min = 0;
 
	if (0 <= seconds && seconds < 60)
		sec = seconds;
	else
		sec = 0;
}
 
clockType::clockType()
{
	hr = 0;
	min = 0;
	sec = 0;
}
--------------------------------------
 
 
#include <iostream>
#include "clockType.h"
#include "extClockType.h"
 
using namespace std;
 
int main()
{
	int hours;
    int minutes;
    int seconds;
	int timeZone;
 
	clockType clockOne;
	clockType clockTwo;
 
    extClockType tzClock;
	extClockType tzClock2;
		
    clockOne.setTime(9, 30, 26); 
	tzClock.setTimeZone(8);
	cout << "Clock One's time is currently: ";
	tzClock.printTime();
	clockOne.printTime();
    cout << endl;  
 
    clockTwo.setTime(9, 30, 28);
	tzClock2.setTimeZone(7);
	cout << "Clock Two's time is currently: ";
	tzClock2.printTime();
    cout << endl;
   
    if (clockOne.equalTime(clockTwo))
        cout << "Both times are equal."
             << endl;
    else
	{
    cout << "Both times are not equal."
             << endl;
    cout << "Please, enter the current time"
		<< "(hrs, mins, secs, and timezone): ";
    cin >> hours >> minutes >> seconds >> timeZone;
    
    clockOne.setTime(hours, minutes, seconds);
	tzClock.setTimeZone(timeZone);
	cout << "The time is has been set to: ";
	tzClock.printTime();
	cout << endl;
	}
 
    clockOne.incrementSeconds();
    cout << "After incrementing the time by one second: ";
	tzClock.printTime();
    cout << endl;
    
    clockOne.getTime(hours, minutes, seconds);
	tzClock.getTimeZone(timeZone);
	     
    cout << "hours = " << hours 
         << ", minutes = " << minutes
         << ", seconds = " << seconds << endl;
 
	return 0;
}

Open in new window

C++

Avatar of undefined
Last Comment
Chris Jones
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

I'm not sure what problem(s), but I'd start be declaring the class after in #include statements.



Kent
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Chris Jones
Chris Jones
Flag of United States of America image

ASKER

ok it compiles  now but i get the wrong data and i am not sure were it is geting lost at it returns 0 for all my times
Avatar of jkr
jkr
Flag of Germany image

Well, seems like you have to debug ;o)
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Chris Jones
Chris Jones
Flag of United States of America image

ASKER

fast respomse great answer
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo