在条件c ++语句中创建对象 [英] Create objects in conditional c++ statements

查看:142
本文介绍了在条件c ++语句中创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习c ++,而我刚刚学习了面向对象的章节。我有一个关于在if语句中创建对象的问题。

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).

问题是,具体而言,双参数默认构造函数应该允许在创建新的Report对象时指定这些[公司和报告名称]。如果用户创建了一个Report对象而没有传递任何参数,请使用默认值。否则,请使用用户指定的名称值。

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."

所以我的问题是,如何创建这些对象?我理解如何创建一个没有任何参数的对象(即Report newobj;),以及参数(即Report newobj(string string);)。基本上,我首先了解如何在main函数的顶部创建这些对象。但是有可能在基于用户选择的if语句中创建它们吗?这是我到目前为止,显然,它不起作用:

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;
}

所以我想使用默认值创建一个对象(如果没有指定),或者如果这是给定的选择,则使用用户值创建一个。我只是无法弄清楚如何在c ++中以交互方式进行。到目前为止,我无法在任何地方找到任何类似的问题。

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.

提前谢谢!

推荐答案

我不知道我是否正确理解了你的问题,但你不能在if / else块之前声明报告然后在其中初始化吗?

I don't know if I understood your question correctly but can't you just declare report before the if/else block and then initialize inside it?

Report header;

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

或以较短的方式:

Report header; // calls default constructor

if (shouldInitializeWithParams) {
  header = Report(name,company);
}

当然这需要你定义空构造函数。

Of course this requires you to have the empty constructor defined.

这篇关于在条件c ++语句中创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆