This programming challenge, 2 Report Heading, asks us to create a program that outputs the heading for a company report. We need to create a header, implementation and client file. If you’re not familiar with these three different files, check out my blog about programming challenge 7-1 Date.

First, I created the header file, this is where the class along with its variables and methods are declared.

[code language=”cpp”] // Heading.h
// 7.2 – Report Heading
// Header file, declarations and default parameters
#ifndef HEADING_H
#define HEADING_H
#include <string>
using namespace std;

class Heading
{
// Variable declarations
private:
string company;
string report;

// Decelerations in header file, definitions in the .cpp implementation file
public:
// Default parameters are "ABC Industries" and "Report"
Heading(string com = "ABC Industries", string rep = "Report");
void setNames();
void centerItem(string);
void print1line();
void print4lines();
};

#endif // HEADING_H

[/code]

Now that I have the needed declarations. The implementation file is used to define what I declared in the header file. To do this, I need to use ‘#include “Heading.h”‘.

[code language=”cpp”] // Heading.cpp
// 7.2 – Report Heading
// Implementation file, used for declarations
#include "Heading.h"
#include <iostream>
#include <string>
using namespace std;

/* I set the default parameters in the header file, notice I don’t
set the parameters equal to anything. Instead I use an
initialization list, the code after the single colon. */
Heading::Heading(string com, string rep):company(com), report(rep){}

// Class method to set variables
void Heading::setNames()
{
cout << "Company name: ";
getline(cin,company);
cout << "Report name: ";
getline(cin, report);
};

// Method to center output
void Heading::centerItem(string item)
{
int itemLength = (80 – item.length())/2;
for (int i=0; i < itemLength; i++)
cout << " ";
cout << item << "\n";
}

// Method to print one line heading
void Heading::print1line()
{
string line = company + " " + report;
centerItem(line);
};

// Method to print 4 line heading
void Heading::print4lines()
{
string stars = "*********************************************";
centerItem(stars);
centerItem(company);
centerItem(report);
centerItem(stars);

};
[/code]

Finally, I create the client file. This includes “main”, the final file of our program.

[code language=”cpp”] // main.cpp
// 7.2 – Report Heading
#include "Heading.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
//Create an Heading object
Heading myHeading;
//Explain programs functionality
cout << "This program using a header file for class specification, "
"Class implementation is done in a cpp file, this is "
"linked to the client code, application program, which "
"includes the main function.\n\n" << endl;
cout << "One line heading: \n";
myHeading.print1line();
cout << "\nFour lines heading: \n";
myHeading.print4lines();
return 0;
}

[/code]

 

Output when using the default constructor:

Starting Out with C++ Early Objects - Chapter 7

Default Output

 

Then I added arguments to the default constructor:

Starting Out with C++ Early Objects - Chapter 7

Constructor with arguments

 

Output with arguments:

Starting Out with C++ Early Objects - Chapter 7

Output with arguments

Output with arguments: