Objects and Classes in C++: Separating Class Definition from Implementation

Separating class definition from class implementation makes the class easy to maintain.

C++ allows you to separate class definition from implementation. The class definition describes the contract of the class and the class implementation carries out the contract. The class definition simply lists all the data fields, constructor prototypes, and function prototypes. The class implementation implements the constructors and functions. The class definition and implementation may be in two separate files. Both files should have the same name but differ­ent extension names. The class definition file has an extension name .h (h means header) and the class implementation file an extension name .cpp.

Listings 9.3 and 9.4 present the Circle class definition and implementation.

Listing 9.3 Circle.h

1 class Circle
2 {
3   
public:
4   
// The radius of this circle
5    double radius;
6
7   
// Construct a default circle object
8    Circle();

9
10   
// Construct a circle object
11   Circle(double);
12
13   
// Return the area of this circle
14    double getArea();
15 };

Listing 9.4 Circle.cpp

1 #include “Circle.h”
2
3
// Construct a default circle object
4 Circle::Circle()
5 {
6    radius =
1;
7 }
8
9
// Construct a circle object
10 Circle::Circle(double newRadius)
11 {
12    radius = newRadius;
13 }
14
15
// Return the area of this circle
16 double Circle::getArea()
17 {
18   
return radius * radius * 3.14159;
19 }

The :: symbol, known as the binary scope resolution operator, specifies the scope of a class member in a class.

Here, Circle:: preceding each constructor and function in the Circle class tells the compiler that these constructors and functions are defined in the Circle class.

Listing 9.5 is a program that uses the Circle class. Such a program that uses the class is often referred to as a client of the class.

Listing 9.5 TestCircleWithHeader.cpp

1 #include <iostream>
2
#include “Circle.h”
3 using namespace std;
4
5
int main()
6 {
7    Circle circle1;
8    Circle circle2(
5.0);
9
10   cout <<
“The area of the circle of radius ”
11   << circle1.radius << ” is ” << circle1.getArea() << endl;
12   cout <<
“The area of the circle of radius ”
13   << circle2.radius << ” is ” << circle2.getArea() << endl;
14
15   
// Modify circle radius
16   circle2.radius = 100;

17   cout << “The area of the circle of radius ”
18   << circle2.radius << ” is ” << circle2.getArea() << endl;
19
20   
return 0;
21 }

There are at least two benefits for separating a class definition from implementation.

  1. It hides implementation from definition. You can feel free to change the implementation. The client program that uses the class does not need to change as long as the definition is not changed.
  2. As a software vendor, you can just provide the customer with the header file and class object code without revealing the source code for implementing the class. This protects the software vendor’s intellectual property.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *