构造函数与析构函数(其中有两点值得学习)

// 构造函数与析构函数.cpp : 定义控制台应用程序的入口点。
//要学习的两点:
//要想表现出析构函数,必须在对象外面加大括号!!
//  while (cin.get() != '\n')   num++;
#include "stdafx.h"
#include<iostream>
using namespace std;
class Count
{
public:
    Count();
    ~Count();
    void process();
private:
    int num;
};

Count::Count()
{
    num = 0;
}

Count::~Count()
{
    cout << "The length of sentence:" << num << endl;

}

void Count::process()
{
    while (cin.get() != '\n') //this is need to study!!
        num++;
}
int main()
{
    {
        Count c;
        c.process();
    }//要想表现出析构函数,必须在对象外面加大括号!!
    system("pause");
    return 0;
}
捕获

posted @ 2016-05-24 03:09  01Turing  阅读(151)  评论(0编辑  收藏  举报