Need help with classes

I don't know what I'm doing..Or how to do any of this. I missed class for a week due to an illness and the teacher just tells me to figure it out.

Here's the problem:

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
using namespace std;

class Heading
{
private:
	string companyName;
	string reportName;
public:
	Heading(string companyName = "ABC Industries", string reportName = "Report")
	{
		cout << "ABC Industries" << endl;
		cout << "Report" << endl;
	}

	// Prints company
	void PrintCompany()
	{
		cout << "ABC Industries" << endl;
	}

	// Prints report
	void PrintReport()
	{
		cout << "Report" << endl;
	}

	// Prints both
	void Print()
	{
		cout << "ABC Industries" << endl;
		cout << "Report" << endl;
	}
};




int main()
{
	string companyName;
	string reportName;

	cout << "Enter the company name" << endl;
	cin >> companyName;
	

	cout << "Enter the report name" << endl;
	cin >> reportName;
	
	cout << companyName << endl;
	cout << reportName << endl;

	return 0;
}
Take look at this tutorial:

http://www.cplusplus.com/doc/tutorial/classes/

It should give you an idea.
I've already looked at that tutorial, and still don't know how to solve this problem. What's wrong with my code?

I just need to know how to set "ABC Industries" and "Report" as the default.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
using namespace std;

class Heading
{
private:
	string companyName;
	string reportName;
public:
	Heading(string aCompanyName = "ABC Industries", string reportName = "Report") //<--
	{
		companyName = aCompanyName; // <--
		cout << "Report" << endl;
	}

	// Prints company
	void PrintCompany()
	{
		cout << "ABC Industries" << endl;
	}

	// Prints report
	void PrintReport()
	{
		cout << "Report" << endl;
	}

	// Prints both
	void Print()
	{
		cout << companyName << endl; //<--
		cout << "Report" << endl;
	}
};




int main()
{
	string companyName;
	string reportName;

	cout << "Enter the company name" << endl;
	cin >> companyName;
	
	cout << "Enter the report name" << endl;
	cin >> reportName;
	
	Heading hdg( companyName, reportName); //<--
	
	hdg.Print(); //<--	

	return 0;
}


This will give you a start on the changes you need to make.

if you declare a Heading object Heading hdg; then the defaults will be used as your existing constructor shows.
Last edited on
You could do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
    // Using constructor initialization list to set "default" values.
    Heading() : companyName("ABC Industries"),  reportName ("Report")
    { 
        // Blank body!
    }
    // Using constructor initialization list to set user defined values.
    Heading(const std::string& c_name, const std::string& r_name) :
                companyName(c_name), reportName(r_name)
    {
        // Blank body!
    }
...
    void PrintNames() const
    {
        cout << companyName << "  " << reportName << endl;
    }
...

(in main)
    // Declare a class using the default names.
    Heading defaultCompany;
...

(after your inputs)
    // Create an instance of the class with the user defined names:
    Heading userCompany(companyName, reportName); 
...    
    // Now print the names:
    defaultCompany.printNames();
    userCompany.printNames();


One more problem: When I run the program and it asks me "What is the company name", if I enter two words, the second word goes to the report instead of all of it being a part of the company name. How do I fix this? Here's what I've got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
using namespace std;

class Heading
{
private:
	string companyName;
	string reportName;
public:
	Heading(string aCompanyName = "ABC Industries", string reportName = "Report")
	{
		companyName = aCompanyName;
	}

	// Prints company
	void PrintCompany()
	{
		cout << "ABC Industries" << endl;
	}

	// Prints report
	void PrintReport()
	{
		cout << "Report" << endl;
	}

	// Prints both
	void Print()
	{
		cout << companyName << endl;
		cout << "Report:" << endl;
	}
};




int main()
{
	string companyName;
	string reportName;

	cout << "Enter the company name\n" << endl;
	cin >> companyName;


	cout << "Enter the report name\n" << endl;
	cin >> reportName;

	Heading hdg(companyName, reportName);
	hdg.Print();


	cout << "\t" << "*****************************\n"; 
	cout << "\t\t" << companyName << endl; 
	cout << "\t\t" << reportName << endl; 
	cout << "\t" << "*****************************\n";

	return 0;
}
closed account (48T7M4Gy)
You need to use getline()
Topic archived. No new replies allowed.