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",把这三个文件放在同一目录下。

猜你喜欢

转载自www.cnblogs.com/ybqjymy/p/12396219.html