C++primer Learning Notes------objects and classes (1)

Source: Internet
Author: User

I. Basic knowledge of related classes


1. Type

Think about what criteria we usually use to determine the data type of a data. There are two, the first is how the data is stored, such as char stands for itself, int accounts for four, and the second is what this type can do. For example, integer data can be used to do the remainder operation, but the floating-point number can not.

So when defining a variable, we usually do the following:

(1) Decide which memory to allocate for this type of variable

(2) Decide how to interpret the bits in memory, such as when defining an array, in the array index + 1 o'clock, in the end is to move a few memory units, based on the data type of the array element is determined.

(3) determines the method by which this variable can be used.


PS: About object-oriented, about the concept of class, I always feel that this is a higher level of abstraction, in fact, we now use the operating system is already abstract. Why the use of the threshold of the computer is getting lower, because the level of abstraction is getting higher, if a few decades back, then the programmer is to use sinks to write software, directly and hardware to deal with, where there is what the bullshit vs compiler this thing. Therefore, the concept of object-oriented and class in C + + is the same, it makes a packaged operation on all the properties and methods of a real entity, in which he organizes some data to provide interfaces to external users. Before you use this class, there is only the function of this class on the instruction manual, which function is called, which does not tell you how the function is implemented, thus reducing the number of errors in the programming process. Just like our daily life, we don't know how the things we eat are digested internally, but we know that as long as we want to eat, we have to use the mouth of the "interface", the truth is the same right.


2. Class

A class usually has two parts, part is the declaration of the class, and the other part is the definition of the class, in fact the definition of the class is mainly the definition of the method of the class.


3, Class of access control

There are basically three types of access rights for a class, public, private, and protected. A common part can be accessed by external objects, the protected part can only be accessed by the quilt class, the private part can only be accessed by the internal of the class itself, the rest of the place is inaccessible.


4. About member methods of classes

The member method of a class has two characteristics, the first is declared within the class, implemented outside of the class, but the implementation has to identify what class the member function belongs to in the outside of the class, so a class that uses the scope identifier to specify which member function the implementation belongs to.

Why do I have to qualify with a scope identifier, because the function inside the class is also scoped, and this scope is called the scope of the class. If two classes define an update function with the same name at the same time, then two functions can be used inside the class without the scope identifier, and even if the same name does not affect each other, because their scopes are different, but outside the class, it is not their scope. They need to be clearly defined where and where they belong. A private member of a class can be accessed in a member method of a class, because only functions within the class can access it.


5. About inline functions

Inline functions have been mentioned in the previous blog post, which is simply to add the keyword "inline" at the top of the function definition when the function is defined, and wait until the next time it is called, to copy the code in the past without making the sequence of normal function calls. In C + + classes, functions defined in the declaration of a class are the default inline functions, because the declaration of a class is usually in the header file, and the function defined in the life of the class must be the more dapper, otherwise it violates the principle of the inline function. If you are still ready to declare the function prototype in the class, the outside of the class is defining the function, then the keyword "inline" will be added to the definition.

The special rules of the inline function determine that he must have a corresponding definition in each file used by him, so the best way is to put the inline function in the function's header file, each referencing the header file.


6. Methods & Objects

As we all know, an object in a class can call some member methods in a class outside the class, and only one member method, so when two objects call a method, how does this method determine which object the data in the function belongs to?

Very simple, the method that the object calls, then the data in this method is the object that belongs to, each object when the method is called, there will be a corresponding this pointer to the object is passed the progressive function. So inside the function, the corresponding data of this object can be manipulated by the way of this-> property.


As we all know, C + + as far as possible to change the object into a normal variable for us to use, every time we create a class object, the whole object will have its own storage space, then a class can create many objects, if these objects are executed a member method, then this member method is one of each object? No, there is only one member method in a class, and there can be many objects, and several objects execute the same piece of code when they call the same member method, except that the data is executed differently.



PS: Above is basic knowledge, so come to a small summary, first sent out, to everyone to see.

To use a class, you first define a class, which is a complete description of the class you want to create, and some implementation details in the specific class do not have to worry too much when declaring the description. The first thing to do is to write a declaration of a class. In general, some data members of a class are placed in the private section, and some member methods are placed in the public domain as the public interface of the entire class, which is also an abstract part of the class design. The data encapsulation to the private part guarantees the concealment of the data, the member method of the public part implements the abstract part of the public interface, and the implementation details of the member functions are encapsulated.

When declaring a class, the next step is to implement some of the member functions declared in the class outside of the class. In addition to this particular case of inline functions, the rest of the functions need to be implemented in a concrete implementation by adding scope identifiers outside the class.

For the function name of any class member function, there is a class name inside, that is, the function name of the member function in the class, not only include our own definition, but also add the class name, after all, this function belongs to this class. A class used to define, in fact, a user-defined type, so that when the object is defined directly, the name of the class name object this form, it is clear that the class name as the type of view.








C++primer Learning Notes------objects and classes (1)

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.