4 Replies - 561 Views - Last Post: 11 May 2013 - 03:43 AM Rate Topic: -----

#1 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

how to create a header file and implement it

Posted 11 May 2013 - 01:39 AM

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.

#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();
         }
}



Is This A Good Question/Topic? 0
  • +

Replies To: how to create a header file and implement it

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: how to create a header file and implement it

Posted 11 May 2013 - 02:23 AM

The class definitions go in individual header (.h) files. Don't forget include guards in the headers.

myclass.h
#ifndef CLASS_MYCLASS_H
#define CLASS_MYCLASS_H

class myclass {
private:
   int myInt;

public:
   void setMyInt(int theInt);
   int getMyInt();
};
#endif


myotherclass.h
#ifndef CLASS_MYOTHERCLASS_H
#define CLASS_MYOTHERCLASS_H

#include <string>

class myotherclass {
private:
   std::string myString;

public:
   void setMyString(const std::string& theString);
   std::string getMyString();
};
#endif


The implementation of each class goes in a separate .cpp file.

myclass.cpp
#include "myclass.h"

void myclass::setMyInt(int theInt)
{
   myInt = theInt;
}

int myclass::getMyInt()
{
   return myInt;
}



myotherclass.cpp
#include "myotherclass.h"

void myotherclass::setMyString(const std::string &theString)
{
   myString = theString;
}

std::string myotherclass::getMyString()
{
   return myString;
}


These files are added to your project so they all get compiled and linked. Your main file then includes the header file and uses the classes.

mymain.cpp
#include <iostream>
#include <string>
#include "myclass.h"
#include "myotherclass.h"

using namespace std;

int main()
{
   myclass itsMyClass;
   itsMyClass.setMyInt(42);

   myotherclass itsMyOtherClass;
   itsMyOtherClass.setMyString("The answer to life the universe and everything is:");

   cout << itsMyOtherClass.getMyString() << " " << itsMyClass.getMyInt() << endl;
   return 0;
}


Your include files should be:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>


You should not use conio.h.

This post has been edited by JackOfAllTrades: 11 May 2013 - 02:55 AM
Reason for edit:: Added string include as noted below

Was This Post Helpful? 1
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: how to create a header file and implement it

Posted 11 May 2013 - 02:41 AM

Jack gave you a really great start ...

Don't forget the #include <string> below ... see added comments


#ifndef CLASS_MYOTHERCLASS_H
#define CLASS_MYOTHERCLASS_H

#include <string> // you need to include this here so <string> is included in related .cpp file

class myotherclass {
private:
   std::string myString; 

public:
   void setMyString(const std::string& theString);
   std::string getMyString();
};
#endif



For small projects, I like to just leave all my class code in one header (.h) file
(as done with the code for a template class)

Then ... if you want to ... you can pull the code apart into .h file(s) and .cpp file(s)
if you like.

Here is a recent (working) example of just that ... a .h file that holds all the class code

See if you can pull it apart ... following the example Jack gave you.

A example of class code all in one .h file for you to 'pull apart'

This post has been edited by David W: 11 May 2013 - 02:51 AM

Was This Post Helpful? 1
  • +
  • -

#4 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

Re: how to create a header file and implement it

Posted 11 May 2013 - 02:46 AM

what about if I use derived classes? should I still separate them from the base class?
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: how to create a header file and implement it

Posted 11 May 2013 - 03:43 AM

View Postirkzuptang, on 11 May 2013 - 05:46 AM, said:

what about if I use derived classes? should I still separate them from the base class?


If your design spec's call for that ... yes!

If it's for your own personal pleasure ... then you can do whatever ...

I find it (sometimes) easier ...

to leave the class code

all in one file (for each BIG class)

at the first
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1