在条件 C++ 语句中创建对象

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

问题描述

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

我正在处理的问题是创建一个将显示报告标题的类.该类有一个默认构造函数,用于将公司名称和报告名称设置为通用内容,并且如果用户需要,还具有一个带有两个参数(字符串公司名称和报告名称)的构造函数.

问题特别指出,一个有两个参数的默认构造函数应该允许在创建新的 Report 对象时指定这些 [公司和报告名称].如果用户创建了一个 Report 对象而不传递任何参数,使用默认值.否则,使用用户指定的名称值."

所以我的问题是,如何创建这些对象?我了解如何创建一个没有任何参数的对象(即报告 newobj;),也有参数(即报告 newobj(string string);).基本上,我知道如何最初在我的主函数顶部创建这些对象.但是是否可以根据用户选择在 if 语句中创建它们?这是我到目前为止所拥有的,显然它不起作用:

#include #include <字符串>#include "report.h"使用命名空间标准;bool enter_company_name();//如果用户想输入公司名称,则返回真bool print_form();//如果用户想在格式化输出中打印,则返回 trueint main(){字符串公司名称,报告名称;布尔名称 = 假,格式=假;name = enter_company_name();格式 = print_form();如果(姓名){cout<<"输入公司名称:";getline(cin, company_name);cout<<"输入报告名称:";getline(cin,report_name);报告标题(公司名称,报告名称);//这是我的问题}别的报告标题;//这是我的问题如果(格式)header.print_formatted();别的header.print_one_line();返回0;}bool enter_company_name(){字符选择;cout<<"你想输入一个名字吗?
>";cin >>选择;如果(选择 == 'y' || 选择 == 'Y')返回真;别的返回假;}bool print_form(){字符选择;cout<<"你想打印一个格式化的标题吗?
>";cin >>选择;如果(选择 == 'y' || 选择 == 'Y')返回真;别的返回假;}

所以如果没有指定,我想使用默认值创建一个对象,或者如果这是给定的选择,则使用用户值创建一个.我只是不知道如何在 C++ 中以交互方式进行.到目前为止,我在任何地方都找不到任何类似的问题.

我遇到的最接近的事情是使用指针来做类似于我想做的事情,但是我正在使用的书还没有使用指针,我想尝试找出一种方法来做它保持在我正在处理的章节的范围内(即不使用指针).

我没有包含头文件或类实现文件,因为我认为它们在这里不相关.

先谢谢你!

解决方案

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

>

报告头;如果 (...) {标题 = 报告();别的标题 = 报告(姓名,公司);

或者用更短的方式:

报告头;//调用默认构造函数如果(shouldInitializeWithParams){标题 = 报告(姓名,公司);}

当然,这需要您定义空构造函数.

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?
>";
  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?
>";
  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!

解决方案

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

Or in a shorter way:

Report header; // calls default constructor

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

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

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

上一篇:我需要将 constexpr 放在 else-if 之后吗? 下一篇:C++ if 使用字符串的语句不能按预期工作

相关文章

最新文章