Nice-standard simple type

zhaozj2021-02-11  172

Nice-standard simple type

We all know that some kinds of categories are actually achieved a particular type and is an extension of language variable types. Such as score, grade class, etc. These final achievements are to allow users to operate just like the standard types provided by the language, or even ignore it. This type of use is very typical, I call it a standard type of NICE class. Ansistring in C Builder is the SST class that meets this standard. Let's take a look at the definition of the NICE class to see what needs to be implemented as a NICE class. An example of an SST class does not provide a score data type in C , and we implement it in a class, because the score is a data type, naturally to be implemented into NICE form. Class Faction {private: // Provides actual value data member int _Numerator; // molecular int _denominator; // branch, pay attention to inert _maxcommondivisor (int A, int b); // maximum number INT _MINCOMMONMULTIPLE (int A) , INT B); // Minimal male number void _simple (); // 简 Void CommonAminator (const fraction & osfrac); // Point public: FAction (); FAction (int digue); FAction (int value) ; Faction (float Value); Fraction & operator = (const Fraction & FracValue); friend Fraction operator (const Fraction & Frac1, const Fraction & Frac2); friend Fraction operator- (const Fraction & Frac1, const Fraction & Frac2); friend Fraction operator * (const Fraction & Frac1, const Fraction & Frac2); friend Fraction operator / (const Fraction & Frac1, const Fraction & Frac2); friend int operator> (const Fraction & Frac1, const Fraction & Frac2); friend int operator <(const Fraction & Frac1, const Fraction & Frac2); Friend int operator> = (Const Fraction & FRAC1, Const Fraction & Frac2); Friend Int Operator <= (Const Fraction & Frac1, Const Fraction & Frac2); Friend Int OP ERATOR == (Const Fraction & Frac2); Friend Int Operator! = (Const Fraction & Frac1, Const Fraction & Frac2);}; General NICE class has one or several data to store actual values, but obvious Should not expose them, have you seen INT.VALUE similar form.

This requires that there must be no direct interaction with other types of data or data members through member functions (operator) or data members, which can be converted through operator overloading and constructive functions. There are also some common operators to be used as usual. As can be seen from the above example, the data of the actual value of the NICE class is always placed in the private section declaration to avoid direct access, all access is performed by operator functions. As a type, it is clear that the variables must be possible, assigning initial values, and the addition and subtraction and segmentation and logic comparison operations. Why is the FACTION class be designed? Let's take a look at the most common type of Int, a type generally use, as long as our Nice class can be used as using the INT identifier, that is our goal. First, the first is statement: declare a variable INT X; with initial value declared variable INT x = 1; we must use the same use of our Nice class, clearly need to achieve the default constructor (you can see the actual situation, you may not need, But we recommend to assign an initial value for actual values) and assignment operators. Here, how to give new types to specify its constant form, the plastic constant form is a numeric character with a small number of points, which is divided into decimal form, eight-in-form, hexadecimal form, these are its constant form. As the constant form of the fraction, it is easier to think of x / y, but because it is a legal expression, it will be calculated before the transmission value, but finally passed to the score variable, it may be due to operation. Return to form an error of an error. This is not what we want. Here, the method for the first time, and the method of adding numbers avoid formation of expression operations. Then the two declarations of the above places apply to scores: FAction X; FACTION X = "2/3"; non-rigor number. Or FAction X = 2 can be integrated into an integer; or FAction X = 1.5; can be except for the decimal. This requires fractional FACTION class implementation: 1. Default constructor FAction :: FAction (); 2, Conversion string type data constructor FAction :: FACTION (const char * const); 3, Conversion Plastic data constructor Faction :: FAction (int); 4, Conversion Floating Point Data Constructor FAction :: FAction (FLOAT), 5, Assignment Operator Faction :: Operator = (Const Faction &). Second, the basic calculation: assuming that X and Y have been declared as int. x plus Y: X Y; X Decrease Y: XY; X and Y Multiply: x * Y; X divided by Y: X / Y; score class to fully implement these operators, need to pay attention to the constant Forms and convertible types of operations.

For example, a shaped operation: FAction X; X 2; or 2 X; here should be used for the front-rear position of the integrated number of integrated arithmeters, using a friend function method to implement operator overload: Friend Fraction Operator (Const Fraction & , const Fraction &); friend Fraction operator- (const Fraction &, const Fraction &); friend Fraction operator * (const Fraction &, const Fraction &); friend Fraction operator / (const Fraction &, const Fraction &); three boolean comparison: greater than> , Less than <, equal to ==, not equal to! =, Greater than or equal to> =, less than or equal to <=. There is also a problem with the constant form of the arithmetic operator, naturally also need to be implemented by a friend's method, the principle has been explained, and details are not described here. Implemented as follows: Friend Int Operator> (Const Fraction &, Const Fraction &); Friend Int Operator <(Const Fraction &, Const Fraction &); Friend Int Operator> = (Const Fraction &, Const Fraction &); Friend Int Operator < = (const fraction &, const fraction &); friend int operator == (const fraction &, const frame "; friend int operator! = (const frame"; four, output stream: no matter what type Data, it must eventually be output. The most common output mechanism in C is COUT, and the score class also implements the output in COUT: Cout

转载请注明原文地址:https://www.9cbs.com/read-5972.html

New Post(0)