Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
View Poll Results: Did you find this helpful?
Yes 1 50.00%
No 1 50.00%
Voters: 2. You may not vote on this poll

  #1 (permalink)  
Old May 25th, 2014, 04:32 AM
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:
  #2 (permalink)  
Old September 8th, 2018, 02:24 AM
Registered User
 
Join Date: Sep 2018
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Separate header and implementation file

I use simple example of my site http://www.welookups.com
Separate header and implementation file

#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
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Class Header Commenting abinashpatra Visual Studio 2008 3 June 16th, 2010 03:05 AM
separate file location for different databases Aprile MySQL 1 September 6th, 2007 04:34 PM
How to access form controls from separate class? paket C# 6 August 1st, 2007 06:18 AM
file upload processed on separate page vauneen ASP.NET 2.0 Professional 0 August 8th, 2006 03:03 AM
problem for creating the separate log file for con goel_man Apache Tomcat 0 December 19th, 2004 11:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.