构造函数和句柄类

for(int i=0;i<100;i++)
 cout<<i<<endl;
新的标准C++说明书上说,一个在for循环的控制表达式中定义的循环计数器只在该
循环内有效。

//:0720.H--Handle classes
#include "stdafx.h"
#include <stdlib.h>
#include <assert.h>
#ifndef HANDLE_H_
#define HANDLE_H_
/*句柄类
  可见的实现部分
  减少重复编译*/
class handleu{ 
public:
    handleu();
 ~handleu();
 int read();
 void change(int,int);//仅仅是声明 声明可以不带参数
private:
 struct cheshire;
 cheshire *smile;
};
#endif

// 0720.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "0720.h"
#include <iostream>
using namespace std;

struct handleu::cheshire
{
 int i;
 int j;
};  //注意这里

handleu::handleu()//创建类的对象时,编译器自动调用
{
 smile=(cheshire*)malloc(sizeof(cheshire));
 assert(smile);
 smile->i=1;
 smile->j=1;
}
handleu::~handleu()
{
 cout<<"析构函数被调用了";
  free(smile);
 
}

int handleu::read()
{
 return smile->i+smile->j;
}

void handleu::change(int x,int y)
{
 smile->i+=x;
 smile->j+=y;
}

int _tmain(int argc, _TCHAR* argv[])
{
 //存取控制的好处 存取控制并不是面向对象的特征,但它为类的创建者提供了
    // 很有价值的访问控制。类的用户可以清楚地看到,什么可以用,什么应该忽略。
 // 更重要的是,他保证了类的用户不会依赖任何类的实现细节。
 handleu u;
 cout<<u.read()<<endl;
 u.change(5,8);
 cout<<u.read()<<endl;
 cout<<"aaaa"<<endl;
// getchar();
 getchar();
 return 0;
}

你可能感兴趣的:(构造函数)