Practical training c ++ language design-clock analog clock system

Source: Internet
Author: User

Platform: VC ++ 2005 passed the test!
. Vcproj
This is the main project file of the VC ++ project generated by using the Application 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!

/*
In this example, the implementation of the cclock type demonstrates the C ++
Operator overload, explicit type, implicit conversion.
1. Provides the ++, ++, and-operators for the cclock type to overload functions,
So that the ++, ++, and-operators can be used for variables of the cclock type.
.
2. For T3 = T1 * 5; T3 = 6 * T3 in the Customer Code, implement
There are two ways to use this function:
2.1 data type conversion functions of the cclock type
Cclock (unsigned long I); Convert unsigned long to cclock
Operator unsigned long (); converts cclock to unsigned long
The compiler will implicitly call the preceding conversion function to support
T3 = T1 * 5; T3 = 6 * T3; operation, the specific process is:
The compiler implicitly calls operator unsigned long (),
T1 and t3 in T1 * 5, 6 * T3 are automatically converted to unsigned long
Then execute the * algorithm operation, and then call cclock (unsigned long I)
Convert the result to the cclock type and assign it to T3.
2.2 implement the * method overload function of the cclock type to support cclock
* Method operation of the type and unsigned long type. The * method is reloaded.
There are two methods:
2.2.1 implement the * method for implementing the cclock type to overload "member functions"
Cclock operator * (unsigned long m); this function
Only operations such as T1 * 5 are supported, while operations 5 * T1 are
Not supported.
2.2.2 implement the * method for implementing the cclock type to overload the "friend function"
Friend cclock operator * (unsigned long M, cclock C );
Friend cclock operator * (cclock C, unsigned long m );
Supports T1 * 5 operations and 5 * T1 operations.

*/

# Include <iostream>
Using namespace STD;

Class cclock {
// The following are three global friend functions.
Friend cclock operator + (int I, const cclock & C );
Friend cclock operator + (const cclock & C, int I );
// Friend cclock operator + (const cclock & C1, const cclock & C2 );
*/
Public:
/* Use constructor as ADT Conversions
The constructor cclock here can
Set the unsigned long type variable
Convert to the cclock type.
Constructor with only one parameter
Both define an implicit conversion */
Cclock (unsigned long I );
Cclock (const cclock & rc );

Void print () const; // formatted Printout
Void tick (); // Add one second

/* Overload ++,-two operators to make them available
Variable used for cclock */
Cclock operator ++ () {tick (); return * This ;}
Cclock operator ++ (INT) {tick (); return * This ;}
Cclock operator + (const cclock & C );
// Cclock operator + (int I );
/* Cclock operator-(const cclock & rc );
// Cclock operator * (unsigned long m );

/* Define a cclock type to unsigned long type
Variable Function */
Operator unsigned long () {return this-> _ totsecs ;};
PRIVATE:
Unsigned long _ totsecs; // time length
Unsigned long _ secs, _ mins, _ hours, _ days; // second, minute, hour, day
};

Cclock: cclock (unsigned long I ){
/* Convert the time to seconds, minutes, hours, and days */
_ Totsecs = I;
_ Secs = _ totsecs % 60;
_ Mins = (_ totsecs/60) % 60;
_ Hours = (_ totsecs/3600) % 24;
_ Days = _ totsecs/86400;
}

Cclock: cclock (const cclock & rc ){
_ Totsecs = RC. _ totsecs;
_ Secs = RC. _ secs; _ mins = RC. _ mins;
_ Hours = RC. _ hours; _ days = RC. _ days;
}

Void cclock: Tick (){
Cclock temp (++ _ totsecs );
_ Secs = temp. _ secs; _ mins = temp. _ mins;
_ Hours = temp. _ hours; _ days = temp. _ days;
}

Cclock: Operator + (const cclock & rc ){
/* Note that the return value of this function must be cclock,
The content of the Return Statement is unsigned long.
Type of this-> _ totsecs + RC. _ totsecs, implicit
Type conversion occurs */
Return (this-> _ totsecs + RC. _ totsecs );
}
/*
Cclock: Operator-(const cclock & rc ){
Return (this-> _ totsecs-RC. _ totsecs );
}*/

/* Cclock: Operator * (unsigned long m ){
_ Totsecs = _ totsecs * m;
Return (this-> _ totsecs );
}
*/

/* Cclock operator + (cclock C1, cclock C2 ){
// Note that C1. _ totsecs + C2. _ totsecs is unsigned long.
// But the return value type of the function is cclock, And the compiler automatically calls the cclock class
// Conversion the ADT for Conversion Processing.
Return (C1. _ totsecs + C2. _ totsecs );
}

Cclock: Operator-(cclock c ){
Return (_ totsecs-C. _ totsecs );
}

Cclock operator * (unsigned long M, cclock C)
{
Return (M * C. _ totsecs );
}

Cclock operator * (cclock C, unsigned long m)
{
Return (C * m );
}
*/

/* Cclock operator + (const cclock & C1, const cclock & C2 ){
Return (C1. _ totsecs + C2. _ totsecs );
}
*/

Cclock operator + (int I, const cclock & C ){
Return (I + C. _ totsecs );
}

/* Cclock: Operator + (int I ){
Return (I + _ totsecs );
}*/

Cclock operator + (const cclock & C, int I ){
Return (I + C. _ totsecs );
}

Void cclock: Print () const
{
Cout <_ days <"D:" <_ hours <"H :"
<_ Mins <"M:" <_ secs <"S" <Endl;
}
 

 

// Clockpro. cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include "cclock. H"

Int _ tmain (INT argc, _ tchar * argv [])
{
Cclock T1 (59), T2 (172799); // 172799 = 2 days-1 sec
Cclock T3 (0 );
Cclock C1 (900), C2 (400 );

Cout <"Initial times are" <Endl;
T1.print (); t2.print ();
 
Int I;
Cin> I;
(T1 + T2). Print ();
(I + T1). Print ();
// (T1 + I). Print ();

Cout <"after one second times are" <Endl;
/* (++ T1). Print (); (++ T2). Print ();

T3 = t1 + T2; t3.print ();
T3 = T3-T1; t3.print ();
/* The * operator overload is not implemented in the program
How is the * Operation implemented here? */
/* T3 = T1 * 5; t3.print ();
T3 = 6 * T3; t3.print ();

Cout <"/n C1 and C2/N ";
C1.print (); c2.print ();
*/
Return 0;
}

 

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.