Help With Classes

Working on a program using classes:
I cannot figure out to write my default constructor.

Report Heading- uses a class named Heading that has 2 data
members. There is a 2-parameter default constructor allows for theses to be specified
at the a new Heading object is created. If the user creates a Heading object without
passing any arguement, the default for company name will be "ABC Industries and the
default for the report name will be "Report",
*/
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
62
63
#include <iostream>
 
using namespace std;


//Class declaration.
class Heading                                                       
{
     private:
       // 2 member variables. 
		string companyName;                                       
        string reportName;
		
     public: 
		 void displayHeading (string, string);
         string getNames();

//Member fubctions:

/**********************************************************
*              Heading::displayHeading                    *
* This function will display the heading with company     *
* name on the first line and the report name name on the  *
* secomd line.                                            *
**********************************************************/

		void Heading::displayHeading(string, string)
		{
			cout << companyName << endl;
			cout << reportName << endl;
		}
		
/***********************************************************
*                   Heading::getNames                      *
* This function will retrieve the names of the compANY and *
* the name of the report. If no name is provided for either*
* a default name will print.                               *
***********************************************************/
         string Heading::getNames(string, string)
		{
			cout << "Company Name: ";
			cin >> companyName;
			cout <<"Report Name: ";
			cin >> reportName;
		}
 };

      int main ()
{ 
	//Object created.
	Heading heading;                

	//Gets names from the user.
	 string getNames();
	 
    //Displays the company heading.
	 void displayHeading();


	system ("pause");
	return 0;
	  }
Hmmm! I see we have a long way to go as you calling a function by creating it over again in your main, and manipulating/assigning values for that function inside your class.


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
62
63
64
65
//Class declaration.
class Heading                                                       
{
     private:
       // 2 member variables. 
		string companyName;                                       
        string reportName;
		
     public: 
		 void displayHeading (string, string);
         string getNames();
}; //Your done with your class right here  <<<<<<<---------------------------------------------


//Member fubctions:  <<<<<<<<<-----------------------------------------------------------

/**********************************************************
*              Heading::displayHeading                    *
* This function will display the heading with company     *
* name on the first line and the report name name on the  *
* secomd line.                                            *
**********************************************************/

void Heading::displayHeading(string, string)
{
	cout << companyName << endl;
	cout << reportName << endl;
}
		
/***********************************************************
*                   Heading::getNames                      *
* This function will retrieve the names of the compANY and *
* the name of the report. If no name is provided for either*
* a default name will print.                               *
***********************************************************/
 string Heading::getNames(string, string)
{
	cout << "Company Name: ";
	cin >> companyName;
	cout <<"Report Name: ";
	cin >> reportName;
}

//      --------------------------Now for main----------------------------
int main ()
{ 
	//Object created.
	Heading heading;                

	//Gets names from the user.
	 //string getNames(); <<<<<<<<<<<<<You not calling the function this way
	 heading.getNames(); <<<<<<<<<<<This is how you call it. do the same for below
         
          /*Note when you created getNames() at the top you not passing parameters yet on
             line 39 you have string heading::getNames(string, string)
           */

//Displays the company heading.
	 void displayHeading();


	system ("pause");
	return 0;
}



try and resolve your code then we move on
Last edited on
I changed it all like you showed me and then I changed this:
1
2
//Displays the company heading.
	 void displayHeading();

to this:
1
2
//Displays the company heading.
	 Heading.displayHeading();
I am getting error on line 38 - 41 on the couts << it says there is no such operator as << and error on the dot operator in heading.getNames in line 52.
Topic archived. No new replies allowed.