zoukankan      html  css  js  c++  java
  • 自考新教材--p76_2

    源程序:

    #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_;

    Student &sy = ss;   //sy是ss的别名

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

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

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

    sy.printStudent();

    system("pause");

    return 0;

    }

    运行结果:

  • 相关阅读:
    JAVA GUI设
    3.4 jmu-java-随机数-使用蒙特卡罗法计算圆周率的值 (10 分)
    问题:关于2.3 jmu-Java-02基本语法-03-身份证排序 (9 分)
    关于3.1 jmu-Java-03面向对象基础-01-构造函数与toString (3 分)
    linux vim文件编辑的常用命令
    linux的常用命令
    linux文件存储方式
    第一个java
    hdu 2795
    hdu 1394
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11975398.html
Copyright © 2011-2022 走看看