Traditionally, C ++ has advantages over some trendy languages, such as Java and C #, in terms of program running performance. This concept is incomplete. If a person is convinced of this, it means that he does not fully understand C ++ and that language. At the same time, people who hold such ideas are often misled (the culprit is, of course, the wealthy companies ). For these companies, they have hidden the core differences between C ++ and XX languages, and strengthened the differences that most programmers are not very concerned about, that is, performance. As the CPU performance improves rapidly, the performance issue is no longer of concern. This is called "Li daitao ". Many well-known programmers believe them. As a result, the conspiracy of big companies was successful.
In this article series, I will do my best to use some practical cases to crack such a lie, and the world is still innocent. I hope my efforts will not be in vain.
Software Engineering
Generally, the development cost of Java or C # is lower than that of C ++. However, if you can fully analyze the differences between C ++ and these languages, you will find that this sentence is true. The condition is that the scale and complexity of the software are relatively small. If there are no more than 30 thousand lines of valid Code (excluding the code generated by the generator), this statement can be basically true. Otherwise, as the amount of code and complexity increase, the advantages of C ++ will become more and more obvious.
The difference is the software engineering of C ++. When talking about software engineering in Java and C #, C ++ has quietly elevated the software engineering to an unprecedented level. This is ignored by most people and is overwhelmed by large companies.
The quality of language in software engineering depends on the abstract ability of language. From process-oriented to object-oriented, the abstract ability of Languages has made a qualitative leap. However, in practice, people find that object-oriented cannot solve all the problems in software engineering. As a result, elites gradually introduce and expand generic programming to solve higher-level software engineering problems. (In fact, the origins of object-oriented and generic programming can be traced back to 1967, but because generic programming is more abstract, the application lags far behind object-oriented ).
By chance, I suddenly wondered how to use a normal arithmetic expression to calculate the currency type, without worrying about the exchange rate conversion. I have written the specific content in my blog: http://blog.csdn.net/longshanks/archive/2007/05/30/1631391.aspx. (The csdn Forum seems to be a little indigestion for large articles ). Next I will briefly describe the problem, focusing on the differences between language capabilities.
At that time, I was faced with four currencies: RMB, USD, UKP, and jpd. I hope to calculate them like this:
RMB _ (1000 );
USD _;
UKP _;
Jpd _ (2000 );
USD _ = RMB _; // value assignment operation, which implies exchange rate conversion. USD _ the actual value is 1000/7. 68 = 130.21
RMB _ = RMB _ * 2.5; // The unit price multiplied by the upper number.
UKP _ = USD _ * 3.7; // The unit price is multiplied by the upper quantity and assigned to the pound. Implicit exchange rate conversion.
Double N = jpd _/(USD _-UKP _); // calculates the Quantity Based on the price difference. Three currencies are involved, with implicit exchange rate conversion.
Traditionally, we usually use a double or currency type to represent all currencies. Therefore, when different currencies are involved in the operation, explicit exchange rate conversion must be performed:
Double RMB _ (100), USD _ (0), UKP _ (0), JPN _ (2000 );
USD _ = RMB _ * usd_ RMB _rate;
UKP _ = (USD _ * usd_ukp_rate) * 3.7;
Double N = jpd _/(USD _ * usd_jpd_rate)-(UKP _ * ukp_jpd_rate ))
Obviously, after strong typing, the code is much more concise. In addition, you can use heavy loads or special features to directly give auxiliary information related to currency, such as currency symbols (I did not do this, but it is not complicated ).
In C ++, I quickly developed this currency system using templates, Operator overloading, operator function templates, and other technologies:
Template <int currtype>
Class currency
{
Public:
Currency <currtype> & operator = (count currency <CT2> & V ){
...
}
Public:
Double _ Val;
...
};
Template <int ty, int TP>
Inline bool operator = (currency <ty> & C1, const currency <TP> & C2 ){
...
}
Template <int ty, int TP>
Inline currency <ty> & operator + = (currency <ty> & C1, const currency <TP> & C2 ){
...
}
Template <int ty, int TP>
Inline currency <ty> operator + (currency <ty> & C1, const currency <TP> & C2 ){
...
}
...
There are no more than 200 lines of code in total. (Of course, an industry-intensive currency system requires more auxiliary classes and functions. But basically there will be no more than 500 lines of code ). If I need a currency, first specify a constant value of the int type for it, and then type Def:
Const int ct_ RMB = 0; // you can also use Enum
Typedef currency <ct_ RMB> RMB;
Const int ct_usd = 1;
Typedef currency <ct_usd> USD;
Const int ct_ukp = 2;
Typedef currency <ct_usd> USD;
Const int ct_jpd = 3;
Typedef currency <ct_usd> USD;
...
For each new currency, you only need to define a value and then Type Def. For the core currency <> and operator overload, you do not need to make any Ding changes.
Then I tried to port the code of this currency system to C. According to the test results, I also wrote an article (also in blog: http://blog.csdn.net/longshanks/archive/2007/05/30/1631476.aspx ). I worked with a colleague (who was developed using C # And was more familiar with it) and spent most of the morning finally finishing the work.
The frustrating thing is that C # does not support = overload. Therefore, it can only be replaced by asign <> () generic functions. Then, because the generic type of C # does not support non-Generic parameters, that is, the generic equivalent of the int currtype template parameter in the preceding C ++ code, C # does not support generic Operator overloading. The entire currency system degrades from the generic programming mode to the object-oriented mode. Of course, with our unremitting efforts, we finally achieved the same code effect as in C ++ (except for the value assignment operation ):
Assign (RMB _, UKP _);
Assign (USD _, RMB _ * 3.7 );
...
I know, some people will say, since OOP can do it, why use GP? GP is too complicated. Here, I have prepared a set of statistics for these people: in the C # code, I implemented three currencies and the results defined four classes (one base class, three currency classes); reload 30 Arithmetic Operators (10 operators are implemented in the same way as C ++, and each class has to reload 10 Operators ); six type conversion operators (Conversion operators from two types of currency to the third type ).
This is not the worst. When I add a currency, the number of currencies becomes four, and the data is changed to: 5 classes, 40 Arithmetic Operators, and 12 type conversion operators.
When the number of currencies increases to 10, 11 classes, 100 Arithmetic Operators are overloaded, and 90 type conversion operators are overloaded.
In contrast, the implementation of C ++ includes one class template for three currencies, one value assignment operator overload template, ten Arithmetic Operators overload templates, and three const int definitions, 3 typedef.
When there are 10 currencies: one class template; one value assignment operator overload template; 10 Arithmetic Operators overload templates; the number of const int definitions and typedef is increased to 10.
That is to say, the C ++ version of Code increases linearly with the increase of currency. In addition, the coefficient added by the code line is only 2. Please note that it is a code line! It is not the number of classes, functions, or operators. The amount of code in the C # version increases with a geometric level. Geometric level !!!
I don't need to talk about the meanings of these numbers. No matter the number of codes, maintainability, and scalability, C ++ is far better than C. Not to mention availability (how ugly the assign function is ).
I know that some people will also say that currency is so special that it is rare in practice. Yes, currency is special, but it is not as special as it is. I once made a simple case of reading the graphic information in the script and drawing the output to demonstrate some basic concepts of OOP for training. However, if you refine it, you can develop a very good script graphics engine. Specifically, I used techniques such as combination recursion, polymorphism and dynamic link, and class factory. That is the class factory. because I used the template, the code of the class factory was reduced by 2/3, and there was no repeated code, making it easier to maintain. Alexandrescu has more cases about GP optimization in the abstract class factory in mow.c ++ design. The same technology can also be promoted to the class system of the business model to optimize the code of the class factory.
If you are not satisfied, check boost. Many boost libraries implement almost unimaginable functions, such as lambda expressions and BGL naming parameters. It provides many new ideas for optimizing software code. Many technologies and methods can greatly optimize code and reduce development costs.
Finally, if you think the biggest advantage of C # is. NET platform, so I can tell you that there is another thing in the world called C ++/CLI, which can fully meet the requirements. NET development, and better, enough to clean. net that dirty ass. However, this will be another story...