Platform: VC ++ 2005 passed the test!
. Vcproj
This is an applicationProgramThe main project file of the VC ++ project generated by the wizard.
It contains the Visual C ++ version information that generates the file, as well as information about the platform, configuration, and project features selected by the Application Wizard.
Stdafx. H, stdafx. cpp
These files are used to generate a pre-compiled header (PCH) file named twod. PCH and a pre-compiled file named stdafx. obj.
These are all VC ++ files generated using the Application Wizard, so they are not listed
I will only list the main parts of the program!
# Include <iostream>
Using namespace STD;
Class crational {
Public:
Crational (INT n = 0): _ numerator (N), _ denominator (1 ){}
Crational (int I, Int J): _ numerator (I), _ denominator (j ){}
// The following constructor also acts as type coversion,
// Convert from double to crational, for example, 3.5 to 35/10
Crational (Double R ):
_ Numerator (R * big), _ denominator (BIG ){}
// The operator type () is used to define a type coversion:
// From crational to double
// This type of conversion function uses crational
// Variables bring a lot of convenience, in many cases the Compiler
// This function will be automatically called to convert the variable of the crational type
// Use this function to convert to double
Operator double (){
Return static_cast <double> (_ numerator)/_ denominator;
}
Void print () const {
Cout <_ numerator <"/" <_ denominator;
}
PRIVATE:
Long _ numerator, _ denominator;
Enum {big = 100 };
};
// Rationalpro. cpp: defines the entry point of the console application.
/* This example provides the designer's own type design.
Instance of the type conversion function.
(1) type conversion can be provided through the Type constructor. In this example
Crational (Double R), which can convert double type variables to crational
(2) define the type conversion function. In this example, operator double ()
This function can be called in the program "display" or "implicitly" to implement
Conversion from crational to double.
*/
# Include "stdafx. H"
# Include "crational. H"
/* The following defines three greater functions with the same name, which is a function overload */
Int greater (int I, Int J) {return (I> J? I: J );}
Double greater (Double X, Double Y) {return (x> Y? X: Y );}
// Why is there no overloading> here?
// Note the execution process of this function
Crational greater (crational W, crational Z ){
Return (W> Z? W: Z );
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Int I = 10, j = 5; float x = 7.0; Double Y = 14.5;
Crational W (10) and Z (3.54 );
// The following y + Z is not based on operator overloading, but based on
// Type conversion to realize the validity of Y + z
Cout <Y + z;
Cout <"/ngreater (" <I <"," <j <") ="
<Greater (I, j );
Cout <"/ngreater (" <x <"," <Y <") ="
<Greater (x, y );
Cout <"/ngreater (" <I <","; Z. Print (); cout <") ="
// Call the conversion from double to rational as shown below
<Greater (static_cast <crational> (I), Z );
// The following implicitly calls the conversion from rational to double
Cout <"/ngreater ("; W. Print (); cout <","; Z. Print (); cout <") = ";
Greater (W, Z). Print ();
Return 0;
}