Classes and objects (upper) C ++

This section is a preliminary understanding of the class, familiar with the following:
1. What is the class? What is an object? Object-oriented and process-oriented difference?
2. How to define a class? Struct is defined by the difference between class and class with the class definitions?
3. C ++ how to implement the package?
4. Class of scope: how to understand the definition of a class, you define a scope?
The class is instantiated: Why do we need to instantiate the class before they can store content?
6. How to get the size of a class? How much space the size of class time, and why? Added: What is memory alignment structure? Why do we need memory alignment? How to set alignment parameters? Know how to structure a starting position offset with respect to the structure of the? What is the size of the end? Under what scenario requires taking into account the size of the end? How to verify?
7. What is the this pointer? this pointer characteristics? this pointer can empty it? this pointer is empty, the code must collapse under what circumstances?

1. What is the class? What is an object?

Categories: type (category) of things with common characteristics formed. The common properties of things have certain common attributes summed up, the result is these kinds of things (category). As described students, student number, name, age, sex, grades, etc. properties, which are summarized together, is the student class. In C ++, the class has attributes describe things (data elements), as well as initialization and assignment embodiment of the data elements, and the activities can be class (the class is achieved by the performance function), i.e., the class data elements, there are functions, or even class there are classes, such as students in the class can also have a class cadre class.

Object: specific things people studied, such as a student, or other people and things can be an object. It is the object class is instantiated.

Relationship classes and objects: class instantiation result is an object. The abstract description of the object class. The mold type, the object is a casting, the mold tool is out of the object.

Process-oriented and object-oriented What is the difference?

Process-oriented: the development of things to describe things, the development steps from event to event analysis, analyze what steps to resolve the incident (problem) needs, and then implement these steps. He emphasized that the step to define the code embodied in the plurality of functions, and the step of press calls these functions.

Object-oriented: to break the problem into multiple objects from the object involved in the event to event analysis, analysis of the object involved in the event which, what behavioral characteristics (function) have stressed that the behavioral characteristics of objects and their events. Is embodied in the code declaration defines related classes, define attributes (data elements) object class in the class, the behavior described by the function (function), then instantiate a subject in need thereof, by the interaction between the object and the object to solve the problem.

2. How to define a class? Struct is defined by the difference between class and class with the class definitions?

Defining classes: class is the class definition keyword (struct class may be defined), className is the class name, class {} is the body, the body can be declared a friend, defined functions, define the elements, at the end of semicolons .

class className     // class is the class keyword, classname is the class name 
{
       // body parentheses class, members of the class definition 
};    // end semicolon and no less 

// define twenty-one student class 
class Student 
{ 
    int Id [ 15 ];
     char name [ 10 ];
     int Age;
     int Gender; 

};

 

C ++ classes in strengthening the C language structure, so c ++ is class actually structure, but the structure in C there is a clear distinction, the structure of the C language can only define a variable, but in C ++ structure body can define a variable, the function may also be defined.

Classes in C ++, struct defined elements are shared by default, external access to, and class elements defined default class is private, there is no external access (except friend). (C ++ class definitions like to use in class). In C, the structure can only define variables.

 

3. C ++ how to implement the package?

Packaging: The method of operation of data and an organic binding data, the hidden object properties and implementation details, only publicly interface and interact with the outside world. It is essentially a management. (Such as when we surf the Internet, our network name is public property, other people can see, but we generally do not see the real names of other people, we are the information real name, ID number and other protected, not public, the screen name public, information management) combines with class attributes and methods of the object, so the object more perfect. Use access qualifier to access to a predetermined, selectively be provided to users through external access to their interfaces. There are three access qualifiers: 1, public (public) modified members can be accessed directly outside the class. 2, protected (protection) and private (proprietary) modified members can not be accessed directly outside the class. Scope access starting from the position of the access qualifier arise, to appear next visit qualifier position. Class default access class is declared private, class visits the default file permissions struct declared as public, because struct to be compatible with C. Can access an external element structure C. Access modifier is only useful at compile time, mapping data into memory, there is no difference access qualifier. That is over a compile phase, access qualifier is not at work.

class Student 
{ 
public : 
    Student () 
    {} 
    int A = . 5 ;
 protected : 

Private :
     char Id [ 15 ];
     char name [ 10 ];
     char Gender;
     int Age = 18 is ; 
}; 

int main () 
{ 
    Student A; 
    the printf ( " % d \ the n- " , aA); // normal access
     // printf ( "% d \ the n-", a.Id); // will complain, a.Id inaccessible 

    sizeof(a);
    return 0;
}

 

4. Class of scope: how to understand the definition of a class, you define a scope?

Is a class method (function) describes the collection of attributes (data elements) and some type of object, these properties and methods only work in a class, a member of the class to be used outside the class, need scope resolution symbol indicates that the members belong to the class domain.

The class is instantiated: Why do we need to instantiate the class before they can store content?

Creating an object class using a process called class instantiation. Class is to design drawings, the definition of a class, is to design some drawings, we can not say there has been drawing product, like castles in the air, and can not really draw a pie. Only really make a pie to pie with meat and vegetables in. (Shaanxi go to school but do not really like to eat Hamburger (QAQ)). Define a class is not actually allocate memory space to store the class, the line type int, char, etc., are used to describe the characteristics of a class of objects or data. Only the class to instantiate an object, it allocates physical storage space, you can add real data for each object in the object.

6. How to get the size of a class? One

The size of a class is the class member variable and the sum of the sizes, attention memory alignment.

How much space the size of class time, and why?

Empty class size is a byte. The compiler to a null byte space to uniquely identify the class.

What is memory alignment structure?

In the structure, the size of the member variable to different types, according to certain rules to open up space for storage, the member variables to store a certain size law. Default alignment member variables in the structure, according to the order set, a variable number of bytes is equal to or a multiple of an integer, the sum of all the variables size, are adjusted to minimize the maximum number of type byte integer multiples.

Memory Alignment alignment values ​​on several concepts:

1, self-alignment value basic types: the size of the system built-in types, such as the number of bytes int char and other types is their own type alignment value.

2, alignment values ​​custom type: members of the internal structure, the type of the maximum number of bytes that custom type alignment value.

3, the program specified alignment value: By using #pragma pack (n) th to manually specify the alignment of size n if the value is 2, and the like, such as 1,2,4,8.

4, the effective alignment values: When memory alignment, the alignment specified default values ​​are compared with values ​​aligned program, who who is small effective alignment value.

Why do we need memory alignment?

1, different reading and writing data on different platforms manner, for compatibility, memory alignment needs to be done.

2, improve efficiency, not aligned memory, the processor to do work to access the two visits, and aligned memory, the processor is only used when accessing a job.

How to set alignment parameters?

Program specified alignment value: By using #pragma pack (n) to manually specify the alignment of size n if the value of the power of 2, such as 1,2,4,8 like.

Know how to structure a starting position offset with respect to the structure of the?

The offset structure: the structure of the body of a member variable with respect to the offset value of the address structure. (My own understanding is that the difference between the starting address and the address of the structure of the member variables). Find the starting address offset is subtracted by the address structure of the member variables.

 

What is the size of the end?

Big endian: high byte of data stored in the lower memory address of the lower byte data is stored in the upper memory address in. Memory address increases from left to right, our reading habit is big-endian mode. 12345 (thousands hundred ten).

Little-endian mode: the high byte stored in high address memory, the low byte data stored in the lower memory address. The low weight combined with the address, high address part weights, low weight small part of the address. The way the ancients read from right to left. Such as (a Shibaiqianwan) four hundred fifty 32,001,000 (54321).

Under what scenario requires taking into account the size of the end?

When you view the data stored in the memory, we will read the data were necessary to consider reducing the size of the end of the combination.

How to verify? As a simple authentication, as follows

void CheckMode () 
{ 
    int A = 0x1234 ;
     char B = * ( char *) & A ;
     IF (B == 0x12 ) 
    { 
        the printf ( " big endian mode \ n-. " ); 
    } 

    the else 
        the printf ( " . little endian \ n- " ); 

} 

int main () 
{ 
    CheckMode (); 
    return  0 ; 
}

 

7. What is the this pointer?

this is a keyword in c ++, it is a const pointer modification. Is a default parameter in a member function, the function calls the receiving object. It points to the current object, the calling object member functions, access to the member variables and member functions of the current object through it.

this pointer characteristics?

Do not pass argument, const pointer modified, the default parameter object member function, not written in the code, point to the current object, have access to the current object through it. It can only be done within the function used in the function, not the object. Passing by way of the push compile time. You can no longer use the static function.

this pointer can empty it? this pointer is empty, the code must collapse under what circumstances?

May be empty, when not operated by this pointer, this pointer may be null. But if you need to use this refers to the procedure for the object, an error occurs.

Guess you like

Origin www.cnblogs.com/fengkun/p/11279857.html