如何比较类中的两个对象(调用对象和参数)? [英] How to compare two objects (the calling object and the parameter) in a class?

查看:164
本文介绍了如何比较类中的两个对象(调用对象和参数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个作业写了一个Date类,我在执行一个函数时遇到麻烦。



这是类的头文件。

  
{
public:
Date(); //没有参数的构造函数
Date(int m,int d,int y); //带参数的构造函数。

//访问器
int GetMonth(); //返回钻石的大小
int GetDay();
int GetYear();

// mutators
bool Set(int m,int d,int y);
bool SetFormat(char f);

//标准输入和输出例程
void Input();
void Show();
void Increment(int numDays = 1);
int比较(const Date& d);

private:
int month,//月变量
day,//日变量
年; //年变量
char格式;
};

我想要的成员函数是int Compare(const Date& d)函数。我需要这个函数来比较两个Date对象(调用对象和
参数),并且应该返回:-1如果调用对象先到达
按时间顺序,0如果对象是相同的日期,如果参数对象
按时间先后顺序排列。



我试过用==运算符做一个简单的if语句,但我得到错误。

  if(d1 == d2)
cout< 日期是一样的;
return(0);

创建对象后,函数应该像这样调用d1.Compare p>

提前谢谢!

解决方案

  int Date :: Compare(const Date& d){

if(year< d.year){
return -1;
}
else if(year> d.year){
return 1;
}
else if(month< d.month){
return -1;
}
else if(month> d.month){
return 1;
}
//一天一样

return 0;
}

通常,您还需要提供重载的比较运算符,例如也在类定义内):

  bool operator ==(const Date& d)const {
return!Compare (d)。
}

bool operator< (const Date& d)const {
return Compare(d)< 0;
}

... //考虑使用boost :: operators

PS:有更聪明的实现 Compare() - 只是检查其他答案。这个很简单易读,但完全符合你的规格。


I am writing a "Date" class for an assignment and I am having trouble doing one the of the functions.

This is the header file for the class.

class Date
{
public:
Date();                                  // Constructor without parameters
Date(int m, int d, int y); // Constructor with parameters.

// accessors
int GetMonth();               // returns the size of the diamond
int GetDay();
int GetYear();

// mutators
bool Set(int m, int d, int y);
bool SetFormat(char f);

// standard input and output routines
void Input();             
void Show();              
void Increment(int numDays = 1);                 
int Compare(const Date& d);     

private:
int month,                    // month variables
    day,                 // day variable
    year;               // year variable
char format;
};

The member function I am trying to make is the int Compare(const Date& d) function. I need this function to compare two Date objects (the calling object and the parameter), and should return: -1 if the calling object comes first chronologically, 0 if the objects are the same date, and 1 if the parameter object comes first chronologically.

I have tried doing a simple if statement with the == operator but I get errors.

  if (d1 == d2)
     cout << "The dates are the same";
     return (0);

After the objects are created, the function should be called like this d1.Compare(d2)

Thank you in advance!

解决方案

int Date :: Compare (const Date& d) {

   if (year<d.year) {
      return -1;
   }
   else if (year>d.year) {
      return 1;
   }
   else if (month<d.month) {
      return -1;
   }
   else if (month>d.month) {
      return 1;
   }
   // same for day

   return 0;
}

Usually, you'lll also want to provide overloaded comparison operators, for example (also within the class definition):

bool operator == (const Date& d) const {
   return !Compare(d);
}

bool operator < (const Date& d) const {
  return Compare(d)<0;   
}

... // consider using boost::operators

PS: There are smarter implementations of Compare() - just check the other answers. This one is pretty straightforward and readable, but conforms exactly to your specification.

这篇关于如何比较类中的两个对象(调用对象和参数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆