• C++头文件、源代码文件简单总结



    一、头文件

    头文件中写类的声明、结构的声明、函数原型、宏定义等
    注意不可以定义全局变量,否则可能会引起重复定义的错误

    • testStruct.h
          #pragma once
          #define MAXPRICE 200000
      
          //结构体定义,这个地方一定要加typedef(否则vs2019会报重复定义的错)
          typedef struct Book {
              char Name[50];
              int ID;
              double price;
          }book;
      
          //车辆类定义
          class Vehicle {
          public:
              char Brand[50];
              double price;
              int siteNum;
              Vehicle(void) {
                  price = 0;
                  siteNum = 0;
                  strcpy_s(Brand, "car");
              }
              Vehicle(char* id, double newprice, int sitenum) {
                  strcpy_s(Brand, id);//char*转char[]拷贝  char[]转char*直接赋值
                  price = newprice;
                  siteNum = sitenum;
              }
          public:
              void printVehicleInfo();
          };
      
          //结构体作为参数
          void printDetail(Book book);
      
          //结构体指针作为参数
          void printDetail(Book* book);
      
          //对象指针作为参数
          void printCarInfo(Vehicle* vehicle);
      
          //对象引用作为参数
          void printCarInfo(Vehicle& vehicle);
      
          //复制信息,对象引用作为参数,返回引用
          Vehicle* copyCarInfo(Vehicle& vehicle);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44

    二、源文件

    源文件中主要是对.h文件中声明的定义、实现,注意引入“testStruct.h”

    • testStruct.cpp

          #include 
          #include 
          #include "testStruct.h"
          using namespace std;
      
      
          //输出详细信息
          void printDetail(Book book) {
              cout << "书名:" << book.Name << endl;
              cout << "ID:" << book.ID << endl;
              cout << "价格:" << book.price << endl;
          }
      
          void printDetail(Book* book) {
              cout << endl;
              cout << "指针作为参数:" << endl;
              cout << "书名:" << book->Name << endl;
              cout << "ID:" << book->ID << endl;
              cout << "价格:" << book->price << endl;
          }
      
          void Vehicle::printVehicleInfo() {//this是个指针
              cout << "品牌:" << this->Brand << endl;
              cout << "价格:" << this->price << endl;
              cout << "承载人数:" << this->siteNum << endl;
          }
      
          void printCarInfo(Vehicle* vehicle) {
              cout << "品牌:" << vehicle->Brand << endl;
              cout << "价格:" << vehicle->price << endl;
              cout << "承载人数:" << vehicle->siteNum << endl;
          }
      
          void printCarInfo(Vehicle& vehicle) {
              cout << "品牌:" << vehicle.Brand << endl;
              cout << "价格:" << vehicle.price << endl;
              cout << "承载人数:" << vehicle.siteNum << endl;
          }
      
          Vehicle* copyCarInfo(Vehicle& vehicle) {
              Vehicle* pnewvehicle = &vehicle;
              strcpy_s(vehicle.Brand, "MINI-point");
              vehicle.price = 200000;
              return pnewvehicle;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45

    三、不同文件之间的调用

    在其他文件(例如含有main()入口的testNew.cpp)中使用testStruct定义的函数,只需要引入testStruct.h即可

    • testNew.cpp

          #include 
          #include 
          #include "testStruct.h"
          using namespace std;
      
          int main(){
              
              Vehicle myminicar;
              Vehicle* minicarinfo = copyCarInfo(myminicar);
              printCarInfo(minicarinfo);
              cout << "------------分割线------------" << endl;
              cout << "设置的宏定义MAXPRICE:" <<MAXPRICE << endl;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    • 执行结果
      其他文件引用执行结果

    四、问题记录

    在cpp文件中结构体可以不使用typedef,可以直接定义,但是在头文件中不加typedef会出现报错
    定义结构体出现的问题

  • 相关阅读:
    App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
    java毕业设计电子存证系统mybatis+源码+调试部署+系统+数据库+lw
    JavaScript 防抖 (vue中写法总结)
    windos本地文件上传到ubuntu
    vue自动更新版本号
    笔记本电脑配置知识大全
    Android 13.0 根据包名授权悬浮窗权限
    Python之人机猜拳游戏
    openGauss学习笔记-88 openGauss 数据库管理-内存优化表MOT管理-内存表特性-使用MOT-MOT使用将磁盘表转换为MOT
    markdown
  • 原文地址:https://blog.csdn.net/hehe_soft_engineer/article/details/126864772