| | |
also having a problem with inheritance
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2005
Posts: 2
Reputation:
Solved Threads: 0
Code compiles w/o error but print function does not pull data from base class. I have looked over the chapter numerous times. This is a long one, sorry
C++ Syntax (Toggle Plain Text)
class extClockType: public clockType { public: void getTimeZone(int& timeZone); void setTimeZone(int timeZone); void printTime() const; extClockType(); extClockType(int hours, int minutes, int seconds, int timeZone); private: int tz; }; #include <iostream> #include "clockType.h" #include "extClockType.h" using namespace std; void extClockType::setTimeZone(int timeZone) { if(tz <= 12 && tz >= -12) tz = timeZone; else tz = 0; } void extClockType::getTimeZone(int& timeZone) { timeZone = tz; } void extClockType::printTime() const { clockType::printTime(); cout << " timezone is: "; if(tz >= 0) cout << "GMT+" << tz; else cout <<"GMT" << tz; } extClockType::extClockType(int hours, int minutes, int seconds, int timeZone): clockType(hours, minutes, seconds) { if(tz <= 12 && tz >= -12) tz = timeZone; else tz = 0; } extClockType::extClockType() { tz = 0; } 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; }
![]() |
Similar Threads
- Problem with inheritance (C++)
- problem about font (C)
- Problem with program (Java)
- Ask a problem,about inheritance :) (C++)
- inheritance, polymorphism, and other features (C++)
- Point to Point3D Inheritance (C++)
- multiple inheritance (C)
- hybrid inheritance prob (C++)
Other Threads in the C++ Forum
- Previous Thread: Using Cout or printf commands
- Next Thread: How can i make this work?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





