1.多文件

100阅读 0评论2014-03-29 whoamifxy
分类:C/C++

文件c.h

点击(此处)折叠或打开

  1. class MyClass
  2. {
  3.     private:
  4.         int x,y;
  5.     public:
  6.         void setValue(int x, int y);
  7.         void disPlay();
  8. };
文件fun.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "c.h"

  3. using namespace std;

  4. void MyClass::setValue(int x, int y)
  5. {
  6.     //MyClass::x = x 或者 this->x = x
  7.     //x = x 会出现错误!
  8.     MyClass::x = x;
  9.     MyClass::y = y;
  10. }

  11. void MyClass::disPlay()
  12. {
  13.     cout << "x = " << x << ",y = " << y << endl;
  14. }
文件main.cpp

点击(此处)折叠或打开

  1. #include "fun.cpp"

  2. int main()
  3. {
  4.     MyClass a;
  5.     a.setValue(1,2);
  6.     a.disPlay();
  7.     return 0;
  8. }

上一篇:没有了
下一篇:没有了