c++ - How to compare two objects (the calling object and the parameter) in a class? -


I am writing a "date" class for assignment and I'm having trouble doing the function one.

This is the header for the class.

  class date {public: date (); // constructor date without parameters (int m, int d, int y); // Constructor with // Parameter // Accessories in Amnath (); // Diamond enters the size of the gateway; Int gear (); // set the mutator bool (int m, int d, int y); Bull Set Format (Four F); // standard input and output routine zero input (); Show zero (); Zero increment (int number = 1); Int Comparison (Constellation and D); Private: int month, month of month, // day variable year; // variable variables in year; };  

The member function I am trying to do is the int compare (constant date and d) function. I need this function to compare two functions (calling object and parameter), and should return it: -1 If the calling object comes first in chronological order, 0 if the object is the same date, and 1 if the parameter object The first comes from chronological order.

I have tried to make a simple statement with the operator == but I get errors.

  if (D1 == D2) COAT & LT; & Lt; "Dates are the same"; Return (0);  

After the objects are created, the function should be deemed as D1. Compared (D2)

Thanks in advance!

  int date: compare (concat date and d) {if (year & lt ; D year) {return -1; } And if (year> D.Year) {return 1; } And if (month> month lt) {return -1; } And if (month> D. month) {return 1; } // 0 for the same day return; }  

Usually, you also want to provide the overloaded comparison operator, for example (even within class definition):

  bool operator == (Const date & amp; D) const {return! Compare (D); } The Bull Operator & lt; (Constant Date and D) Cost {Compare Comparison (D) & lt; 0; } ... // Promotions: Operator  

: Better ways than PS: () - just the other answers Check it is a very simple and readable, but consistent with your specification.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -