how to create a header file and implement it.

This program is printing possible gifts to whom the user wants to give. It asks the user to input the name and the birthday.

I have this problem involving classes. I must have misunderstood the whole thing about classes. I don't know how to create a header file and an implementation file. I don't know what to put inside the implementation file. So, how do I separate this into three files: the header, implementation and the main program. Please help me. I don't really understand. My teacher requires me to give him those three files.

I'm using Dev C++ and this program needs to run in MS Visual Studio Express 2012. And i also don't know how.


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<fstream>
#include<string.h>
#include<iomanip>
#include<conio.h>
#include<stdlib.h>
using namespace std;

//classes
class nameType
{
      public:
             void getName();
             //function to input user's name

      private:
              string first;
              string middle;
              string last;   
};

class bdayType
{
      public:
             void getBirthday(); //function to get user's birthday
             void printGifts(); //function to display the possible gifts
             void printAge(); //function to display the user's age on this year's birthday
             void getGender(); //function to get user's gender
             void exit(); //function to choose whether to exit or perform the program again
            
      private:
              char gndr;
              string m;
              int d;
              int y;
              int age;
              int a;
};
                     
//body
int main()
{
      nameType name;
      bdayType gender;
      bdayType birthday;
 
      
      name.getName();
      birthday.getGender();
      birthday.getBirthday();
      birthday.printAge();
      birthday.printGifts();
      birthday.exit();
}

//functions
void nameType::getName()
{
      nameType name;
      
      cout<<"\tEnter your name";
      cout<<"\n\nFirst Name: ";
      std::getline( std::cin, name.first );
      cout<<"Middle Initial: ";
      cin>>name.middle;
      cout<<"Last Name: ";
      cin>>name.last;
}

void bdayType::getGender()
{
     bdayType gender;
     cout<<"\nGender: \n\tM - Male\n\tF - Female\n";
     cin>>gender.gndr;
     
          
}
     
void bdayType::getBirthday()
{
     bdayType birthday;
     cout<<"\n\tEnter your birthday\n\tFormat: (month-dd-yyyy)";
      cout<<"\n\nMonth (ex:Jan/January): ";
      cin>>birthday.m;
      cout<<"Day: ";
      cin>>birthday.d;
      cout<<"Year: ";
      cin>>birthday.y;
      if(birthday.y<=999)
           cout<<"Sorry! You entered an invalid year. 1900s-2013 only!";
      else
           birthday.age=2013-birthday.y;
    
}
     

void bdayType::printAge()
{
     bdayType birthday;
     cout<<"\nAge this year's birthday: "<<birthday.age<<"\n";
     
}

void bdayType::printGifts()
{
      bdayType birthday;
      bdayType gender;
      
      if(birthday.age>=1&&birthday.age<=12)
     {
         cout<<"\nPossible Gifts: \n\tTicket to a Theme Park\n\tVideo Game\n\tAction Figures\n\tArt Essentials\n\tPet\n\tToy";
         getch();
     }
     else if(birthday.age>=13&&birthday.age<=18)
     {
          cout<<"\nPossible Gifts: \n(Male):\n\tVideo Game\n\tMovie DVD\n\tClothes\n\tCologne\n\tGadget\n\tBook\n\tMusic Album\n\tSports Equipment\n(Female):\n\tJewelry\n\tAccessories\n\tMake Up\n\tPurse\n\tSentimental Gifts\n\tCologne\n\tGadget\n\tBook\n\tBag";
          getch();
     }
     
     else if (birthday.age>=19&&birthday.age<=60)
     {
          cout<<"\nPossible Gifts:\n(Male):\n\tWatch\n\tJersey of Favorite Sportsman\n\tNecktie\n\tGadget\n\tTrip to Somewhere\n\tWallet\n(Female):\n\tBag\n\tPair of Shoes\n\tChocolate and Flowers\n\tPet\n\tPortrait\n\tCosmetics\n\tAccessories";
          getch();
     }
     else
     {
          cout<<"\nSorry, wrong input!";
          getch();
     }
}
     
void bdayType::exit()
{
     bdayType birthday;
     cout<<"\n\n\n\t1 - Perform the program again\n\t2 - Exit";
     cout<<"\nEnter choice: ";
     cin>>birthday.a;
     if(birthday.a==1)
        {
                      system("cls");
                      int main();
        }
     else if(birthday.a==2)
        {
                         EXIT_SUCCESS;
        }
     else
         {
         cout<<"Invalid input!";
         birthday.exit();
         }
}
My advice: put the class declaration in a header file(.h or .hpp), say head.h, and add it tk your project. Also, create a .cpp file(implementation file) with the same name as the header, so head.cpp. In the head.cpp-implementation file-, #include your header file, head.h, contaning the class, and define the methods(member functions). Then the main .cpp file would initialize any static member variable, and also have your main function.

Aceix.
Normally you do it like this:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "classOne.h"  // Header file of a class to be used
#include "classTwo.h"  // Another class if needed

int main()
{
    // compiler knows the types
    // so no errors compiling this.
    classOne c1(1,2);  // using constructor to initialize field1 and field2
    c1.function();

    classTwo c2;
}


classOne.h
1
2
3
4
5
6
7
8
9
10
class classOne {
    private:
        int field1;    // variables are left uninitialized in .h file
        int field2;
    public:
        int function();    // usually there is no body for functions in .h file 
        int otherFunction();
        // constructor
        classOne(int num1, int num2); // also no body in .h file  
}; // semicolon - I always forget this one 

classOne.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// cpp file must include .h file, so that
// all the names are known to compiler
#include "classOne.h" 

// We must use full name of a function, 
// that is use its class name and '::'
int classOne::function() {  
    return field1;  // Body is here
}

// All functions from .h files must have a body 
int classOne::otherFunction() {
    return field2;
}
// Body of a constructor, also with full name
classOne::classOne (int num1, int num2){
    field1 = num1;
    field2 = num2;
}
Last edited on
You can use menu: File, New,then choose headfile, and enter other information. Check "Add to project", then you can edit it. I'm using Viscul C++, and I don't know if the step is the same.
Topic archived. No new replies allowed.