• 自考新教材--p75


    源程序:

    #include <iostream>

    #include <string>

    using namespace std;

    class myDate

    {

    public:

    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 << " 生日:";

    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;

    }

    运行结果:

  • 相关阅读:
    hdu 1106 排序(排序)
    hdu 1040 As Easy As A+B(排序)
    hdu 1029 Ignatius and the Princess IV(排序)
    mysql-9索引
    mysql-8 alter命令
    mysql-7事务管理
    mysql-6正则表达式
    http协议
    9-2交互体验
    9-2专项测试下午
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11975260.html
Copyright © 2020-2023  润新知