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

  • 相关阅读:
    hdu 1028 Ignatius and the Princess III (n的划分)
    CodeForces
    poj 3254 Corn Fields (状压DP入门)
    HYSBZ 1040 骑士 (基环外向树DP)
    PAT 1071 Speech Patterns (25)
    PAT 1077 Kuchiguse (20)
    PAT 1043 Is It a Binary Search Tree (25)
    PAT 1053 Path of Equal Weight (30)
    c++ 常用标准库
    常见数学问题
  • 原文地址:https://www.cnblogs.com/timssd/p/4781151.html
Copyright © 2011-2022 走看看