|
 |
 |
[an error occurred while processing this directive]
- 5. Create functions
Did we make the program more readable? Did we save a few lines in the code? I doubt it. Whats the advantage of using structures?
The advantage will appear when we start to extend our program. Therefore, the next thing we are going to do is create functions in our program. The first function will enable the entering data, the second function will display dates, and the third function will calculate the next date based on a given date.
// incdate2.cpp//
The program increments a given date
// and displays the result
#include <iostream.h>
struct DDate{
int dday;
int mmonth;
int yyear;
};
DDate GetDate(void);
void DisplayDate(DDate);
DDate GetNextDate(DDate);
int main()
{
DDate ddate1, ddate2;
// Enter a base date
ddate1= GetDate();
// Calculate the next date
ddate2= GetNextDate(ddate1);
// Display the result
cout << " The next date after ";
DisplayDate(ddate1);
cout << " is ";
DisplayDate(ddate2);
cout << endl;
return 0;
}
// GetDate function gets the date into
// a DDate variableDDate GetDate(void)
{
DDate ddate;
cout << "Enter day (integer): ";
cin >> ddate.dday;
cout << "Enter month (integer): ";
cin >> ddate.mmonth;
cout << "Enter year (integer): ";
cin >> ddate.yyear;
return ddate;
}
// Displays a variable of DDate type
void DisplayDate(DDate ddate)
{
cout << ddate.dday << "/" << ddate.mmonth << "/" <<
⇒ddate.yyear;
}
// Calculates the next date
DDate GetNextDate(DDate ddate)
{
DDate ddateNext;
int DaysInMonth[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int LeapYear;
if ((ddate.yyear % 4) == 0) LeapYear = true;
ddateNext.dday = ddate.dday+ 1;
if ((LeapYear) && (ddate.dday == 29) && (ddate.mmonth == 2))
{ ddateNext.dday = 1; ddateNext.mmonth= 3;
⇒ddateNext.yyear = ddate.yyear;}
else if ((LeapYear) && (ddate1.dday == 28) &&
⇒ (ddate1.mmonth == 2))
{ ddate2.dday = 29; ddate2.mmonth= 2; ddate2.yyear =
⇒ddate1.yyear;}
else if (ddateNext.dday > DaysInMonth[ddate.mmonth - 1])
{
ddateNext.mmonth = ddate.mmonth+ 1;
ddateNext.dday = 1;
}
else
ddateNext.mmonth = ddate.mmonth;
if (ddateNext.mmonth == 13 )
{
ddateNext.mmonth = 1;
ddateNext.yyear = ddate.yyear + 1;
}
else
ddateNext.yyear = ddate.yyear; return ddateNext;
}
Now we can say that we created our own data type: DDate. We can define variables of this data type, use this data type in function definitions, and we can pass parameters of this data type to the functions.
In fact, as long as we work with the main program we even dont need to know what is included in this data type.
- 6. Using the assignment (=) operator
A big advantage of using structures is that we can assign one variable of a new type to another variable of the same type just by writing
a = b;
where a and b are variables of the same structure. For example, lets write a program that takes a DDate variable, copies it to another variable, and displays both variables. We are going to use GetDate and DisplayDate functions from the previous example.
// assignd.cpp
// The program increments a given date
// and displays the result
#include <iostream.h>
struct DDate{
int dday;
int mmonth;
int yyear;
};
DDate GetDate(void);
void DisplayDate(DDate);
int main()
{
DDate ddate1, ddate2;
// Enter a base date
ddate1= GetDate();
// copy ddate1 to ddate2
ddate2= ddate1;
// Display the result
cout << "The first date is ";
DisplayDate(ddate1);
cout << endl;
cout << "The second date is ";
DisplayDate(ddate2);
cout << endl;
return 0;
}
// GetDate function gets the date into
// a DDate variable
DDate GetDate(void)
{
DDate ddate;
cout << "Enter day (integer): ";
cin >> ddate.dday;
cout << "Enter month (integer): ";
cin >> ddate.mmonth;
cout << "Enter year (integer): ";
cin >> ddate.yyear;
return ddate;
}
// Dispalys a variable of DDate type
void DisplayDate(DDate ddate)
{
cout << ddate.dday << "/" << ddate.mmonth << "/" <<
⇒ddate.yyear;
}
Comments
Creating a C++ structure creates a new data type that can be accessed by its own name, such as DDate in the previous example. When declaring a new structure, you dont have to use the struct keyword as in C. The individual elements of a structure are called structure members and can be accessed by the structure member (.) operator (dot operator).
Structures are very helpful if you want to logically combine different variables to use them as a set. They are similar to arrays in the sense that both arrays and structures are sets of variables stored under the same name. However, arrays are aggregates of the elements of the same nature and therefore the same data type. Structure members usually represent data of a different nature and can have different types.
You can create many instances of the same structure just as you can create many variables of the same type. You can assign objects of structure types to other objects of the same type, you can pass them as function parameters, or return them as a function result.
4.2 Hide my data from external programs?
Problem
I want to create functions that use my own data types. For example, I often use stacks and I want other modules working with my module not to have the ability to change my implementation of stacks. In other words, I want my implementation to be hidden from external modules.
Technique
In C++, you can create your own data type (such as a stack or a queue) and specify operations on this data type. To make the specification of the data type complete, you can hide all internal information in a class.
|