"Who does this work" is important for designing classes and using objects.
Before you write a program, an important step is to ask yourself what classes you need, and what functions each class will perform separately.
Here are some of the previously used classes:
string processing of text data
Vector data List
Queue queues, first in, first out
Ifstream Open the data file and read the file
Ofstream opening a data file and writing to a file
To use these classes, you need to include the corresponding library files at the beginning of the program file, and then you can create objects to accomplish the specific work.
object is followed by a dot operator to access the definition functionality in the class
To write your own class:
class declaration:
Class Date
{
The member of the date class is declared here
Class members contain data and functions
};
C + + programs follow the Convention of saving the Declaration of a class in a header file (*.h) with the same class name , while the implementation code of the class member function is saved in *,cpp with the same names, such as Data.cpp.
Therefore, the code for a class is often saved in a pair of files, such as Date.h and Date.cpp. This can facilitate the reuse of code, as long as a class of two files added to the project can be used
The class has two different parts: private and public areas.
Class Date
{
Private: (keyword Private,public is an access control that determines the accessibility of out-of-class code to members within a class)
Data and Function prototypes
Public
Data and Function prototypes
};
Data declaration section:
Class Date
{
Private
Int month, day, year; Save Date numbers
string description; Save description Information
int dayofyear; Save the day of the week
BOOL Bleap; To indicate a leap year or not
}; When designing a class, you need to decide what data it needs to save, and in addition, there will be some local variables in the function member that are used only within the function.
Public
Here is the SET function to save the data to the object
void SetData (int m,int d,int y,string desc);
void Setdesc (string d) {description = d};
This get function returns a date string with a format
String GETFO ... ()
................
There are also two function prototypes for private areas of a class:
Class Date
{
Private
void ... ();
void: ()
}
"Modern C + + program Design" (US) Barbara Johnston----2: Classes and objects