zoukankan      html  css  js  c++  java
  • 自考新教材--p103(复制的构造函数)

    源程序:

    #include <iostream>

    #include <string>

    using namespace std;

    class myDate

    {

    private:

    int year, month, day;

    public:

    myDate();

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

    {

    year = y;

    month = m;

    day = d;

    }

    void setDate(myDate dd)

    {

    year = dd.year;

    month = dd.month;

    day = dd.day;

    }

    myDate(int, int, int);

    void printDate() const;

    };

    myDate::myDate() :year(1990), month(9), day(19) {}

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

    {

    year = y;

    month = m;

    day = d;

    }

    void myDate::printDate() const

    {

    cout << "year:" << year << "month:" << month << "day:" << day << endl;

    }

    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;

    Student(const Student &);  //声明复制的构造函数

    private:

    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(d);

    return;

    }

    void Student::setStudent(string s)

    {

    name = s;

    myDate d;

    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;

    }

    Student::Student(const Student &s)   //定义复制的构造函数

    {

    name = s.name;

    birthday = s.birthday;

    }

    /*

    int main()

    {

    myDate birthday;

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

    }

    int main()

    {

    Student stud;

    stud.printStudent();

    stud.setName("chen");

    stud.printStudent();

    Student *spointer[2] = {new Student(),&stud};//使用构造函数生成对象指针数组

    Student sy[2] = {Student(),stud};

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

    spointer[i]->printStudent();

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

    sy[i].printStudent();

    stud.setName("wang");

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

    spointer[i]->printStudent();

    spointer[0]->setName("liang");

    spointer[1]->setName("liu");

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

    spointer[i]->printStudent();

    stud.printStudent();

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

    sy[i].printStudent();

    system("pause");

    return 1;

    }

    */

    int main()

    {

    Student stud;

    Student ss[2] = {stud,Student()};

    stud.printStudent();

    stud.setName("111111");

    ss[0] = Student(stud);

    ss[1] = Student();

    stud.printStudent();

    ss[0].printStudent();

    ss[1].printStudent();

    system("pause");

    return 1;

    }

    运行结果:

  • 相关阅读:
    BZOJ1819 [JSOI]Word Query电子字典 Trie
    洛谷2973 [USACO10HOL]赶小猪Driving Out the Piggi… 概率 高斯消元
    BZOJ2669 [cqoi2012]局部极小值 状压DP 容斥原理
    BZOJ5047 空间传送装置 2017年9月月赛 最短路 SPFA
    BZOJ5045 打砖块 2017年9月月赛 其他
    BZOJ1858 [Scoi2010]序列操作 线段树
    BZOJ1826 [JSOI2010]缓存交换 堆 贪心
    BZOJ1898 [Zjoi2005]Swamp 沼泽鳄鱼 矩阵
    BZOJ1878 [SDOI2009]HH的项链 树状数组 或 莫队
    BZOJ1875 [SDOI2009]HH去散步 矩阵
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12022046.html
Copyright © 2011-2022 走看看