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

  • 相关阅读:
    HDFS under replicated blocks
    docker-compose
    shell $* 和$@ 的区别以及运算操作
    ajax与文件上传
    Django之模型层(多表操作)
    Django之模型层(单表操作)
    Django之模板层
    Django之视图层
    Django之路由层
    Django之web应用、http协议和django简介
  • 原文地址:https://www.cnblogs.com/timssd/p/4781151.html
Copyright © 2011-2022 走看看