---------------------------------------------------------------- // -- Дефиниция на клас абстрактни данни Time. Достъп до членовете на класа. // -- Инициализация на обекти от тип клас - конструктор и аргументи на // конструктор. // // TIME2.H // Declaration of the Time class. // Member functions defined in TIME2.CPP // prevent multiple inclusions of header file #ifndef TIME2_H #define TIME2_H class Time { public: Time(int = 0, int = 0, int = 0); // default constructor void setTime(int, int, int); void printMilitary(); void printStandard(); private: int hour; int minute; int second; }; #endif ------ // TIME2.CPP // Member function definitions for Time class. #include #include "time2.h" // Constructor function to initialize private data. // Default values are 0 (see class definition). Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); } // Set values of hour, minute, and second. // Invalid values are set to 0. void Time::setTime(int h, int m, int s) { hour = (h >= 0 && h < 24) ? h : 0; minute = (m >= 0 && m < 60) ? m : 0; second = (s >= 0 && s < 60) ? s : 0; } // Display time in military format: HH:MM:SS void Time::printMilitary() { cout << (hour < 10 ? "0" : "") << hour << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second; } // Display time in standard format: HH:MM:SS AM (or PM) void Time::printStandard() { cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second << (hour < 12 ? " AM" : " PM"); } ------- // FIG6_8.CPP // Demonstrating a default constructor // function for class Time. #include #include "time2.h" main() { Time t1, t2(2), t3(21, 34), t4(12, 25, 42), t5(27, 74, 99); cout << "Constructed with:" << endl << "all arguments defaulted:" << endl << " "; t1.printMilitary(); cout << endl << " "; t1.printStandard(); cout << endl << "hour specified; minute and second defaulted:" << endl << " "; t2.printMilitary(); cout << endl << " "; t2.printStandard(); cout << endl << "hour and minute specified; second defaulted:" << endl << " "; t3.printMilitary(); cout << endl << " "; t3.printStandard(); cout << endl << "hour, minute, and second specified:" << endl << " "; t4.printMilitary(); cout << endl << " "; t4.printStandard(); cout << endl << "all invalid values specified:" << endl << " "; t5.printMilitary(); cout << endl << " "; t5.printStandard(); cout << endl; return 0; } ------------------------------------------------------------------- //-- Деструктор. Кога се изпълняват конструкторите и деструкторите. // // CREATE.H // Definition of class CreateAndDestroy. // Member functions defined in CREATE.CPP. #ifndef CREATE_H #define CREATE_H class CreateAndDestroy { public: CreateAndDestroy(int); // constructor ~CreateAndDestroy(); // destructor private: int data; }; #endif ------------- // CREATE.CPP // Member function definitions for class CreateAndDestroy #include #include "create.h" CreateAndDestroy::CreateAndDestroy(int value) { data = value; cout << "Object " << data << " constructor"; } CreateAndDestroy::~CreateAndDestroy() { cout << "Object " << data << " destructor " << endl; } ------------- // FIG6_9.CPP // Demonstrating the order in which constructors and // destructors are called. #include #include "create.h" void create(void); // prototype CreateAndDestroy first(1); // global object main() { cout << " (global created before main)" << endl; CreateAndDestroy second(2); // local object cout << " (local automatic in main)" << endl; static CreateAndDestroy third(3); // local object cout << " (local static in main)" << endl; create(); // call function to create objects CreateAndDestroy fourth(4); // local object cout << " (local automatic in main)" << endl; return 0; } // Function to create objects void create(void) { CreateAndDestroy fifth(5); cout << " (local automatic in create)" << endl; static CreateAndDestroy sixth(6); cout << " (local static in create)" << endl; CreateAndDestroy seventh(7); cout << " (local automatic in create)" << endl; } ------------------------------------------------------------------ //-- Get и set член-функции. Помощни член-функции. // // TIME3.H // Declaration of the Time class. // Member functions defined in TIME3.CPP // prevent multiple inclusions of header file #ifndef TIME3_H #define TIME3_H class Time { public: Time(int = 0, int = 0, int = 0); // constructor // set functions void setTime(int, int, int); // set hour, minute, second void setHour(int); // set hour void setMinute(int); // set minute void setSecond(int); // set second // get functions int getHour() { return hour; } int getMinute() { return minute; } int getSecond() { return second; } void printMilitary(); // output military time void printStandard(); // output standard time private: int hour; // 0 - 23 int minute; // 0 - 59 int second; // 0 - 59 }; #endif ------------ // TIME3.CPP // Member function definitions for Time class. #include "time3.h" #include // Constructor function to initialize private data. // Calls member function setTime to set variables. // Default values are 0 (see class definition). Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); } // Set the values of hour, minute, and second. void Time::setTime(int h, int m, int s) { hour = setHour(h); minute = setMinute(m); second = setSecond(s); } // Set the hour value void Time::setHour(int h) { hour = (h >= 0 && h < 24) ? h : 0; } // Set the minute value void Time::setMinute(int m) { minute = (m >= 0 && m < 60) ? m : 0; } // Set the second value void Time::setSecond(int s) { second = (s >= 0 && s < 60) ? s : 0; } // Display military format time: HH:MM:SS void Time::printMilitary() { cout << (hour < 10 ? "0" : "") << hour << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second; } // Display standard format time: HH:MM:SS AM (or PM) void Time::printStandard() { cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << (minute < 10 ? "0" : "") << minute << ":" << (second < 10 ? "0" : "") << second << (hour < 12 ? " AM" : " PM"); } --------------------------------------------------- // FIG6_10.CPP // Demonstrating the Time class set and get functions #include #include "time3.h" void incrementMinutes(Time &, const int); main() { Time t; t.setHour(17); t.setMinute(34); t.setSecond(25); cout << "Result of setting all valid values:" << endl << " Hour: " << t.getHour() << " Minute: " << t.getMinute() << " Second: " << t.getSecond() << endl << endl; t.setHour(234); // invalid hour set to 0 t.setMinute(43); t.setSecond(6373); // invalid second set to 0 cout << "Result of attempting to set invalid hour and" << " second:" << endl << " Hour: " << t.getHour() << " Minute: " << t.getMinute() << " Second: " << t.getSecond() << endl << endl; t.setTime(11, 58, 0); incrementMinutes(t, 3); return 0; } void incrementMinutes(Time &tt, const int count) { cout << "Incrementing minute " << count << " times:" << endl << "Start time: "; tt.printStandard(); for (int i = 1; i <= count; i++) { tt.setMinute((tt.getMinute() + 1) % 60); if (tt.getMinute() == 0) tt.setHour((tt.getHour() + 1) % 24); cout << endl << "minute + 1: "; tt.printStandard(); } cout << endl; } ------------------------------------------------------------ //-- Оператор за присвояване (почленно копиране). // // fig06_12.cpp // Demonstrating that class objects can be assigned // to each other using default memberwise copy #include // Simple Date class class Date { public: Date( int = 1, int = 1, int = 1990 ); // default constructor void print(); private: int month; int day; int year; }; // Simple Date constructor with no range checking Date::Date( int m, int d, int y ) { month = m; day = d; year = y; } // Print the Date in the form mm-dd-yyyy void Date::print() { cout << month << '-' << day << '-' << year; } int main() { Date date1( 7, 4, 1993 ), date2; // d2 defaults to 1/1/90 cout << "date1 = "; date1.print(); cout << "\ndate2 = "; date2.print(); date2 = date1; // assignment by default memberwise copy cout << "\n\nAfter default memberwise copy, date2 = "; date2.print(); cout << endl; return 0; } ------------------------------------------------------------