C ++ class (9)-heavy arithmetic Relational operators

Source: Internet
Author: User

You are welcome to repost the post, but please mark the author as "nine days Yan Ling". Of course, the link to this post is better.

I could have explained all the reloads in one lecture. Because it was too late yesterday and very difficult, I only talked about the reload input and output operators. Today I will not talk about the concept, let's take a look at "talking about C ++ class (8) -- overload input and output operators" in the previous lecture. Today, we will add some additional operators, which are almost the same, however, I feel that I have a completely different idea after I have entered and debugged it. I wrote all operators except subscripts and member access operators in the following example. You can analyze and debug them yourself. I only debugged a part of the main program.

Example 9.0:

# Include <string>
# Include <iostream>
Using namespace STD;
Class fruit // defines a class named fruit
{
Double price; // defines a price member to indicate the price
Double weight; // defines a weight member to indicate the weight.
String color; // defines the color of a color member.
String name; // defines the name of a name Member.
Double convalue; // defines a convalue member to indicate the total value.
Public:
Friend istream & operator> (istream &, Fruit &); // overload the input Operator
Friend ostream & operator <(ostream &, const Fruit &); // reload the output Operator
Fruit & operator + = (const Fruit & orig) // reload the composite addition operator. The behavior is unknown, so it is strange.
{
If (name! = Orig. Name)
{
Name = Name + "mix" + orig. Name;
}
If (color! = Orig. color)
{
Color = color + "mix" + orig. color;
}
Weight + = orig. weight;
Convalue + = orig. convalue;
Price = convalue/weight;
Return * this;
}
Friend fruit operator + (const Fruit & lf, const Fruit & RF); // overload the addition operator. You must use friend yuan because there are two operands.
Fruit & operator = (Fruit & orig) // overload value assignment operator
{
Name = orig. Name;
Color = orig. color;
Price = orig. price;
Weight = orig. weight;
Convalue = orig. convalue;
Return * this;
}
Bool operator = (const Fruit & orig) // overload equal Operators
{
Return convalue = orig. convalue;
}
Bool Operator! = (Const Fruit & orig) // overload unequal Operators
{
Return! (* This = orig );
}
Bool operator <(const Fruit & orig) // overload less than Operator
{
Return convalue <orig. convalue;
}
Bool operator> (const Fruit & orig) // reload greater than the operator
{
Return convalue> orig. convalue;
}
Bool operator <= (const Fruit & orig) // overload operators smaller than or equal
{
Return * THIS <orig | * This = orig;
}
Bool operator> = (const Fruit & orig) // overload operator greater than or equal
{
Return * This> orig | * This = orig;
}
Void print () // define an output member print ()
{
Cout <weight <"kilogram" <color <"" <name
<"Worth" <convalue <"yuan." <Endl;
}
Fruit (const double & DP, const double & DW, const string & CST = "green ",/
Const string & NST = "apple"): price (DP), weight (DW), color (CST), name (NST) // Constructor
{
Convalue = price * weight;
}
Fruit (const Fruit & orig) // defines a replication Constructor
{
Name = orig. Name;
Color = orig. color;
Price = orig. price;
Weight = orig. weight;
Convalue = orig. convalue;
}
~ Fruit () // The Destructor does not need to be defined, so you can use the system.
{
}
};
Ostream & operator <(ostream & out, const Fruit & S)
{
Cout <S. weight <"kilogram" <S. Color <"<S. Name
<"Worth" <S. convalue <"yuan .";
Return out;
}
Istream & operator> (istream & in, Fruit & S)
{
Cout <"Price :";
In> S. price;
Cout <"Weight :";
In> S. weight;
Cout <"What :";
In> S. Color> S. Name;
S. convalue = S. Price * S. weight;
If (! In)
Cerr <"wrong input! "<Endl;
Return in;
}
Fruit operator + (const Fruit & lf, const Fruit & RF)
{
Fruit RET (LF );
RET + = RF;
Return ret;
}

Int main ()
{
Fruit greenapple (3.0, 10.0 );
Fruit redapple (4.5, 10.0, "Red ");
Fruit mixapple = greenapple + redapple;
If (greenapple = redapple)
{
Cout <"they are the same :";
}
If (greenapple! = Redapple)
{
If (greenapple> redapple)
Cout <"biger:" <greenapple <Endl;
If (greenapple <redapple)
Cout <"biger:" <redapple <Endl;
}
Cout <greenapple <Endl;
Cout <redapple <Endl;
Cout <"mix:" <mixapple <Endl;
 
Return 0;
}

The program is very simple, and there are enough comments, so I will not explain them much. Some suggestions are to say that the operators of the two parameters will use the membership function, because the member function has a hidden parameter This, operators can be called to simplify each other. For example, operator + uses operator + =, operator> =, and operator>. For the value assignment operation, remember to return a reference to * This, for continuous use, such as a = B = C or D + = e + = f.

If you think the form of a = B = C is quite understandable, but you are unfamiliar with the form of D + = e + = f and do not know what is going on, I will give an example.

Example 9.1:

# Include <iostream>
Using namespace STD;
Int main ()
{
Int A = 1;
Int B = 3;
Int c = 4;
Int d = 6;
D + = a + = B + = C;
Cout <"A:" <A <Endl
<"B:" <B <Endl
<"C:" <C <Endl
<"D:" <D <Endl;
Return 0;
}

When you see the result, you will understand that the + = Operator is read from the right, and why do we need to return * This is a good understanding. We actually return the left operand.

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.