I've looked over this, and never having used classes before am a bit confused on what I'm doing wrong. If i initialize the counter variable prior to main as part of the class declaration it works fine, otherwise it gives me errors.
Code:#include <iostream> using namespace std; class time { private: int hours; int minutes; int seconds; public: time( int = 0 , int = 0 , int = 0 ); void show_time(); void tick_time(); }; time::time( int hr , int min , int sec ) { hours = hr; minutes = min; seconds = sec; } void time::show_time() { cout << hours << ':' << minutes << ':' << seconds << '\n'; return; } void time::tick_time() { ++seconds; if( seconds == 60 ) { seconds = 0; ++minutes; if( minutes == 60 ) { minutes = 0; ++hours; } } return; } int main() { time counter; counter.show_time(); counter.tick_time(); counter.show_time(); return 0; }



LinkBack URL
About LinkBacks


