2

I've written a program to display a company's name and report name, however, I need to make it so that if someone does not enter anything the program displays two default names. I need to add a constructor with 2 parameters. I find that I'm confusing myself, and getting more and more lost. I've tried making my code look like that of the textbook example, but to no avail. Could someone please give me some pointers and a direction?

I am using C++ in Microsoft Visual Studios Express 2012, and here is my current code.

//This program displays a company's name and report. 
#include <iostream>
#include <string>
using namespace std;



class Heading
{   
private:
    string company;
    string report;

public:

    void storeInfo (string c, string r);

    string getCompany()
    {
        return company;
    }
    string getReport()
    {
        return report;
    }
};

void Heading::storeInfo(string c, string r)
{
company = c;
report = r;
}

void storeInfo(Heading&);
void showInfo(Heading);

int main()
{
Heading company;

storeInfo(company);
showInfo(company);

cin.ignore();
cin.get();

return 0;
}

/*****storeInfo*****/
void storeInfo(Heading &item)
{
string company;
string report;

cout << "\nPlease enter the company name.\n";
getline(cin, company);

cout << "\nPlease enter the report name.\n";
getline(cin,report);

item.storeInfo(company, report);
}

/*****showInfo*****/
void showInfo(Heading item)
{
cout << item.getCompany() << endl;
cout << item.getReport();
}
6
  • 1
    I don't see any constructors defined here. A constructor has the same name as the class, and no return type. Commented Oct 17, 2013 at 18:38
  • 1
    where are the constructors? Commented Oct 17, 2013 at 18:38
  • Would it be adding Heading(string company, string, report) to the public section of the class? Commented Oct 17, 2013 at 18:41
  • @user2792977 See Atle's awnser. Commented Oct 17, 2013 at 18:43
  • so I've added, Heading() { company = "ABC Industries"; report = "Report"; } Heading (string c, string r) { company = c; report = r; } But it won't display the default names if nothing is entered in the program, I must be missing something major. Commented Oct 17, 2013 at 19:53

3 Answers 3

4

Put this inside your class to create a constructor:

Heading() {
    company = "Default company";
    report = "Default report";
}

You can also do this:

Heading(const char *def_company, const char *def_report) {
     company = def_company;
     report = def_report;
}

And create new Heading-objects like this (dynamic memory):

Heading *object = new Heading("default company", "default report");

Or like this (allocated on the stack):

Heading object("default_company", "default report");
Sign up to request clarification or add additional context in comments.

2 Comments

I recommend using constructor initializer lists, not assigning to members inside the body. And I highly recommend not using that dynamic approach, but if you need it, use a smart pointer.
How is your second initialization in static memory?
0

It is not clear what you actually want. Either you indeed do not know how to write a constructor or you need setters for the data members. If you need a constructor you could write

class Heading
{   
private:
    string company;
    string report;
    const char *default_company = "Unknown company";
    const char *default_report  = "Unknown report";

public:

    Heading( const std::string &company = default_company, const std::string &report = default_report )
        : company( company ), report( report )
    {
    }
    void storeInfo (string c, string r);

    string getCompany()
    {
        return company;
    }
    string getReport()
    {
        return report;
    }
};

If you need setters then simply name them appropriately. For example

void storeCompany( const std::string & );
void storeReport( const std::string & );

instead of storeInfo.

Comments

0

One other approach is this in order to default initialize member variables:

class Heading
{
   ...

private:
    std::string company = "Default";
    std::string report = "Default";
};

For reference: Non-static data members Member initialization

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.