头文件(shape.h):

class Circle
{
public:
    double calculate_perimeter(double radius);
    double calculate_area(double radius);
private:
    int radius;
};

 

shape.cpp:

#include "shape.h"

#define PI 3.14159

double Circle::calculate_perimeter(double radius)
{
    return 2 * PI * radius;
}

double Circle::calculate_area(double radius)
{
    return PI * radius * radius;
}

 

主程序

test.cpp

#include <iostream>
#include "shape.h"

using namespace std;

int main()
{
    int radius;
    
    cout<< "Please input radius of the circle:";
    cin>>radius;
    Circle c;
    cout << "Circle Perimeter: " << c.calculate_perimeter(radius) <<endl;
    cout << "Circle Area: " << c.calculate_area(radius) <<endl;
    return 0;
}

 

ubuntu下编译命令:

g++ -o test.exe shape.cpp test.cpp

 

运行结果:

Please input radius of the circle:5
Circle Perimeter: 31.4159
Circle Area: 78.5397

 

如果我门把头文件放在某个draw目录下

在主程序include的路径需要修改一下

#include "draw/shape.h"

 

编译指令:

g++ -o test.exe draw/*.cpp test.cpp

 

 

 


 

 

技术改变世界