Study Notes (3)

Source: Internet
Author: User

The following content is written after hughen learns C ++ primer plus (version 6th.

I read a book today and got a sentence unexpectedly. Stephen Prata talked about a custom Vector class and gave the computer a starting and ending position to find the path between them and calculate the number of steps. He made three sets of data output, the results are very different, the computer random path, the same distance, a very different, finally the author said: "If you find yourself walking randomly, please be confident and take a big step. Although it is still unable to control the direction of forward in the process of winding forward, it will at least go a little farther ."

I was deeply touched by this passage. I used to think that C ++ has learned a good job. After reading C ++ primer plus, I found that what I know is too small, less than 1% of the essence of C ++, so recently, it seems that everything has returned to the ideal age. I don't know how far success is from me, but I believe his distance from me is getting shorter and shorter. (This may be a complicated algorithm. I cannot accurately estimate the time complexity, but I am confident that sooner or later I will return)

Now, let's get down to the truth and continue to review ......

Features of Object-Oriented Programming (OOP)
  • Abstract;
  • Encapsulation and data hiding;
  • Polymorphism;
  • Inheritance;
  • Code reusability.

To reflect these features in C ++, classes were born. I learned it from C language and got used to the process-oriented language. It is more or less reflected in C ++ and it seems difficult to get rid of this feature. The book says: "when using the OOP method, first consider the object from the user's perspective-the data memory required to describe the operations required for user-data interaction. After describing the interface, you need to determine the graph and implement the interface and data storage. Finally, use the new design scheme to create a program ." You still don't know how to do this. The most direct way is to pull you directly to a large project (such as ERP, HRMS, etc.). You are responsible for the bottom layer, provides basic I/O interfaces or more complex interfaces for the upper layer, such as efficiency problems (multithreading, data sharing, etc ), such a project should know what OOP is, at least I think so. Class access control C ++ provides three access control methods: private, protected, and public. The default access control of the class is private, but the default access type of the struct is public. In the class design, try to separate the public interface from the implementation details. (Separation of interfaces and implementations) Note that functions defined in the class declaration will automatically become inline functions, so the inline mark is not required. Of course, you can also declare a function as an inline function. You need to use the inline qualifier before the function header when defining the function, but you do not need to use the inline identifier in the class declaration. Special requirements for inline functions are defined in each file that uses them. Therefore, the easiest way is to put the definition in the header file of the definition class. For the working principle of the class, see:
The difference between explicit and implicit calls of constructors is as follows:

Stock S1 = stock ("global company", 1000, 23); // explicitly call the constructor stock S2 ("Stock Company", 930, 81 ); // call the constructor implicitly

Of course, both call methods can initialize and construct a stock class normally.

Here, there is also a constructor-default constructor, which roughly looks like this:
Stock::Stock() {......}Stock::Stock(const string &co = "no company", int n = 0, double price = 0.0) {......}

Both of the above Code are Default constructors, but because there is only one default constructor, the above Code can only have one. The default constructor can have no parameters. If yes, you must provide default values for all parameters. When the constructor has only one parameter, You can initialize the object as follows:

Stock: stock (int n) {...} stock P = 90; // valid Code
  • Tip:Parentheses are not required to call constructors implicitly.
  • Tip:Because the object does not exist before the constructor constructs the object, the constructor is used to create the object and cannot call it through the object.
The Destructor is used to release the memory when the lifetime of the class is reached. This process is determined by the compiler and should not be explicitly called in the code.

  • If a static storage class object is created, the Destructor is automatically called at the end of the program.
  • If an Automatic Storage Class object is created, the Destructor is automatically called when the program executes the code block.
  • If the object is created through new, it will remain in the stack memory or free storage area until the delete is used to release the memory, its destructor will be automatically called.

In fact, there is also a question about calling destructor for temporarily created classes, such:

Stock s1 = Stock("Microsoft Corporation", 1200, 37);s2 = Stock("NETC", 10, 629.1);

Initialize the first statement above. It creates an object with a specified value and may create a temporary object (of course, it may not. This is determined based on different compilation ); the second statement is a value assignment statement. A statement like this will certainly create a temporary variable when using the constructor, and then copy the temporary variable to S2.

C ++ 11 supports the use of "{" and "}" to initialize class variables, such:
Stock pkone = {"One", 12, 370};Stock jock {"Sport Inc."};

Note:A const Class Object cannot call a function that determines whether to modify the class member variables. It can only call the const function. For example:

Const stock STK = stock ("blue car"); STK. Show (); // The call is incorrect because STK is a const object, and show () is not a const function.

To make STK call show (), we need to declare show ()

void show() const;
If the following style function declaration is used in our code, this pointer is needed:
const Stock & topval(const Stock & s) const;

Its internal implementation should be:

Const stock & topval (const stock & S) const {If (S. var> var) return s; elsereturn * This; // note that here is * this, not this pointer}
The scope of a Class scope class means that members of the class cannot be directly transferred from the outside, as do public member functions. That is to say, to call a public member function, you must use an object.

The following code does not allow the use of constants in a class:

Class frame {PRIVATE: const int m_switch = 25; // incorrect declaration of double point [m_switch];}

The main reason is that the declarative knowledge describes the object format and does not create an object, so it will not be assigned a bucket.

But there is no way to use constants in the class, as shown below:
Class frame {PRIVATE: Enum {m_switch = 25}; // use static const int m_switch = 25; also, because static variables are not stored in the object, double point [m_switch];}

Here, m_switch is just a name. It will be replaced with 25 when it is encountered in the Code with the scope of the entire class.





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.