试试这个
Date.h
#ifndef DATE_H
#define DATE_H
#include<string>
using namespace std;
class Date{
public:
Date(int month, int day, int year);
int getMonth() const;
int getDay() const;
int getYear() const;
private:
int month;
int day;
int year;
};
#endif
Date.cpp
#include "Date.h"
#include<string>
using namespace std;
Date::Date(int month, int day, int year) {
this->month = month;
this->day = day;
this->year = year;
}
int Date::getMonth() const{
return month;
}
int Date::getDay() const{
return day;
}
int Date::getYear() const{
return year;
}
Appointment.cpp
#include "Appointment.h"
#include "Date.h"
#include<sstream>
#include<string>
using namespace std;
Appointnment::Appointment(String description, int month, int day, int yr, int hr, int min)
{
this->description = description;
this->month = month;
this->day = day;
this->yr = yr;
this->hr = hr;
this->min = min;
}
void Appointinment::getDate()
{
//cannot change calling object nor its date object member, just return it
}
}
我试图实现getDate()
功能,但由于此类被指定为约会,我无法理解如何返回Date。我有什么资源可以解决这个具体问题吗?
答案 0 :(得分:0)
你的代码看起来不错。但是当返回soemthing时,你需要一个指向Data的指针,或者引用Date或直接对象。
以下任何一种都可以使用:
Date Appointinment::getDate() const
const Date& Appointinment::getDate() const
const Date* Appointinment::getDate() const
从背景来判断,也许首先是你想要的。
答案 1 :(得分:0)
const Date& Appointinment::getDate()
{
return Date(this->month, this->day, this->yr);
}
这是最常见的方式。