c++中头文件的后缀名是*.h

创建一个pro.h的头文件,里面声明两个函数和一个结构体

struct test
{
    int a;
    int b;
    int len();
    int area();
}                                //声明结构体test

int len(int a,int b);           //声明周长函数
int area(int a, int b);         //声明面积函数            

 

再创建一个pro.cpp函数来实现声明函数中的内容

int len(int a, int b)
{
    return 2 * (a + b);        //返回周长值   
}

int area(int a, int b)
{
    return a * b;               //返回面积值
}

 

在主程序中调用创建的头文件可以直接使用函数

#include<iostream>
#include"pro.h"
using namespace std;
int main()
{
    struct test t;
    cin >> t.a >> t.b;                       
    cout << len(t.a, t.b) << endl;
    cout << area(t.a, t.b) << endl;
}

 

c++头文件创建与使用

 

相关文章:

  • 2021-11-27
  • 2021-10-13
  • 2022-12-23
  • 2021-08-27
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
  • 2021-06-03
  • 2021-11-22
  • 2021-08-24
相关资源
相似解决方案