Detailed knowledge of the member variables and member functions of classes in C + + programming _c language

Source: Internet
Author: User
Tags class definition function definition function prototype

member variables and member functions for C + + classes
A class is a data type, similar to a normal data type, but distinct from a normal data type. A class is a data type that contains a collection of member variables and member functions.

Class has the same data type and name as a normal variable, and occupies a fixed length of memory space. However, you cannot assign a value to a member variable when you define a class, because the class is simply a data type that does not occupy the memory space itself, and the value of the variable requires memory to store.

The member function of a class is also the same as a normal function, has a return value and a parameter list, which differs from a general function in that a member function is a member of a class that appears in the class body and whose scope is determined by the class, and the normal function is independent, the scope is global, or is within a namespace.

In the last section we gave the definition of the Student class in the final complete example, as follows:

Class student{public
://classes contain variable
  char *name;
  int age;
  float score;
Public://class contains function
  void Say () {
    printf ("%s is of%d age, score is%f\n", name, ages, score);
  }
;
The above code defines a member function in the class body. You can also declare functions only in the class body, and the function definition outside the class body, as shown in the following figure:
class student{public
:
  Char *name;
  int age;
  float score;
Public:
  void Say ();//function declaration
};
function definition
void Student::say () {
  printf ("%s's age is%d, the result is%f\n", name, aging, score);
}

When you define a function directly in a class body, you do not need to precede the function name with the class name, because it is self-evident which class the function belongs to.

However, when a member function is defined outside the class, it must be qualified with the class name before the function name. :: is called a domain parser (also known as the scope operator or scope qualifier) to connect the class name and function name, indicating which class the current function belongs to.

If the class name is not preceded by the domain parser "::" or the function name is preceded by a class name and no domain Parser "::", such as:

No class name
:: Say () {
  //todo
}
//No class name also no domain parser
say () {
  //todo
}


It means that the say () function does not belong to any class, and this function is not a member function, but a global function, that is, a generic function of a non-member function.

A member function must first be declared in the class body, and then defined outside the class, that is, the position of the class body should precede the function definition, or else it will be wrong at compile time.

Although a member function is defined outside of a class, the function is executed by finding the definition (function code) of the function based on the function prototype declared in the class.
Inline member functions

There is a difference between defining a member function in the class body and the class body: the member function defined in the class body is an inline (inline) function that is not defined outside the class body.

Inline functions are generally not what we expect, and it replaces function calls with function bodies, so I recommend that you declare the member functions within the class body and define them outside of the class body, which is a good programming practice.

Of course, if your function is shorter and wants to be defined as an inline function, there is nothing wrong with that.

If you want to define a function outside the class body and want it to be an inline function, you can add the inline keyword when declaring the function, as follows:

Class student{public
:
  Char *name;
  int age;
  float score;
Public:
  inline void Say ();//declared as inline function
};
function definition
void Student::say () {
  printf ("%s's age is%d, the result is%f\n", name, aging, score);
}

In this way, say () becomes an inline function.
functions defined within the class body can also be added to the inline keyword, but this is superfluous, because the function defined within the class body is by default an inline function.
It is noteworthy that if you define the inline function outside of the class body, you must place the definition of the class definition and the member function in the same head file (or in the same source file), otherwise it cannot be embedded at compile time (the function code is embedded into the function call out). While this improves the execution efficiency of the program, it is not a good idea from the point of view of software engineering quality, so less inline functions are used in the actual development of the class.

The main intention of C + + to propose inline functions is to replace the parameter macro definition with inline functions (function parameters are easier to use than macros), rather than to improve program efficiency, because it takes less time to invoke a function than it takes to execute a function.

How C + + member functions are stored
When you define an object with a class, the system allocates storage space for each object. If a class includes data and functions, you assign storage space to the code for the data and functions, respectively.

Logically, if you define 10 objects with the same class, you need to allocate the storage units for data and function codes of 10 objects, as shown in the following illustration.

Can you just use a space to store this common function code snippet, and call the functions of each object to invoke the public function code. As shown in the figure.

Obviously, this can save a lot of storage space. The C + + compilation system does this, so each object occupies only the storage space occupied by the data portion of the object, not the storage space occupied by the function code. If a class is declared:

Class time
{public
  :
  int hour;
  int minute;
  int sec;
  void Set ()
  {
   cin>>a>>b>>c;
  }}
;

You can use the following statement to output the number of bytes occupied by the class object:

  Cout<<sizeof (Time) <<endl;


The output value is 12.

This proves that the size of an object's space depends only on the space occupied by the data members in that object, regardless of the member function.

The function code is stored outside the object space. If you define 10 objects for the same class, the member functions of these objects correspond to the same function code snippet instead of 10 different function snippets. Note that while calling a member function of a different object executes the same section of the function code, the execution results are generally different.

Different objects use the same function code snippet, how can it operate on the data of different objects separately?

The original C + + has specifically set up a pointer named this to point to different objects. Note:
The code snippets for member functions are stored in the same way, regardless of whether a member function is defined within the class or outside the class. The
does not confuse this storage of member functions with the concept of inmne (built-in) functions. Do not mistake a member function with a inline declaration (or default of inline), whose code snippets occupy the storage space of the object without inline the declared member function, which does not occupy the object's storage space. The code snippet for a member function does not occupy the object's storage space, whether or not it is declared with inline. The effect of declaring with inline is that when the function is invoked, the copy the code snippet of the function to the function call point, and without the inline declaration, when the function is called, the process goes to the population address of the function code snippet, and after the function code snippet is executed, the process returns the function call point. Inline is not related to the storage space of the member functions, they are not the same question and should not be confused. The
should indicate that the "member function of So-and-so object" is from a logical point of view, and that the way in which member functions are stored is not contradictory in terms of physical aspects.

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.