View Single Post
  #1 (permalink)  
Old May 25th, 2014, 04:32 AM
DeepakSingh DeepakSingh is offline
Registered User
 
Join Date: May 2014
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default C++ Class Example | Separate header and implementation file

Separate Header and Implementation Files
In this Tutorial, I'll demonstrate how to make class reusable by separating it into another files. This example is also available at my site : cppforschool



Header File
Class declarations are stored in a separate file. A file that contains a class declaration is called header file. The name of the class is usually the same as the name of the class, with a .h extension. For example, the Time class would be declared in the file Time .h.

Code:
#ifndef TIME_H
#define TIME_H

class Time
{
     private :
          int hour;
          int minute;
          int second;
     public :
          //with default value
          Time(const int h = 0, const int m  = 0, const int s = 0);
          //	setter function
          void setTime(const int h, const int m, const int s);
          // Print a description of object in " hh:mm:ss"
          void print() const;
          //compare two time object
          bool equals(const Time&);
};
 
#endif
Implementation File
The member function definitions for a class are stored in a separate .cpp file, which is called the class implementation file. The file usually has the same name as the class, with the .cpp extension. For example the Time class member functions would be defined in the file Time.cpp.

Code:
 

#include <iostream>
#include <iomanip>
#include "Time.h"
using namespace std;
 
Time :: Time(const int h, const int m, const int s) 
  : hour(h), minute (m), second(s)
{}
 
void Time :: setTime(const int h, const int m, const int s) 
{
     hour = h;
     minute = m;
     second = s;     
}		
 
void Time :: print() const
{
     cout << setw(2) << setfill('0') << hour << ":"
	<< setw(2) << setfill('0') << minute << ":"
 	<< setw(2) << setfill('0') << second << "\n";	
 
}
 
bool Time :: equals(const Time &otherTime)
{
     if(hour == otherTime.hour 
          && minute == otherTime.minute 
          && second == otherTime.second)
          return true;
     else
          return false;
}
Client Code
client code, is the one that includes the main function. This file should be stored by the name main.cpp


Code:
 
#include <iostream>
using namespace std;
#include "Time.h"

int main()
{
     Time t1(10, 50, 59);
     t1.print();   // 10:50:59
     Time t2;
     t2.print(); // 06:39:09
     t2.setTime(6, 39, 9);
     t2.print();  // 06:39:09
   
     if(t1.equals(t2))
          cout << "Two objects are equal\n";
     else
          cout << "Two objects are not equal\n";	
  
     return 0;
}
The advantages of storing class definition in separate file are

1. The class is reusable

2. The clients of the class know what member functions the class provides, how to call them and what return types to expect

3. The clients do not know how the class's member functions are implemented.
Reply With Quote
The Following User Says Thank You to DeepakSingh For This Useful Post: