Classes, objects, and members of C + +

Source: Internet
Author: User

classes, objects, and members of C + +

1. declaration of a class

A class is a user-defined type, and if a class is used in a program, the definition must first be declared.

The syntax form for a class declaration is as follows:

    class name
       {
Public :
Public Member (external interface)
protected:
protected Members
Private:
Private members
       }

class   Declare a class to use it  

   course, here's , protected and private keywords can be changed in any order, such as declaring a private member before declaring the other can also, each keyword can appear multiple times, such as declaring some public public Span style= "font-family: ' The song Body '; It is also possible to declare other members, but in general we declare classes in the form above.

2. Create an Object

When defining an object, start with the class name, empty one, and then write the object name you want to define.

such as:computer ThinkPad;

A class is a template for creating objects, a class can create multiple objects, each object is a variable of a class type, and the process of creating an object is also called an instantiation of the class. Each object is a concrete instance of the class (Instance), which has member variables and member functions for the class.

Like structs, a class is simply a declaration of a complex data type that does not occupy memory space. The object is a variable of the data type of the class that occupies memory space.

The difference between a class and an object:

A class is just an abstract noun, whereas an object is an actual individual.

3. The relationship between an object and a member in a class

Object can be used with the dot operator. "To access the data members of the object and assign them values, which cannot be omitted when used.

such as:ThinkPad. CPU

Attention:

"Do not confuse classes and objects, do not assign values to classes."

The object can only be a member that already exists in the class.

4. Access control for class members

the difference between protect,public,private?

Public is accessible at every point of the program, whether in this category or in

Protected is in this class and in the friend class, the subclass can access

Private can only be accessed within this class, and other places cannot

member variable for public decoration

can be accessed anywhere in the program, that is, the meaning of a public variable, without the need for a member function to be directly accessible by the instance of the class

member variable for private decoration  

Only in the class can be directly accessed, private, class instances to be accessible through member functions, this can play a message to hide

protected is a protected variable   

Class and subclasses can be accessed directly, that is, the base class has protected members, subclasses inherit from the base class, then you can also access the protected members of the basis class , if the base class is Private member, it is also hidden from the child class, inaccessible

5. Declaration and definition of member functions

Each member function has its declaration part and definition section.

The declaration section is simply a description of the function's parameter type and the return value type

The definition section is a function of the functions of the implementation

A class is a data type that resembles a normal data type, but differs from a normal data type. The data type of a class is a collection that contains member variables and member functions.

The member variables of a class have the same data type and name as normal variables, occupying a fixed-length memory space. However, when defining a class, you cannot assign a value to a member variable, because the class is only a data type and itself does not occupy memory space, and the value of the variable requires memory to be stored.

The member function of a class, like a normal function, has a return value and a list of parameters, and it differs from a general function in that the member function is a member of a class and appears in the class body, and its scope is determined by the class, while the normal function is independent, the scope is global, or is in a namespace.

However, when the member function is defined outside the class, it must be qualified with the class name in front of the function name .

:: Called a domain resolver (also called a scope operator or scope qualifier),

Used to connect the class name and function name to indicate which class the current function belongs to.

The member function must first be declared in the class body and then defined outside the class, i.e. the position of the class body should precede the function definition, otherwise it will compile with an error.

Although the member function is defined outside the class, the function is executed based on the invocation of the function's definition (function code), as defined in the functional prototype declared in the class.

6. Common inline functions

When a function is defined, the compiler creates a set of instructions in memory for it, and when we use this function, the program jumps to the instruction set.   When the function finishes running, the program returns to the next line where the function statement was originally called to execute. If you perform hundreds of calls to the function, you will have to jump back and forth hundreds of times, which can affect the execution efficiency of the program.

The workaround in C + + is to declare functions using the keyword inline , such as:

inline int func ();

The compiler will not create a real function, but will copy all the code of the inline function into the calling function, so that the program does not need to jump back and forth when executing the calling function, which naturally increases the efficiency of the program's operation.

However, if the calling inline function is more than hundreds of times, its code will be duplicated hundreds, thus increasing the volume of the code, will reduce the efficiency of code execution.

So this is a double-edged sword, if the function is small, multiple copies also increase the volume of not much, you can use the appropriate inline function, depending on the situation.


Classes, objects, and members of C + +

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.