How to include a .h file?

Hi! I have written a Clas in C++ but my professor wants me to turn in .h file instead of the whole program and i don't know exactly how to do it. Can anyone show me steps to create a .h file, please? Thank you. This is the program i have:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
using namespace std;

class CCustomer
{
private:
	string Card_number;
public:
	string first_name;
	string last_name;
	string customer_ID;
	CCustomer();//Default constructor
	CCustomer(string, string, string, string);//Parameterized constructor
	CCustomer(CCustomer&);//Copy constructor
	void input();//Function to get inputs
	void output();//Function to get outputs
	void display();//To display the info of a customer
	string getCard_number();//Accessor
	void setCard_number (string Card_Num);//Mutator
};

void main()
{
	CCustomer myself;
	CCustomer yourself("Ravi", "Shah", "A123", "1111 2222 3333 4444");
	yourself.output();
	CCustomer name(yourself);
	yourself.output();
	myself.input ();
	string myCardNum = myself.getCard_number();
	myself.setCard_number ("1234 5678 1234 5678");
	myself.output();
}

CCustomer::CCustomer()
{
	first_name = "no name";
	last_name = "no name";
	customer_ID = "no info";
	Card_number = ("0000 0000 0000 0000");
}

CCustomer::CCustomer(string f, string l, string id, string n)
{
	first_name = f;
	last_name = l;
	customer_ID = id;
	Card_number = n;
}

CCustomer::CCustomer(CCustomer& x)
{
	first_name = x.first_name;
	last_name = x.last_name;
	customer_ID = x.customer_ID;
	setCard_number(x.getCard_number());
}

void CCustomer::input()
{
	cout << "Please enter your first name, last name, customer ID, and card number each" << endl; 
	cout << "seperated by space, then press enter ";
	cin >> first_name
		>> last_name
		>> customer_ID
		>> Card_number;
	setCard_number(Card_number);
}

void CCustomer::output()
{
	cout << "First name: " << first_name << endl;
	cout << "Last name: " << last_name << endl;
	cout << "Customer ID: " << customer_ID << endl;
	cout << "Card number: " << getCard_number() << endl;
}

void CCustomer::display()
{
	cout << first_name << ' ' << last_name << ' ' << customer_ID << ' ' << getCard_number() << endl;
}

string CCustomer::getCard_number()
{
	return Card_number;
}

void CCustomer::setCard_number (string Card_Num)
{
	Card_number = Card_Num;
}


Typically you would code the above in 3 files.
Customer.h would be the header file and contain just the declaration of class Customer (with any #includes required for that).
Customer.cpp would #include "Customer.h" and contain the definitions of the functions within class Customer.
main.cpp would #include "Customer.h" and just include main().
You would also put a #ifndef in the .h file to prevent problems with it being included multiple times
eg
1
2
3
4
#ifndef _CUSTOMER_
#define _CUSTOMER_
// All the code here
#endif 

Last edited on
Topic archived. No new replies allowed.