Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create objects in conditional c++ statements

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
    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 default values if none are specified, or create one with the user values if that's the choice given. I just can't figure out how to do it interactively in c++. I have not been able to find any similar questions anywhere so far.

The closest thing I've come across uses pointers to do something similar to what I want to do, but the book I'm using has not gotten to pointers yet, and I want to try to figure out a way to do it that stays within the bounds of the chapter I'm working in (i.e. not using pointers).

I didn't include the header file or class implementation file because I don't think they are relevant here.

Thank you in advance!

like image 937
nik Avatar asked Feb 19 '12 03:02

nik


People also ask

Can objects be created in C?

Create an ObjectIn C++, an object is created from a class. We have already created the class named MyClass , so now we can use this to create objects. To create an object of MyClass , specify the class name, followed by the object name.

How do you write a conditional statement in C?

C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

What is object in C with example?

In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality. Object is a runtime entity, it is created at runtime.

What are conditional statements explain with example?

Example: We have a conditional statement If it is raining, we will not play. Let, A: It is raining and B: we will not play. Then; If A is true, that is, it is raining and B is false, that is, we played, then the statement A implies B is false.


1 Answers

First off, you cannot create an object within a conditional statement and use it after the conditional statement: the two branches of the conditional statement create a scope each and any object created within in destroyed a 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 the object to a function which returns the objects as appropriate:

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

...
Report report = makeReport();

An alternative approach is to use the ternary operator to conditonally create the Report one way or another:

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 is actually copyable.

like image 61
Dietmar Kühl Avatar answered Oct 13 '22 14:10

Dietmar Kühl