zoukankan      html  css  js  c++  java
  • C++ 多源码文件简单组织

    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

  • 相关阅读:
    记录ci框架中定时任务的执行
    2019 年MySQL面试题及答案
    Net线程问题解答(转)
    vs2005 Team System的版本
    ASP.NET 安全认证(如何运用 Form 表单认证)
    .net调用存储过程时的输出函数
    在服务器执行js脚本
    简单的批量更新(小技巧)
    UNION 和UNION ALL 的区别
    ServerVariable(环境变量)
  • 原文地址:https://www.cnblogs.com/timssd/p/4781151.html
Copyright © 2011-2022 走看看