C++primer Seventh class (related definition of Class)

Source: Internet
Author: User
Tags define abstract

The basic idea of a class: data abstraction and encapsulation. Data abstraction is a programming technique that relies on interfaces and implements separation. The interface of a class includes the actions that the user can perform, and the implementation of the class includes the data members of the class, the function body responsible for the implementation of the interface, and the various private functions required. Encapsulation implements the interface of the class and the implementation of the separation (in their own words, is the encapsulation of the implementation of the class is hidden, others have been the implementation of the function to help you complete, you just need to call directly).

1. Define abstract data types:

Class Scope: The class itself is a scope.

The compiler has two-step processing classes: the declaration of the member is compiled first, and then the member function body.

A. Define the Read and print functions:

Read function: Reads the data from the given stream into the given object; (IStream input stream, read and write operations change the content in the stream, which is generally defined as a normal reference)

For example:

Sales_data::sales_data (IStream &is)

{

the role of the Read (Is,*this)//read function is to read a trade message from IS and then deposit it in the This object

}

Print function: is responsible for printing the contents of a given object into a given stream; (Ostream output stream, printing content, which can be defined as a constant reference)

B. Constructors

Definition of a constructor: A class controls the initialization of an object through one or more special member functions;

Usage: The constructor has the same name as the class name; it has no return type; (also Supplement the use of destructors: Same as the class name, just precede the class name with the ~ symbol)

Default constructor: The previous section also mentions that the class controls the default initialization process through a special constructor (the default constructor does not require any arguments)

Default constructor Usage:

For example:

Sales_data () =default;

is to add =default after the constructor with no arguments;

the only purpose of the constructor: assigns the initial value to the data member;

2. Access Control and encapsulation

Access to public and private in a class, PUBILC access: Members of a class can be accessed throughout the program, private access: can be accessed by members of the defined class, or accessed by friend functions. (Chapter 15 will be explained in detail later)

Class differs from struct (the only difference): The default permissions are different. The members of the class default class are private, and the members of the struct default class are public.

Friend: It can access the non-public members of the class, thereby increasing the access rights of the function.

Usage: Add friend in front of the function declared in the class (friend declaration)

Advantages of encapsulation: one is to ensure that user code does not inadvertently break the state of the encapsulated object. The second is that the specific implementation details of the encapsulated class can be changed at any time without the need to adjust the user-level code.

3. Scope of the class

A. Name lookup and scope of the class

Name lookup: First, look for its declaration statement in the block where the name resides, and only consider the declaration that appears before the name is used. If it is not found, continue to look for the outer scope, or the program will error if no matching claims are found.

B. Type name to be handled specially: in a class, if a member uses a name from the outer scope, and the name represents a type, the class cannot redefine the name later.

Example:

typedef double Money;

Class account{

Public

Money balance () {return bal;} //using the outer scope of money;

Private

typedef double Money; //Error: Money cannot be redefined;

Money Bal;

};

4. Further exploration of the constructor function

A. Constructor initializer list

In terms of data members of an object, there are similar differences between initialization and assignment. If the member is not initialized in the initial value list of the constructor, the member performs the default initialization before the constructor is complete.

If a member is a const, a reference, or a class type that does not provide a default constructor, we must provide the default initial values for these members by constructing a list of function initializers.

B.explicit constructor Function

Explicit is valid only for constructors of one argument. It can only be used for direct initialization;

5. Static members of a class

The definition of a static member of a class: use static to declare a member function, a static data member that does not belong to any object of the class, similar to a global variable (which is not owned by an object and belongs to the entire class).

Pros: First, avoid conflicts with other class members or global object names, and second, implement encapsulation.

The difference between a static member and an ordinary member:

One is that the type of a static data member can be the class type to which it belongs, while non-static data members are restricted and can only be declared as pointers or references to the classes to which they belong;

The second is that the static member can be the default argument;

Summarize c++11 features:

1. Use of the default constructor =default

(explained in detail earlier;)

2. Delegate constructors

Definition: A delegate constructor performs its own initialization process using the other constructors of the class it belongs to, (delegating its responsibilities to other constructors)

Example:

Class sales_data{

Public

Sales_data (String s,unsigned cnt,double price);

Bookno (s), Unit_sold (CNT), Revenue (Cnt*price) {}

//The remaining constructors are all delegated to another constructor

Sales_data (): Sales_data ("", 0,0) {}

Sales_data (String s): Sales_data (s,0,0) {}

Sales_data (IStream &is): Sales_data ()

{Read (is,*this);}

};

When a constructor is delegated to another constructor, the list of initial values and the body of the constructor of the delegate are executed sequentially.










































































C++primer Seventh class (related definition of Class)

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.