gpt4 book ai didi

c++ - 是否可以在 C++ 的 Mutator 中使用 Mutator?

转载 作者:行者123 更新时间:2023-11-28 02:40:52 28 4
gpt4 key购买 nike

下午好!

我做了一些快速搜索,我很难弄清楚我应该如何去做我需要做的事情。

对于这个程序,我们正在创建一个基本的工作票类。每个属性都有自己的修改器和访问器,但除此之外还有一个修改器将所有属性作为参数并一次设置它们。

我不能只使用构造函数来放置值的主要原因是,对于这个增变器,我们必须对值执行一些验证。对于单个属性修改器,其中两个已经在其修改器中执行了验证。其他两个只需要在调用 setWorkTicket 修改器时进行验证。

有没有办法从一个增变器中调用一个增变器?或者确保它验证这两个属性的唯一方法是将它们的验证从它们的增变器复制并粘贴到 setWorkTicket 中?

如果您需要澄清,或者我的问题不清楚,请告诉我。我附上了相关类(class)。

/** Lab2.cpp
* @desc SOME DESCRIPTION
* @author Aaron, Aaron
* @since 24 September 2014
**/


#include <iostream>
#include <iomanip>
#include <string>
#include "MyInputValidation.h"
#include <sstream>
#include <stdexcept>

using namespace std;

class WorkTicket
{
public:
/*** PARAMETERIZED CONSTRUCTOR ***/
WorkTicket(): myWorkTicketNumber(0), myClientID(""), myDay(1), myMonth(1), myYear(2000), myIssue("") {}

WorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue);

/*** ACCESSORS ***/
int getWorkTicketNumber() const { return myWorkTicketNumber; }

string getClientID() const { return myClientID; }

string getDate(int myDay, int myMonth, int myYear) const;

string getIssue() const { return myIssue; }

void showWorkTicket() const;

/*** MUTATORS ***/
void setWorkTicketNumber(int workTicketNumber);

void setClientID(string clientID) { myClientID = clientID; }

void setDate(int day, int month, int year);

void setIssue(string issue) { myIssue = issue; }

bool setWorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue);


private:
int myWorkTicketNumber;
string myClientID;
int myDay;
int myMonth;
int myYear;
string myIssue;

};

/*** MAIN ***/
int main ()
{
// Output an information header
cout << "=============================" << endl
<< "=== WorkTicket Class Demo ===" << endl
<< "=============================" << endl;

try
{
// DECLARATIONS
WorkTicket ticketOne(1, "Aaron", 24, 9, 2014, "Printer not accessible from networked computers.");

ticketOne.showWorkTicket();

}
catch(const exception& ex)
{
cerr << ex.what(); // display the exception message.
}


// done.
cout << endl << endl;
return 0;
}

/*** PARAMETERIZED CONSTRUCTOR ***/
WorkTicket::WorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue)
{
setWorkTicketNumber(workTicketNumber);
setClientID(clientID);
setDate(day, month, year);
setIssue(issue);
}

/*** ACCESSORS ***/
string WorkTicket::getDate(int myDay, int myMonth, int myYear) const
{
stringstream dateReturn;

dateReturn << myDay << "-" << myMonth << "-" << myYear;

return dateReturn.str();
}

void WorkTicket::showWorkTicket() const
{
cout << setw(20) << "\nWork Ticket Number: " << myWorkTicketNumber << endl
<< setw(20) << left << "Client ID: " << myClientID << endl
<< setw(20) << left << "Ticket Date: " << myDay << "-" << myMonth << "-" << myYear << endl
<< setw(20) << left << "Issue: " << myIssue << endl;
}

/*** MUTATORS ***/
void WorkTicket::setWorkTicketNumber(int workTicketNumber)
{
// Local declarations
const int MIN_TICKET_NUMBER = 1; // Ace

if(workTicketNumber < MIN_TICKET_NUMBER)
{
// declare a stringstream object
stringstream strOut;
// build a descriptive error string.
strOut << "\nTicket Number argument: " << workTicketNumber << " is out of range. " << endl
<< "Ticket Number must be greater than or equal to " << MIN_TICKET_NUMBER << ". ";

// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else // otherwise, the rank parameter is fine
{
// assign the parameter to the member variable
myWorkTicketNumber = workTicketNumber;
}
}

void WorkTicket::setDate(int day, int month, int year)
{
const int MIN_DAY_MONTH = 1;
const int MAX_DAY = 31;
const int MAX_MONTH = 12;
const int MIN_YEAR = 2000;
const int MAX_YEAR = 2099;

stringstream strOut;

if(day < MIN_DAY_MONTH || day > MAX_DAY)
{
// build a descriptive error string.
strOut << "\nDate argument: " << day << " is out of range. " << endl
<< "Day must be greater than or equal to " << MIN_DAY_MONTH
<< " and less than or equal to " << MAX_DAY << ". ";

// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else if(month < MIN_DAY_MONTH || month > MAX_MONTH)
{
// build a descriptive error string.
strOut << "\nDate argument: " << month << " is out of range. " << endl
<< "Month must be greater than or equal to " << MIN_DAY_MONTH
<< " and less than or equal to " << MAX_MONTH << ". ";

// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else if(year < MIN_YEAR || year > MAX_YEAR)
{
// declare a stringstream object
stringstream strOut;
// build a descriptive error string.
strOut << "\nDate argument: " << year << " is out of range. " << endl
<< "Year must be greater than or equal to " << MIN_YEAR
<< " and less than or equal to " << MAX_YEAR << ". ";

// throw an out_of_range exception initialized with
// the error string
throw invalid_argument(strOut.str());
}
else // otherwise, the rank parameter is fine
{
// assign the parameter to the member variable
myDay = day;
myMonth = month;
myYear = year;
}
}

bool WorkTicket::setWorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue)
{


return true;
}

最佳答案

只是这样做有什么问题:

bool WorkTicket::setWorkTicket(int workTicketNumber, string clientID, int day, int month, int year, string issue)
{
setWorkTicketNumber(workTicketNumber);
setDate(day, month, year);
setClientID(clientID);
setIssue(issue);

return true;
}

您可以让异常传播或在 setWorkTicket() 中捕获它们并根据需要返回 false

关于c++ - 是否可以在 C++ 的 Mutator 中使用 Mutator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26043435/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com