Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
View Poll Results: Did you find this helpful?
Yes 0 0%
No 1 100.00%
Voters: 1. You may not vote on this poll

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 25th, 2014, 05:32 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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, 03:24 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 08:29 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.