天天看點

myDate類的對象作為student類的私有變量

源程式:

#include <iostream>

#include <string>

using namespace std;

class myDate

{

private:

int year,month,day;

public:

myDate()

{

year=1970;

month=1;

day=1;

}

myDate(int y,int m,int d)

{

year=y;

month=m;

day=d;

}

void setDate(int y,int m,int d)

{

year=y;

month=m;

day=d;

}

void setDate(myDate oneD) //oneD是一個對象

{

day=oneD.day;

}

myDate getDate()

year=oneD.year;

month=oneD.month;

{

return *this;

}

void setYear(int y)

{

year=y;

}

int getMonth()

{

return month;

}

inline void myDate::printDate() const

{

cout<<year<<month<<day;

return;

}

}; //類結束

class student

{

private:

string name;

myDate birthday;

public:

void setStudent(string s,myDate d)

{

name=s;

birthday.setDate(d);

return;

}

void setName(string n)

{

name=n;

return;

}

string getName()

{

return name;

}

void setBirthday(myDate d)

{

birthday.setDate(d);

return;

}

myDate getBirthday()

{

return birthday;

}

void printStudent() const

{

cout<<name;

birthday.printDate();

cout<<endl;

}

};