码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 多源码文件简单组织

时间:2015-09-04 14:00:01      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

C++ 多源码文件简单组织

基本上和C的是一样的,只不过C++的方法要在类中声明。看一个简单实例。
ainimal.h  类里面对外公开的信息。


点击(此处)折叠或打开

  1. #ifndef _ANIMAL_H__
  2. #define _ANIMAL_H__
  3. #include <iostream>
  4. using namespace std;
  5. class Animal{
  6.         private:
  7.                 string name;
  8.         public:
  9.         void print(void);
  10.         Animal(string name){this->name=name;}
  11. };
  12. #endif

animal.cpp 类中方法实现的具体细节,或者是隐藏的部分,我新增了一个本文件私有的函数extra_info,static 修饰。


点击(此处)折叠或打开

  1. #include "animal.h"
  2. static string extra_info(){
  3.         return "Adding info from extra_info";
  4. }
  5. void Animal:: print(void){
  6.         cout << name << endl;
  7.         cout << extra_info() << endl;
  8. }


main.cpp 当然是这个类的使用者。


点击(此处)折叠或打开

  1. #include "animal.h"
  2. int main(void){
  3.         Animal ani("any");
  4.         ani.print();
  5.         cout << "some " << endl;
  6. }

编译


点击(此处)折叠或打开

  1. g++ animal.cpp main.cpp

运行


点击(此处)折叠或打开

  1. ./a.out

输出


点击(此处)折叠或打开

  1. any
  2. Adding info from extra_info
  3. some

好了,三个文件,一个类的头,一个类的实现,一个使用者
good luck

C++ 多源码文件简单组织

标签:

原文地址:http://www.cnblogs.com/timdes/p/4781151.html

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