WRITE IN C++ Design a class called Heading that has data members to hold the com
ID: 3808743 • Letter: W
Question
WRITE IN C++
Design a class called Heading that has data members to hold the company name and the report name. A two-parameter default constructor should allow these to be specified at the time a new Heading object is created. If the user creates a Heading object without passing any arguments, "ABC Industries" should be used as a default value for the company name and "Report" should be used as a default for the report name. The class should have member functions to print a heading in either one-line format, as shown here:
Pet Pals Payroll Report
or in four-line "boxed, as showing here:
*************************************************
Pet Pals
Payroll Report
*************************************************
Try to figure out a way to center the headings on the screen, based on their lengths. Demonstrate the class by writing a simple program that uses it.
Explanation / Answer
Code:
#include<iostream>
#include<string>
using namespace std;
class Heading
{
string name,report;
int report_size;
string *given_report;
public:
Heading(){
name.assign("ABC Industries");
report.assign("Report");
report_size=1;
given_report=new string[report_size];
}
Heading(char* n, char * r,int rep_size)
{
name.assign(n);
report.assign(r);
report_size=rep_size;
given_report=new string[report_size];
}
~Heading()
{
delete [] given_report;
}
void read_report()
{
cout<<"Enter the report string line by line:"<<endl;
for(int i=0;i<report_size;i++)
cin>>given_report[i];
}
void printHeader()
{
int size=name.size();
for(int i=0;i<size;i++)
cout<<" ";
for(int i=0;i<3*size;i++)
cout<<"-";
cout<<endl;
for(int i=0;i<2* size;i++)
cout<<" ";
cout<<name<<endl;
for(int i=0;i<2* size;i++)
cout<<" ";
cout<<report<<endl;
for(int i=0;i<size;i++)
cout<<" ";
for(int i=0;i<3*size;i++)
cout<<"-";
cout<<endl;
}
void print(){
printHeader();
int size=name.size();
for(int i=0;i<report_size;i++){
for(int j=0;j<2* size;j++)
cout<<" ";
cout<<given_report[i]<<endl;
}
}
};
int main(void)
{
Heading h("Pet Pals","Payroll Report",4);
h.read_report();
h.print();
};
Output:
Enter the report string line by line:
NewYork_41
Houston_50
Denmark_56
London_68
------------------------
Pet Pals
Payroll Report
------------------------
NewYork_41
Houston_50
Denmark_56
London_68
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.