自考新教材--p75

源程序:

#include <iostream>

#include <string>

using namespace std;

class myDate

{

public:

扫描二维码关注公众号,回复: 8057011 查看本文章

myDate();

myDate(int, int, int);

void setDate(int, int, int);

void setDate(myDate);

myDate getDate();

void setYear(int);

int getMonth();

void printDate() const;

private:

int year, month, day;

};

//在类体外定义成员函数

myDate::myDate()

{

year = 1970;

month = 1;

day = 1;

}

myDate::myDate(int y, int m, int d)

{

year = y;

month = m;

day = d;

}

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

{

year = y;

month = m;

day = d;

return;

}

void myDate::setDate(myDate oneD)

{

year = oneD.year;

month = oneD.month;

day = oneD.day;

return;

}

myDate myDate::getDate()

{

return *this;

}

void myDate::setYear(int y)

{

year = y;

return;

}

int myDate::getMonth()

{

return month;

}

void myDate::printDate() const

{

cout << year << "/" << month << "/" << day;

return;

}

class Student

{

public:

void setStudent(string, myDate);

void setName(string);

string getName();

void setBirthday(myDate);

myDate getBirthday();

void printStudent() const;

private:

string name;

myDate birthday;

};

//在类体外定义成员函数

void Student::setStudent(string s, myDate d)

{

name = s;

birthday.setDate(d);

return;

}

void Student::setName(string n)

{

name = n;

return;

}

string Student::getName()

{

return name;

}

void Student::setBirthday(myDate d)

{

birthday.setDate(d);

return;

}

myDate Student::getBirthday()

{

return birthday;

}

void Student::printStudent() const

{

cout << "姓名:" << name << "\t 生日:";

birthday.printDate();

cout << endl;

}

int main()

{

Student ss;

int y, m, d;

string name_;

cout << "请输入学生的姓名和生日,生日以\"年 月 日\"的次序输入:";

cin >> name_ >> y >> m >> d;

ss.setStudent(name_,myDate(y,m,d));

ss.printStudent();

system("pause");

return 0;

}

运行结果:

猜你喜欢

转载自www.cnblogs.com/duanqibo/p/11975260.html