Class in the preparation process of some notes, notes in the preparation process

Source: Internet
Author: User
Tags function calculator

Class in the preparation process of some notes, notes in the preparation process

When writing classes, we should grasp the details well, not only avoid some obvious errors, but also how to form a good programming style. Next, we will use the following example to compile the analysis class:

Class Complex

{

Public:

Complex (double real, double imaginary = 0): _ real (real), _ imaginary (imaginary ){}

Void operator + (Complex other)

{

_ Real = _ real + other. _ real;

_ Imaginary = _ imaginary + other. _ imaginary;

}

Void operator <(ostream OS)

{

OS <"(" <_ real <"," <_ imaginary <")";

}

Complex operator ++ ()

{

++ _ Real;

Return * this;

}

Complex operator ++ (int)

{
Complex temp = * this;

++ _ Real;

Return temp;

}

Private:

Double _ real, _ imaginary;

}

1. Be careful about the implicit temporary objects generated in implicit type conversion. A good way to avoid this problem is to convert the displayed type in the constructor as much as possible, and avoid writing type conversion operators.

Because the second parameter of the constructor in the above class has a default value, this function can also be used as a single-parameter constructor, this also means that implicit conversion from the double type to the Complex type can be performed. Note that this type conversion may not always be expected.

2. When passing a parameter object, we should first select the const & method instead of the value passing method. For example:

Void operator + (Complex other) will generate temporary objects when passing parameters, and the execution efficiency will be reduced.

3. We should first select "a op = B", instead of "a = a op B" (here op represents an operator ). This method is clearer and more efficient.

Why is operator ++ = more efficient? The reason is that this operator performs operations on the objects on the left and returns a reference rather than a temporary object. Operator + must return a temporary object. For example:

T & T: operator + = (const T & other)

{

//...

Return * this;

}

Const T operator + (const T & a, const T & B)

{

T temp ();

Temp + = B;

Return temp;

}

Note the relationship between operators + and + =. The former is implemented through the latter. This is not only to make the code concise, but also to ensure code consistency. Note: To prevent programmers from writing expressions like "a + B = c", the type of the returned temporary object should be "const Complex" instead of "Complex ), this is why const T is returned in the above operator +.

4. The C ++ standard stipulates that operators =, (), [], and-> must be defined as member functions, the operator functions defined in the class, such as new, new [], delete, and delete [], must be static member functions. For other operator functions: IF operator> or operator <is used for convection I/O, or if type conversion is required for its left parameter; or, if it can be implemented through the public interface of the class, this function is defined as a non-member function (in the first two cases, it can also be defined as a friend function if needed ). If operator functions need to implement virtual functions, add a virtual member function to provide virtual functions. Then, add a virtual member function to provide virtual functions, use this virtual member function to implement the operator function. Otherwise, operator functions are defined as member functions.

5. operator <should not be defined as a member function. In addition, the non-member function operator <should be implemented using a member function (usually a virtual function), and the function of this member function is to output a stream. Furthermore, operator <should return an application of the "ostream &" type and apply this stream object to implement chained operations.

6. The pre-increment operator should return a very large number of references, which not only enables the client code to be written in a more intuitive way, but also avoids unnecessary inefficiency. A constant value should be returned in the Post-incrementing operator function. This method can avoid modifying the returned object, thus avoiding the problematic code like "a ++. To maintain code consistency, we should use pre-increment to achieve post-increment.

7. Avoid using reserved names. Some identifiers with leading underscores are retained in the implementation of the C ++ standard library, which are hard to remember. Therefore, it is best not to use leading underscores at all.

After the above analysis, the modified class is:

Class Complex

{

Public:

Explicit Complex (double real, double imaginary = 0): _ real (real), _ imaginary (imaginary ){}

Complex & operator ++ = (const Complex & other)

{

_ Real + = other. _ real;

_ Imaginary + = other. _ imaginary;

Return * this;

}

Complex & operator ++ ()

{

++ _ Real;

Return * this;

}

Const Complex operator ++ (int)

{
Complex temp (* this );

++ _ Real;

Return temp;

}

Ostream & Print (ostream & OS) const

{

Return OS <"(" <real _ <"," <imaginary _ <")";

}

Private:

Double real _, imaginary _;

}

Const Complex operator + (const Complex & lhs, const Complex & rhs)

{

Complex ret (lhs );

Ret + = rhs;

Return ret;

}

Ostream & operator <(ostream & OS, const Complex & c)

{

Return c. Print (OS );

}

Zookeeper
What should I pay attention to when learning programming?

When you learn a language (that is, you remember the syntax, lexical, and some common functions of the language), it means that you have mastered the basic tools for programming. No matter what language you use, it is similar. However, tools are long and short. For example, applications such as Delphi and VB for Windows environments are very convenient; network-based programs are easily developed using Java; C and C ++ are suitable for developing system-level software. If we use C (C ++) to write a notepad, it is estimated that we need to write hundreds of lines of code, and there is no guarantee that there are no major errors. Instead, we use delphi, or VC to do this, just a few mouse clicks on the control to complete it, which is convenient. This means that tools have their own characteristics, and each aspect has the most suitable tools. However, tools are only tools. Tools alone cannot write good programs. For example, if I want to write an article, first determine the language used for writing (Chinese, English, Russian, or others? Obviously, if I want to write it to Chinese people, I will naturally use Chinese characters, I want to write it to Americans, and of course I want to use English .) Okay. What if I have determined the language? Can I write an article? How to Design the article structure? What are the skills and precautions for writing articles? I don't know about this. Naturally, I cannot write a good article. Obviously, it is far from enough language to write a good article. Similarly, it is far from enough to write a good program (the program running result is correct, the time, the space efficiency is high, the robustness is good, and the readability is strong. Learning programming methods is what we should do. University computer education begins by teaching a language (generally C language), and then begins to learn data structures and algorithms. Data Structure and algorithm design analysis are the foundation of the entire programming method, the top priority, is also a required subject for postgraduate professional courses. In the past, we had a popular saying: Data Structure + algorithm = program. This is unreasonable. The representation and storage of data in a computer are not unorganized, regular, and structured. Therefore, when we operate on these data, there is theoretically an optimal (or near-optimal) algorithm that compares with a structure to ensure the time-space efficiency of the operation data. For example, there is a row of data: a, B, c, d, e. If you need to search for the data, you can sort and store the sequence to reduce the operation time. Similarly, if you often want to insert or delete an object, using link storage can reduce the time complexity of this operation. It can be seen that a reasonable data structure and efficient algorithms have an important impact on program quality! For example, we want to write a program to sort the ID card numbers of all Chinese citizens. This is an extremely large set of data. So much data adopts what structure to represent and store, And what algorithms are used to sort the data so that the program can be sorted as quickly as possible, use as little space as possible. This is a learning and programming knowledge! Therefore, I think the programmer's understanding of data structures and algorithms directly determines the level of programming. So how can we improve ourselves? We can study functions in the C (C ++) function library (API), or functions in the JFC (JAVA class library). These functions are implemented by very experienced programmers, it has a good running efficiency. Furthermore, it is the importance of mathematics. I think the importance of mathematics to programs is reflected in the support for algorithms. The idea of calculus provides theoretical guidance for computer numerical computation, while the ideas and methods in data structures and algorithm sources and discrete mathematics, linear algebra and probability statistics. In addition, computer cryptography, computer graphics, and computer multimedia cannot be separated from mathematics. Good algorithms need mathematical tools for theoretical verification and mathematical improvements! So what role does object-oriented play? In the end, I think the purpose of Object-oriented thinking is code reuse. The three concepts of object-oriented: encapsulation, inheritance, and polymorphism are all for code reuse. Reusing code with high quality not only saves manpower, but also greatly improves the quality of the original program. For example, if I want to write a multi-function calculator, I have designed the interface based on my preferences. In terms of function implementation, I have inherited the correct functional classes that others have already written, which not only saves the trouble of writing by myself, it also ensures the correctness of the calculator. Object-oriented thinking may not be superior in writing small programs, but its power will be apparent in medium and large software development. People who have not worked on medium and large projects will not feel the greatness of Object-oriented thinking. Here, I 'd like to emphasize the following:

Precautions during preparation of quality control plan

1. Formulate effective and suitable quality control plans for the company based on specific quality conditions.
2. Perform step-by-step control.

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.