文件结构如下:

header/swap.h
#ifndef SWAP_H
#define SWAP_H
#include <iostream>
using namespace std;
class MySwap{
private:
int a;
int b;
public:
MySwap(int a,int b);
void run();
void printInfo();
};
#endif
src/swap.cpp
#include "swap.h"
MySwap::MySwap(int a, int b){
this->a=a;
this->b=b;
}
void MySwap::run(){
int tmp=a;
a=b;
b=tmp;
}
void MySwap::printInfo(){
cout<<a<<' '<<b<<endl;
}
src/main.cpp
#include "swap.h"
int main(int argc, char const *argv[])
{
MySwap myswap(3,4);
cout<<"交换前:";
myswap.printInfo();
myswap.run();
cout<<"交换后:";
myswap.printInfo();
return 0;
}
CMakeList.txt
#指定最小编译版本
cmake_minimum_required(VERSION 2.8)
#执行工程名称
project(03DEMO)
#指定头文件路径 就是g++ 编译器 -I 选项
include_directories(./header)
#生成可执行程序
add_executable(swap_exe src/main.cpp src/swap.cpp)
编译并运行结果

1031

被折叠的 条评论
为什么被折叠?



