KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am learning c++, and I just got to the object oriented chapter. I have a question about creating objects inside if statements. The problem I'm working on says to create a class that will display a report header. The class has a default constructor that sets the company name and report name to a generic thing, and also, if the user wants, has a constructor that takes two arguments (strings company name and report name). The problem says, specifically, "A two-parameter default constructor should allow these [company and report names] to be specified at the time a new Report object is created. If the user creates a Report object without passing any arguments, use the default values. Otherwise, use user specified values for the names." So my question is, how to create these objects? I understand how to create an object without any arguments (i.e. Report newobj;), and also with arguments (i.e. Report newobj(string string);). Basically, I get how to create these objects initially at the top of my main function. But is it possible to create them inside if statements based on user choices? Here is what I have so far and, obviously, it doesn't work: #include <iostream> #include <string> #include "report.h" using namespace std; bool enter_company_name(); // return true if user wants to enter company name bool print_form(); // return true if user wants to print in formatted output int main() { string company_name, report_name; bool name = false, format = false; name = enter_company_name(); format = print_form(); if (name) { cout << "Enter company name: "; getline(cin, company_name); cout << "Enter report name: "; getline(cin, report_name); Report header(company_name, report_name); // THIS IS MY PROBLEM } else Report header; // THIS IS MY PROBLEM if (format) header.print_formatted(); else heade
Tags (comma-separated)
Save Edits
Cancel