C ++ operator overload + instance

Source: Internet
Author: User

[Project 2 Expansion 2 (thinking)] This question hangs your appetite: sets two score class objects, such as CFraction c1 and c2. If int I is defined, we can use cin> I> j; enter the values of I and j on the keyboard, whether to use cin> c1> c2; what about the input score? Similarly, can I use cout <c1 <c2; for output? In addition, c1 + c2 is used to obtain a new score. c1/c2 is used to divide two scores, and other values such as addition, subtraction, comparison, and reciprocal are also taken for granted. In fact, to customize the score class, these intuitive basic operations should be implemented, which is called the overload of operators. Amplify () is used in this task to provide an implementation solution for the "zoom in" operation. More content is worth looking forward.

Note: Some C ++ compiling systems (such as VC ++ 6.0) do not fully implement the C ++ standard. the header file of h does not support reload the member function as a friend function. Therefore, in VC ++ 6.0, the first two lines of the program should be:

# Include <iosteam>

Using namespace std;

Change to a line: # include <iosteam. h> // C Language Style

This is my own program for operator overloading:


[Cpp]
# Include <iostream. h>
 
Enum style_enum {zero, one, two, three };
 
Class CFraction
{Private:
Int nume; // molecule
Int deno; // denominator
Public:
CFraction (int nu = 0, int de = 1): nume (nu), deno (de) {}// constructor, initialized
 
Void simplify (); // simplification (removing the public factor from the numerator denominator)
 
// Input/output overload
Friend ostream & operator <(ostream & out, const CFraction & cf );
Friend istream & operator> (istream & in, CFraction & cf );

// Add, subtract, multiply, and divide the result.
Friend CFraction operator + (const CFraction & lcf, const CFraction & rcf );
Friend CFraction operator-(const CFraction & lcf, const CFraction & rcf );
Friend CFraction operator * (const CFraction & lcf, const CFraction & rcf );
Friend CFraction operator/(const CFraction & lcf, const CFraction & rcf );

// Relational operators
Friend bool operator> (const CFraction & lcf, const CFraction & rcf );
Friend bool operator <(const CFraction & lcf, const CFraction & rcf );
Friend bool operator >=( const CFraction & lcf, const CFraction & rcf );
Friend bool operator <= (const CFraction & lcf, const CFraction & rcf );
Friend bool operator = (const CFraction & lcf, const CFraction & rcf );
Friend bool operator! = (Const CFraction & lcf, const CFraction & rcf );
 
 
// Obtain the + and-target operators.
CFraction operator + ();
CFraction operator -();
};
 
 
 
Void CFraction: simplify ()
{
Int v1 = nume;
Int v2 = deno;
While (v2)
{
Int temp = v2;
V2 = v1 % v2;
V1 = temp;
}
Nume/= v1;
Deno/= v1;
If (deno <0)
{
Deno =-deno;
Nume =-nume;
}
}
 
// Output overload
Ostream & operator <(ostream & out, const CFraction & cf)
{
Out <cf. nume <'/' <cf. deno;
Return out;
}
 
// Input overload
Istream & operator> (istream & in, CFraction & cf)
{
Char ch;
While (1)
{
In> cf. nume> ch> cf. deno;
If (cf. deno = 0)
Cerr <"the denominator is 0. Enter \ n again ";
Else if (ch! = '/')
Cerr <"format error (such as m/n) Please input \ n again ";
Else break;
}
Return in;
}
// Addition overload
CFraction operator + (const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. deno + lcf. deno * rcf. nume;
Cf. deno = lcf. deno * rcf. deno;
Cf. simplify ();
Return cf;
}
// Subtraction overload
CFraction operator-(const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. deno-rcf. nume * lcf. deno;
Cf. deno = lcf. deno * rcf. deno;
Cf. simplify ();
Return cf;
}
// Multiplication overload
CFraction operator * (const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. nume;
Cf. deno = lcf. deno * rcf. deno;
Cf. simplify ();
Return cf;
}
// Division overload
CFraction operator/(const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. deno;
Cf. deno = lcf. deno * rcf. nume;
Cf. simplify ();
Return cf;
}
// Obtain positive and Heavy Loads
CFraction: operator + ()
{
Simplify ();
If (nume <0)
Nume =-nume;
Return * this;
}
// Obtain the negative overload
CFraction: operator -()
{
Simplify ();
Nume =-nume;
Return * this;
}
 
 
// Heavy load greater than the number
Bool operator> (const CFraction & lcf, const CFraction & rcf)
{
Int l_nume = lcf. nume * rcf. deno;
Int r_nume = rcf. nume * lcf. deno;
Int common_deno = lcf. deno * rcf. deno;
If (l_nume-r_nume) * common_deno> 0) return true;
Return false;
}
 
// Less than heavy load
Bool operator <(const CFraction & lcf, const CFraction & rcf)
{
Return! (Lcf> rcf );
}
 
// Equals to overload
Bool operator = (const CFraction & lcf, const CFraction & rcf)
{
Return lcf. nume = rcf. nume & lcf. deno = rcf. deno;
}
 
// Not equal to overload
Bool operator! = (Const CFraction & lcf, const CFraction & rcf)
{
Return! (Lcf = rcf );
}
 
//
Bool operator> = (const CFraction & lcf, const CFraction & rcf)
{
If (lcf <rcf) return false;
Return true;
}
 
//
Bool operator <= (const CFraction & lcf, const CFraction & rcf)
{
If (lcf> rcf) return false;
Return true;
}
 
Int main ()
{
CFraction cf1;
CFraction cf2;
Cin> cf1> cf2;
Cout <"cf1:" <cf1 <'\ t' <"cf2:" <cf2 <endl
<"Cf1 + cf2:" <cf1 + cf2 <endl
<"Cf1-cf2:" <cf1-cf2 <endl
<"Cf1 * cf2:" <cf1 * cf2 <endl
<"Cf1/cf2:" <cf1/cf2 <endl
<"+ Cf1:" <+ cf1 <endl
<"-Cf1:" <-cf1 <endl
<"+ Cf2:" <+ cf2 <endl
<"-Cf2:" <-cf2 <endl
<"Cf1> cf2? 1/YES 0/NO "<(cf1> cf2) <endl
<"Cf1 <cf2? 1/YES 0/NO "<(cf1 <cf2) <endl
<"Cf1 = cf2? 1/YES 0/NO "<(cf1 = cf2) <endl
<"Cf1! = Cf2? 1/YES 0/NO "<(cf1! = Cf2) <endl
<"Cf1> = cf2? 1/YES 0/NO "<(cf1> = cf2) <endl
<"Cf1 <= cf2? 1/YES
 

Return 0;
}

# Include <iostream. h>

Enum style_enum {zero, one, two, three };

Class CFraction
{Private:
Int nume; // molecule
Int deno; // denominator
Public:
CFraction (int nu = 0, int de = 1): nume (nu), deno (de) {}// constructor, initialized

Void simplify (); // simplification (removing the public factor from the numerator denominator)

// Input/output overload
Friend ostream & operator <(ostream & out, const CFraction & cf );
Friend istream & operator> (istream & in, CFraction & cf );

// Add, subtract, multiply, and divide the result.
Friend CFraction operator + (const CFraction & lcf, const CFraction & rcf );
Friend CFraction operator-(const CFraction & lcf, const CFraction & rcf );
Friend CFraction operator * (const CFraction & lcf, const CFraction & rcf );
Friend CFraction operator/(const CFraction & lcf, const CFraction & rcf );

// Relational operators
Friend bool operator> (const CFraction & lcf, const CFraction & rcf );
Friend bool operator <(const CFraction & lcf, const CFraction & rcf );
Friend bool operator >=( const CFraction & lcf, const CFraction & rcf );
Friend bool operator <= (const CFraction & lcf, const CFraction & rcf );
Friend bool operator = (const CFraction & lcf, const CFraction & rcf );
Friend bool operator! = (Const CFraction & lcf, const CFraction & rcf );


// Obtain the + and-target operators.
CFraction operator + ();
CFraction operator -();
};

 

Void CFraction: simplify ()
{
Int v1 = nume;
Int v2 = deno;
While (v2)
{
Int temp = v2;
V2 = v1 % v2;
V1 = temp;
}
Nume/= v1;
Deno/= v1;
If (deno <0)
{
Deno =-deno;
Nume =-nume;
}
}

// Output overload
Ostream & operator <(ostream & out, const CFraction & cf)
{
Out <cf. nume <'/' <cf. deno;
Return out;
}

// Input overload
Istream & operator> (istream & in, CFraction & cf)
{
Char ch;
While (1)
{
In> cf. nume> ch> cf. deno;
If (cf. deno = 0)
Cerr <"the denominator is 0. Enter \ n again ";
Else if (ch! = '/')
Cerr <"format error (such as m/n) Please input \ n again ";
Else break;
}
Return in;
}
// Addition overload
CFraction operator + (const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. deno + lcf. deno * rcf. nume;
Cf. deno = lcf. deno * rcf. deno;
Cf. simplify ();
Return cf;
}
// Subtraction overload
CFraction operator-(const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. deno-rcf. nume * lcf. deno;
Cf. deno = lcf. deno * rcf. deno;
Cf. simplify ();
Return cf;
}
// Multiplication overload
CFraction operator * (const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. nume;
Cf. deno = lcf. deno * rcf. deno;
Cf. simplify ();
Return cf;
}
// Division overload
CFraction operator/(const CFraction & lcf, const CFraction & rcf)
{
CFraction cf;
Cf. nume = lcf. nume * rcf. deno;
Cf. deno = lcf. deno * rcf. nume;
Cf. simplify ();
Return cf;
}
// Obtain positive and Heavy Loads
CFraction: operator + ()
{
Simplify ();
If (nume <0)
Nume =-nume;
Return * this;
}
// Obtain the negative overload
CFraction: operator -()
{
Simplify ();
Nume =-nume;
Return * this;
}


// Heavy load greater than the number
Bool operator> (const CFraction & lcf, const CFraction & rcf)
{
Int l_nume = lcf. nume * rcf. deno;
Int r_nume = rcf. nume * lcf. deno;
Int common_deno = lcf. deno * rcf. deno;
If (l_nume-r_nume) * common_deno> 0) return true;
Return false;
}

// Less than heavy load
Bool operator <(const CFraction & lcf, const CFraction & rcf)
{
Return! (Lcf> rcf );
}

// Equals to overload
Bool operator = (const CFraction & lcf, const CFraction & rcf)
{
Return lcf. nume = rcf. nume & lcf. deno = rcf. deno;
}

// Not equal to overload
Bool operator! = (Const CFraction & lcf, const CFraction & rcf)
{
Return! (Lcf = rcf );
}

//
Bool operator> = (const CFraction & lcf, const CFraction & rcf)
{
If (lcf <rcf) return false;
Return true;
}

//
Bool operator <= (const CFraction & lcf, const CFraction & rcf)
{
If (lcf> rcf) return false;
Return true;
}

Int main ()
{
CFraction cf1;
CFraction cf2;
Cin> cf1> cf2;
Cout <"cf1:" <cf1 <'\ t' <"cf2:" <cf2 <endl
<"Cf1 + cf2:" <cf1 + cf2 <endl
<"Cf1-cf2:" <cf1-cf2 <endl
<"Cf1 * cf2:" <cf1 * cf2 <endl
<"Cf1/cf2:" <cf1/cf2 <endl
<"+ Cf1:" <+ cf1 <endl
<"-Cf1:" <-cf1 <endl
<"+ Cf2:" <+ cf2 <endl
<"-Cf2:" <-cf2 <endl
<"Cf1> cf2? 1/YES 0/NO "<(cf1> cf2) <endl
<"Cf1 <cf2? 1/YES 0/NO "<(cf1 <cf2) <endl
<"Cf1 = cf2? 1/YES 0/NO "<(cf1 = cf2) <endl
<"Cf1! = Cf2? 1/YES 0/NO "<(cf1! = Cf2) <endl
<"Cf1> = cf2? 1/YES 0/NO "<(cf1> = cf2) <endl
<"Cf1 <= cf2? 1/YES


Return 0;
}

 

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.