Creating Objects in C ++ Conditional Expressions

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 is talking about creating a class that displays the title of the report. The class has a default constructor, which sets the company name and report name for the common thing, and also, if the user wants, has a constructor that takes two arguments (company names and report name).

The problem is: "The two-component default constructor must specify these [company and report names] when creating a new Report object. If the user creates the Report object without any arguments, use the default values. Otherwise, use the 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;), as well as 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 the statements are based on the user's choice? Here is what I still have and obviously this is not working:

#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 header.print_one_line(); return 0; } bool enter_company_name() { char choice; cout << "Do you want to enter a name?\n>"; cin >> choice; if (choice == 'y' || choice == 'Y') return true; else return false; } bool print_form() { char choice; cout << "Do you want to print a formatted header?\n>"; cin >> choice; if (choice == 'y' || choice == 'Y') return true; else return false; } 

So, I want to create an object using the default values ​​if none are specified, or create it with custom values ​​if this choice is specified. I just can't figure out how to do this interactively in C ++. So far I have not been able to find a single similar question.

The closest thing I came across uses pointers to do something similar to what I want to do, but the book I use has not yet received the pointers, and I want to try to figure out a way to do this what remains in within the section in which I work (i.e. without using pointers).

I did not include the header file or the class implementation file because I do not think they are relevant here.

Thank you in advance!

+8
c ++ object constructor class if-statement
source share
5 answers

I don’t know if I understood your question correctly, but can you just declare the report in front of the if / else block and then initialize it inside?

 Report header; if (...) { header = Report(); else header = Report(name,company); 

Or shorter:

 Report header; // calls default constructor if (shouldInitializeWithParams) { header = Report(name,company); } 

Of course, this requires an empty constructor.

+7
source share

First, you cannot create an object in a conditional expression and use it after the conditional statement: two branches of the conditional operator create a scope each and any object created inside destroys the end of the branch. That is, you need to come up with a different approach. The simplest approach is probably to delegate the creation of an object to a function that returns objects accordingly:

 Report makeReport() { if (enter_company_name()) { ... return Report(name, company); } return Report(); } ... Report report = makeReport(); 

An alternative approach is to use a ternary operator to ensure that Report is created anyway:

 bool get_company_name = enter_company_name(); std::string name(get_company_name? read_name(): ""); std::string company(get_company_name? read_company(): ""); Report report = get_company_name? Report(name, company): Report(); 

All of these approaches assume that the Report class can actually be copied.

+11
source share

We do not know whether the Report class is able to copy, so it is better to use pointers.

 Report * header; if (...) { header = new Report(); else header = new Report(name,company); // after all don't forget delete header; 

and of course you should use a header pointer like this

 header->print_formatted(); 
+2
source share

The simplest thing that comes to mind is a little refactoring in the code stream. Create a function that processes the input data and returns the constructed object:

 Report loadReport() { if (user_input()) { // read input return Report(name,company); } else { return Report(); } } 

Then call the function from main. A small design change is the introduction of a function, whose sole responsibility is Report from user input, which actually makes sense as a function.

+2
source share

I am not sure if I understood your question correctly. I apologize if you read the answer and understand that this is the case.

But, nevertheless, I think that the main strategy will be to use constructor overload. those. you define constructors for both: the case when parameters are not passed and the case when parameters are passed. The first (what you call the default constructor) will initialize the names of companies and reports by default. The latter assigns the received parameters to compilation names and reports.

Regarding the use of pointers: you can avoid this by “declaring” many objects of type “Report” (class). For example, you can create an array of headers (objects). And then you can “define” it as and when the user is responding.

But with pointers you do everything at runtime (dynamic assignment), whereas when using arrays (or declaring many objects): the number is fixed. This may be ineffective.

0
source share

All Articles