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

二、源文件

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

  • testStruct.cpp

        #include <iostream>
        #include <string>
        #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;
        }
    

三、不同文件之间的调用

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

  • testNew.cpp

        #include <iostream>
        #include <string>
        #include "testStruct.h"
        using namespace std;
    
        int main(){
            
            Vehicle myminicar;
            Vehicle* minicarinfo = copyCarInfo(myminicar);
            printCarInfo(minicarinfo);
            cout << "------------分割线------------" << endl;
            cout << "设置的宏定义MAXPRICE:" <<MAXPRICE << endl;
        }
    
  • 执行结果
    其他文件引用执行结果

四、问题记录

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

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值