Terms 2: Try to use
Yes, Scanf and Printf are very light, very efficient, you have long known how to use them, this I admit. But although they are useful, in fact, Scanf and Printf and their series can also do some improvements. In particular, they are not type safe and there is no scalability. Because type security and scalability are the cornerstone of C , you have to obey this. In addition, the Scanf / Printf series function separates the variables to be read and the information on the information on the read / write format, just like ancient Fortran. It is time to say to the fifth year!
Don't be amazed, these weaknesses of Scanf / Printf are operators >> and << strong items: int I; Rational R; // r is a number
...
CIN >> I >> R; COUT << i << r;
The above code must be compiled, >> and << must be a heavy-duty function that can handle the Rational type object (may be converted by implicit type). If such a function is not implemented, it will be wrong (handling Int does not do this because it is a standard usage). In addition, the compiler can select different forms of operators according to different variable types, so it is not necessary to deserve that the first object to be read is Int, and the second is Rational.
In addition, the grammar form is the same as the syntax form used when reading and writing objects, so it is not necessary to remember some of the SCANF, for example, if the pointer is not obtained, addicts, and if the pointer has been, it is necessary to add it. Address. These can be handed over to the C compiler. There is nothing else to do anything else, but you are different. Finally, it is important to note that the custom type like INT and the custom type of Rational Rational is the same. And you will try it with SACNF and Printf!
The code you have written to represent the rational class may like the following: Class Rational {public: Rational (int name = 0, int devenator = 1);
...
Private: int N, d; // molecule, branch
Friend Ostream & Operator << (Ostream & S, Const Rational & R);
Ostream & Operator << (Ostream & S, Const Rational & R) {s << r.N << '/' << r.d; returnide
The above code involves some subtle (but very important) usage of Operator <<, which is discussed in detail in this book. For example: The above Operator << is not a member function (Terms 19 explains why), and it is not a reference to operator <<, but is not a reference to the object of Const (see Terms 22). The statement and implementation of Operator >> are similar.
Although I am not willing to admit it, it is still very meaningful to return to those who have proved and correct old roads. First, some iostream's operations are achieved less than the corresponding C stream efficiency, so different options will give you the program (although not necessarily, see the Terms M16) brings a lot. But please keep in mind that this is not for all Iostream, just some special implementation; see Terms M23. Second, in the standardization process, the iostream library has made a lot of modifications in the underlying (see Terms 49), so for those applications that require maximum portability, different manufacturers will follow the standards. Third, the functionality of the iostream library is constructed and the function in
By the way, the title of this Territor has not been printed; I do is