Course List http://www.c-jump.com/bcc/
Consider a program that uses dates:
#include <iostream> int main() { std::cout << "3/8/2010" << '\n'; std::cout << "7/20/2011" << '\n'; return 0; }
Hardcoded dates such "3/8/2010" aren't very useful.
We want programmatic control of dates:
Display dates
Input dates from a user
Store dates in a file or a database
Perform date calculations and comparisons
Use calendars, determine weekdays, holidays, etc.
Conversion functions such as serial Julian date can help:
#include <iostream> int serial_julian_date( int Month, int Day, int Year ); int serial_2_month( int nDate ); int serial_2_day( int nDate ); int serial_2_year( int nDate ); int main() { int month = 3; int day = 8; int year = 2010; // Compute serial date int serial_date = serial_julian_date( month, day, year ); // Modify serial date: serial_date = serial_date + 100; // add 100 days month = serial_2_month( serial_date ); day = serial_2_day( serial_date ); year = serial_2_year( serial_date ); // Display new date in human-friendly format: std::cout << month << '/' << day << '/' << year << '\n'; return 0; }
Functions such as
int serial_julian_date( int Month, int Day, int Year ); int serial_2_month( int nDate ); int serial_2_day( int nDate ); int serial_2_year( int nDate );
perform nice job converting to and from different date formats.
Since we found a way to replace "3/8/2010" with int representation, making calculations and comparing dates becomes easy:
serial_date = serial_date + 100; // add 100 days if ( serial_date < today ) { // compare dates // the date is in the past ("less than today") // ... } int age = serial_2_year( today ) - serial_2_year( serial_date );
However,
Program contains too many small steps.
Program requires up to 4 variables to handle each date: month, day, year, and serial_date.
No protection of any kind exists to prohibit illegal date modifications:
int month = 2; int day = 31; // the day is outside of allowable range for February int year = 2010;
int main() { Date birthday; birthday.month = 3; birthday.day = 8; birthday.year = 2010; std::cout << birthday << '\n'; }
Benefits: all logic is done around a single variable named birthday.
Questions:
What is Date?
What is birthday.month?
What happens here: std::cout << birthday?
Each C++ built-in type is defined by its size and a set of supported operations:
|
|
|
A program is a set of variables and functions.
Variables store data.
Functions define computer operations.
Functions provide convenient way to encapsulate various computations.
|
|
C++ programmer describes user-defined types using keyword class:
#include <iostream> class Date { // member variables int serial_date; public: // member functions void set_date( int month, int day, int year ); int get_month(); int get_day(); int get_year(); }; int main() { Date birthday; // instantiate object of type "Date" birthday.set_date( 2, 8, 1999 ); // member function calls std::cout << birthday.get_month(); return 0; }
The implementation of Date class uses previously defined serial date conversion functions:
#include <iostream> int serial_julian_date( int Month, int Day, int Year ); int serial_2_month( int nDate ); int serial_2_day( int nDate ); int serial_2_year( int nDate ); class Date { // member variables int serial_date; public: // member functions void set_date( int month, int day, int year ); int get_month(); int get_day(); int get_year(); };
C++ allows operator syntax for user-defined types:
#include <iostream> int serial_julian_date( int Month, int Day, int Year ); int serial_2_month( int nDate ); int serial_2_day( int nDate ); int serial_2_year( int nDate ); class Date { // member variables int serial_date; public: // member functions void set_date( int month, int day, int year ) {} int get_month() { return 2; } int get_day() { return 8; } int get_year() { return 1999; } }; void operator<<( std::ostream&, Date dt ) { std::cout << dt.get_month(); std::cout << '/'; std::cout << dt.get_day(); std::cout << '/'; std::cout << dt.get_year(); } int main() { Date birthday; // instantiate object of type "Date" birthday.set_date( 2, 8, 1999 ); // member function calls std::cout << birthday; return 0; }
C++ program is a set of:
variable definitions and
functions
Finally, another look at Hello, world! program:
#include <iostream> int main() { // Statement using operator notation: std::cout << "Hello, World!"; // Statement using function call: operator<<( std::cout, "Hello, World!" ); return 0; }