"Modern C + + program Design" (US) Barbara Johnston----2: Classes and objects

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.