C + + enlightenment classes in C + +

Source: Internet
Author: User
Tags class definition header

Before we solve our bookstore problem, the only remaining problem we need to understand is how to write a data structure (data structure) to represent our transactional data. In C + + We define our own data structure by defining classes (Class). Class mechanism is one of the most important features in C + +. In fact, the main focus of C + + design is to enable us to define the class type (class types) so that its operations are as natural as the built-in type. We've seen class library types (such as IStream and ostream) that are defined as classes-that is, they are not strictly part of the language.

A complete understanding of the class mechanism requires a large amount of information to be mastered. Luckily, we can use classes written by other people without knowing how we define classes. In this section, we'll introduce a simple class that we can use to solve the bookstore problem.

To use a class, we have to know three kinds of information:

1. What is the name of it?

2. Where is it defined?

3. What operations does IT support?

For our bookstore problem, we assume that this class is called Sales_item, which is defined in a header file called Sales_item.h.

1, Sales_item class

The purpose of the Sales_item class is to store an ISBN and track the sales volume, revenue, and average selling price of the book. How these data are stored and computed is our concern. To use a class, we don't have to know the details of its implementation. Instead, we just need to know what operations it supports.

We've seen that when we use a class library tool like Io, we have to include the associated header file. Similarly, for our own classes, we must also let the compiler know the definition associated with the class. The operation that implements such a function is the same as the previous operation. Typically, we put the class definition in a file. Any program that wishes to use our class must include this file.

By convention, class definitions are stored in a file similar to the name of the program source file, which has two parts: file name and extension. Usually the file name is the same as the name of the class. The extension is usually. h, but some programmers use. h,. hpp or. hXX. compiler generic header file name is not very picky, but some Ides are picky. We assume that our classes are defined in the Sales_item.h file.

Related Article

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.