• 自考新教材--p98


    源程序:

    #include <iostream>

    #include <string>

    using namespace std;

    class myDate

    {

    public:

    myDate();

    myDate(int);

    myDate(int,int);

    myDate(int,int,int);

    void setDate(int a, int b, int c)

    {

    year = a;

    month = b;

    day = c;

    }

    void printDate() const

    {

    cout << year << month << day << endl;

    }

    private:

    int year;

    int month;

    int day;

    };

    //以下为函数的定义

    myDate::myDate()

    {

    year = 1970;

    month = 1;

    day = 25;

    }//不带参数

    myDate::myDate(int d) :year(1970), month(1)

    {

    day = d;

    }

    myDate::myDate(int m, int d) : year(1970)

    {

    month = m;

    day = d;

    }

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

    {

    year = y;

    month = m;

    day = d;

    }

    class Student

    {

    public:

    Student();

    Student(string);

    void setStudent(string,myDate);

    void setStudent(string);

    void setName(string);

    string getName();

    void setBirthday(myDate);

    myDate getBirthday();

    void printStudent() const;

    public:

    string name;

    myDate birthday;

    };

    Student::Student() :name("Noname"), birthday(myDate()) {};

    Student::Student(string n) :name(n), birthday(myDate()) {};

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

    {

    name = s;

    birthday.setDate(1971,9,12);

    return;

    }

    void Student::setStudent(string s)

    {

    name = s;

    myDate d;

    birthday.setDate(1982,3,14);

    return;

    }

    void Student::setName(string n)

    {

    name = n;

    return;

    }

    string Student::getName()

    {

    return name;

    }

    void Student::setBirthday(myDate d)

    {

    birthday.setDate(1999,12,19);

    return;

    }

    myDate Student::getBirthday()

    {

    return birthday;

    }

    void Student::printStudent() const

    {

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

    birthday.printDate();

    cout << endl;

    }

    int main()

    {

    myDate d0;

    myDate d1(25);

    myDate(10,25);

    myDate(1970,10,25);

    myDate birthday(1970,1,12);

    Student stud;

    Student ss[2];

    int y, m, d, i;

    string name_;

    stud.printStudent();

    for (i = 0; i < 2; i++)

    ss[i].printStudent();

    for (i = 0; i < 2; i++)

    {

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

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

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

    }

    for (i = 0; i < 2; i++)

    ss[i].printStudent();

    system("pause");

    return 0;

    }

     

  • 相关阅读:
    LeetCode算法第一题
    基础题整理
    MongoDB 之 Limit 选取 Skip 跳过 Sort 排序 MongoDB
    MongoDB 之 $关键字 及 $修改器 $set $inc $push $pull $pop MongoDB
    mongodb的数据类型
    mongodb的增删改查
    session与cookie之间的关系
    Python 格式化输出的3种方式
    判断python字典中key是否存在的两种方法
    python实现忽略大小写对字符串列表排序的方法
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11996746.html
Copyright © 2020-2023  润新知