码迷,mamicode.com
首页 > 其他好文 > 详细

对象2 构造,析构

时间:2015-03-22 00:18:50      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:


#include <iostream>
#include <string>
#include <fstream>

class StoreQuote
{
public:
    std::string quote, speaker;
    std::ofstream fileOutput;

    StoreQuote();
    ~StoreQuote();

    void inputQuote();
    void inputSpeaker();
    bool write();
};

StoreQuote::StoreQuote()
{
    fileOutput.open("test.txt", std::ios::app);
}

StoreQuote::~StoreQuote()
{
    fileOutput.close();
}

void StoreQuote::inputQuote()
{
    getline(std::cin, quote);
}

void StoreQuote::inputSpeaker()
{
    getline(std::cin, speaker);
}

bool StoreQuote::write()
{
    if(fileOutput.is_open())
    {
        fileOutput << quote << "!" << speaker << std::endl;
        return true;
    }
    else
    {
        return false;
    }
}


int main()
{
    StoreQuote sq;

    sq.inputQuote();
    sq.inputSpeaker();

    if(sq.write())
    {
        std::cout << "write ok" << std::endl;
    }
    else
    {
        std::cout << "write err" << std::endl;
    }
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */

 

对象2 构造,析构

标签:

原文地址:http://www.cnblogs.com/i80386/p/4356553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!