类的封装性与隐敝性

1.类声明和成员函数的分离

       在一个工程里面,头文件包含类的声明。另一个文件定义类成员函数。

        最后还有一个主函数的文件。

//student.cpp,类成员函数的定义 
#include<iostream>
#include"student.h"
using namespace std;
void student::display( )
{
  cout<<num<<endl;
   cout<<name<<endl;
    cout<<sex<<endl;
  
}
void student::set( )
{
  cin>>num;
  cin>>name;
  getchar();
  cin>>sex;
}
//student.h,类的声明 
#include<string>
using namespace std;
class student
{
 public:
    void set();
    void display();
 private:
     int num;
     char name[20];
     char sex;
};
//main.cpp
#include <cstdlib>
#include <iostream>
#include "student.h"
using namespace std;

int main(int argc, char *argv[])
{
    student stud;
    stud.set();
    stud.display();
    system("PAUSE");
    return EXIT_SUCCESS;
}
面向对象设计的几个名词,方法,是指对数据的操作。
 消息就是一个命令,由程序语句实现。stud就是对像。

posted on 2011-06-08 22:03  more think, more gains  阅读(348)  评论(0编辑  收藏  举报

导航