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;
}

};


int main()
{
myDate D;
student S;
S.setStudent("zhangsan",D);
S.printStudent();
return 0;
}

原文地址:https://www.cnblogs.com/duanqibo/p/15497172.html