⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pr11031.cpp

📁 c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出版社的光盘
💻 CPP
字号:
////////////////////////////////////////
// File Name: pr11031.cpp
////////////////////////////////////////
#include <iostream>
#include <cstring>

////////////////////////////////////////
// The Date class.
////////////////////////////////////////
class Date
{
    int mo, da, yr;
    char* month;

public:
    Date(int m = 0, int d = 0, int y = 0);

    // Copy constructor.
    Date(const Date&);

    ~Date();
    void display() const;
};

// The constructor definition.
Date::Date(int m, int d, int y)
{
    static char* mos[] =
    {
        "January", "February", "March", "April", "May",
        "June", "July", "August", "September", "October",
        "November", "December"
    };

    mo = m; da = d; yr = y;
    if (m != 0)
    {
        month = new char[std::strlen(mos[m-1])+1];
        std::strcpy(month, mos[m-1]);
    }
    else
        month = 0;
}

// The copy constructor definition.
Date::Date(const Date& dt)
{
    mo = dt.mo;
    da = dt.da;
    yr = dt.yr;
    if (dt.month != 0)
    {
        month = new char [std::strlen(dt.month)+1];
        std::strcpy(month, dt.month);
    }
    else
        month = 0;
}

// The destructor definition.
Date::~Date()
{
    delete [] month;
}

// The display member function.
void Date::display() const
{
    if (month != 0)
        std::cout << month << ' ' << da << ", "
                  << yr << std::endl;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    // First date.
    Date birthday(6,24,1940);
    birthday.display();

    // Second date.
    Date newday = birthday;
    newday.display();

    // Third date.
    Date lastday(birthday);
    lastday.display();

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -