zoukankan      html  css  js  c++  java
  • c++头文件创建与使用

    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;
    }

  • 相关阅读:
    数据库
    数据库
    数据库
    数据库
    数据库
    数据库
    windows
    LeetCode : Word Pattern
    LeetCode : Perfect Number
    LeetCode : Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/f1veseven/p/13636307.html
Copyright © 2011-2022 走看看