C++調用外部文件中的函數


方法一、 
dev_cpp中建一項目,把下列文件添中到項目中
主函數main001.cpp,其中添加 #include "lx.h",

方法二: 

在主函數main001.cpp中直接中添加 #include "add.cpp",#include " sub.cpp",把這三個文件放在同一目錄下。

 

 1 #include<iostream>
 2 #include   "lx.h"
 3 using namespace std;
 4 int main()
 5 {
 6   int a,b;
 7   cout<<"input a,b: ";
 8   cin>>a>>b;
 9   add(a,b);
10   subtraction(a,b);
11 }

頭文件lx.h

1 #include <iostream>
2 int add(int ,int );
3 int subtraction(int ,int);

函數文件add.cpp

1  #include<iostream>
2  using namespace std;
3  int add(int a,int b)
4  {
5      cout<<a<<"+"<<b<<"="<<a+b<<endl; 
6      return 0;
7  }

 函數文件sub.cpp

1 #include<iostream>
2 using namespace std;
3 int subtraction(int a,int b)
4 {
5     cout<<a<<"-"<<b<<"="<<a-b<<endl;
6     return 0;
7 }

 

 方法二:在主函數main001.cpp直接中添加 #include "add.cpp",#include " sub.cpp",把這三個文件放在同一目錄下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM