这段C++代码从书上拼起来的怎么不对
C/C++ code
另外 year month day是private里面的为什么可以 year=newyear; 不一定用year->newyear;
#include<iostream> using namespace std; #ifndef DATE_H #define DATE_H class Date { public: Date(int newYear, int newMonth, int newDay); int getYear(); void setYear(int newYear); private: int year; int month; int day; }; #endif Date::Date(int newYear, int newMonth, int newDay) { year=newYear;//this->year=newYear; month=newMonth;//this->month=newMonth; day=newDay;//this->day=newDay; } int Date::getYear() { return year; } void Date::setYear(int newYear) { year=newYear;//this->year=newYear; }; class Person { Person(int id, int year, int month ,int day); int getId(); Date *getBirthDate(); private: int id; Date * birthDate; }; Person::Person(int id, int year,int month, int day) { this->id=id; birthDate=new Date(year, month, day); } int Person::getId() { return id; } Date* Person::getBirthDate() { return birthDate; }; int main() { Person person(11112,1992,5,29); cout<<"birth year before the change is " <<person.getBirthDate()->getYear()<<endl; Date *pDate=person.getBirthDate(); pDate->setYear (2011); cout<<"birth yera after the change is"<<person.getBirthDate()->getYear()<<endl; system("PAUSE"); return 0; }
另外 year month day是private里面的为什么可以 year=newyear; 不一定用year->newyear;
作者: miaomiao7777 发布时间: 2011-06-14
什么意思?
作者: healer_kx 发布时间: 2011-06-14
你的person类中应该有声明public,如果没有的话,默认是private.改正如下:
class Person
{
public:
Person(int id, int year, int month ,int day);
int getId();
Date *getBirthDate();
private:
int id;
Date * birthDate;
};
class Person
{
public:
Person(int id, int year, int month ,int day);
int getId();
Date *getBirthDate();
private:
int id;
Date * birthDate;
};
作者: zhizichina 发布时间: 2011-06-14
person类都没有public的方法了,当然不行了。
作者: healer_kx 发布时间: 2011-06-14