• September 22, 2021


Bring Technology Ideas with you

 Separate class code in C++ using Header File

What is a class in C++?

Class is the important part of the computer programming which can be a blueprint that classify the codes based on the your suggestion. A class is a user-defined data type that it can be used in C++ program, and it works as an object constructor which can contain variables, functions, etc.

What is a header file in C++ programming?

Head File is the collection of the code with the program itself wants to separate it outside the main code. Header files contain the set of predefined standard library functions and it includes Function definitions, Data type definitions, Macros or Class, Structure, etc. The header file can be created with the extension (*.h). For example, ebook.h

    Interface of C++ Header File in Microsoft Visual Studio
    How to Create Header File in Microsoft Visual Studio?

    In order to Create Header File in C++ Programming, by going to Solution Explorer (You have to create Empty Project in C++ First), then select in the Header Files, you Add New Item

      Creating Header File in Microsoft Visual Studio

      After that in the Add New Item, please select Header File. Name the header file as you wish then click Add

        Add Header File in C++

        In the Header Files, please copy the code below as from our example. We create one class which is called “Car.h” to classify the program with Car Model from users input.

        #pragma once
        #include <string>
        using namespace std;
        class Car
        	int id;
        	string name;
        	string model;
        	double cost;
        	double year;
        	double TDate;
        	void getcar()
        		cout << "Please Enter The Car's Information Below:" << endl;
        		cout << " Car ID: ";
        		cin >> id;
        		cout << "Car Name: ";
        		cin >> name;
        		cout << " Model: ";
        		cin >> model;
        		cout << " Price: ";
        		cin >> cost;
        		cout << "Year of Manufacture: ";
        		cin >> year;
        		cout << "Current Date: ";
        		cin >> TDate;
        	void report()
        		cout << "The Detail Information of The Car" << endl;
        		cout << "- Car ID: " << id << endl;
        		cout << "- Car Name: " << name << endl;
        		cout << "- Car Model: " << model << endl;
        		cout << "- Price: " << cost << endl;
        		cout << "- Year of Manufacture: " << year << endl;
        		double Age = carage(TDate, year);
        		cout << "The age of the car is: " << Age << " years" << endl;
        	double carage(double c_year, double year)
        		return c_year - year;

        By going back to your project solution explorers, please add the new source.cpp to run for the compiler. We name it as “Model Car.cpp”

          Our main program to get the source from Header Files

          In our Model.cpp, you can copy this code below:

          #include "Car.h"
          int main()
          	Car MyCar;
          	cout << endl;

          *** Note: To work with the Header File, you need to include in the header of the main program by type #include “Car.h”. Remember to include Header Files, we use the #include keyword with name of the Header File. For Example, #include “header.h”. Follow the above code, please the result as below:

            Result from using Header File

            Related post