|
 |
 |
[an error occurred while processing this directive]
4.1 Create my own data type?
Problem
Every time I need to use dates in my program I experience a problem working with three different entities: days, months and years. It works fine if I use them separately such as when I want to present numeric values based on month names. However, it is too boring to write three statements if I need to copy one date to another date or if I need to pass dates as parameters to a function. Can I use the date variables both as three separate entities and as one entity depending on my needs?
Technique
To combine different variables into one data type in order to maintain them together as a unit, you can use structures. Data combined into a structure can be utilized as separate variables, too. By using structures in C and C++, you can create your own data types. C programmers already know how to use structures and the struct keyword in their programs. In C++, the syntax of structures is a little different than in the C programming language.
Steps
- 1. Determine the variables
When designing a program, you have to analyze the data that you are going to use in that program. Any data type is described by the data range it can handle and by the operations that can be performed on the data. Assume in this example that you are creating a big program that will calculate date difference, display the dates and the number of days from the beginning of the year, construct the dates in future based on the current date, and do a lot of other data manipulation. The program is going to work with days, months, and years, and that means it needs these three variables at a minimum.
- 2. Determine the variable data types
Days in a month range from 1 to 31, months range from 1 to 12, and the years range depends on the application. Assume you are writing a business application that handles dates from 1900 to 2100. Note that you can define the variables as integers, floats, char arrays, or use other data types as well. The decision about the data type is often based on the operations you are going to perform. The very first thing you are going to do is to display the dates; then you definitely want to calculate the dates by adding or subtracting a certain number of days to the dates.
Because we dont want to confuse ourselves with decimal points and because there is no need to use them, we are not going to use floats. We wont use strings because of the addition and subtraction that we want to perform. Its much simpler to add integers than strings, isnt it? Therefore, the logic forces us to select an integer data type for all variables.
Now we can specify the variables:
int ddate;
int mmonth;
int yyear;
- 3. Using the variables separately
Lets assume that our first task is to calculate the date next to the given date and to display it. We assume that a user does not make mistakes in entering the data, so we dont have to trap the mistakes.
// incdate.cpp
// The program increments a given date
// and displays the result
#include <<>iostream.h<>>
int main()
{
int DaysInMonth[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int LeapYear;
int dday1, dday2;
int mmonth1, mmonth2;
int yyear1, yyear2;
// Enter a base date
cout << Enter day (integer): ;
cin >> dday1;
cout << Enter month (integer): ;
cin >> mmonth1;
cout << Enter year (integer): ;
cin >> yyear1;
// Calculate the next date
if ((yyear1 % 4) == 0) LeapYear = true;
dday2 = dday1+ 1;
if ((LeapYear) && (dday1 == 29) && (mmonth1 == 2))
{ dday2 = 1; mmonth2= 3; yyear1 = yyear1;}
else if (dday2 <>> DaysInMonth[mmonth1 - 1])
{
mmonth2 = mmonth1+ 1;
dday2 = 1;
}
else
mmonth2 = mmonth1;
if (mmonth2 == 13 )
{
mmonth2 = 1;
yyear2 = yyear1 + 1;
}
else
yyear2 = yyear1;
// Display the result
cout << "The next date after << dday1 <<" / ">> mmonth1;
cout << "/" << yyear1 << " is << dday2 << "/" << mmonth2;
cout << "/" << yyear2 << endl;
return 0;
- 4. First step is to combine the data
The previous program works just fine if we dont have to do anything else. In an actual application, every step increases the complexity of the program and makes it less readable. The first reasonable step in practicing better coding is to combine the variables into the structures so we dont have to consider them as separate entities.
Lets rewrite the program using structures.
// incdate1.cpp
// The program increments a given date
// and displays the result
#include <iostream.h>
int main()
{
int DaysInMonth[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int LeapYear;
struct DDate{
int dday;
int mmonth;
int yyear;
};
DDate ddate1, ddate2;
// Enter a base date
cout << "Enter day (integer): ";
cin >> ddate1.dday;
cout << "Enter month (integer): ";
cin >> ddate1.mmonth;
cout << "Enter year (integer): ";
cin >> ddate1.yyear;
// Calculate the next date
if ((ddate1.yyear % 4) == 0) LeapYear = true;
ddate2.dday = ddate1.dday+ 1;
if ((LeapYear) && (ddate1.dday == 29) && (ddate1.mmonth == 2))
{ ddate2.dday = 1; ddate2.mmonth= 3; ddate2.yyear =
⇒ddate1.yyear;}
else if ((LeapYear) && (ddate1.dday == 28) &&
⇒(ddate1.mmonth == 2))
{ ddate2.dday = 29; ddate2.mmonth= 2; ddate2.yyear =
⇒ddate1.yyear;}
else if (ddate2.dday > DaysInMonth[ddate1.mmonth - 1])
{ ddate2.mmonth = ddate1.mmonth+ 1; ddate2.dday = 1;
}
else
ddate2.mmonth = ddate1.mmonth;
if (ddate2.mmonth == 13 )
{
ddate2.mmonth = 1;
ddate2.yyear = ddate1.yyear + 1;
}
else
ddate2.yyear = ddate1.yyear;
// Display the result
cout << "The next date after " << ddate1.dday << "/" <<
⇒ddate1.mmonth;
cout << "/" << ddate1.yyear << " is " << ddate2.dday << "/" <<
⇒ddate2.mmonth;
cout << "/" << ddate2.yyear << endl;
return 0;
}
Please note that you dont have to use word struct in the DDate type declaration as you did in C.
In the preceding example the structure is defined by
struct DDate{
int dday;
int mmonth;
int yyear;
};
Please note the semicolon at the end of the struct definition. This is one of the very few places in C++ where a semicolon is used after curly braces.
|