如何使用用户输入构建一个 class 而没有变量?
How to construct a class with user inputs and no variables?
我的一项作业涉及使用带参数的构造函数创建 class,其中发送用于创建对象的参数基于用户输入。
#include <iostream>
#include "HRCalc_lib.h"
HeartRates::HeartRates(const std::string &first, const std::string &last,
int day, int month, int year){
firstName = first;
lastName = last;
setBirthYear(year);
setBirthMonth(month);
setBirthDay(day);
}
注意:这是来自一个.cpp
文件,其中成员函数被完全写入。所有其他 class 语法都在头文件中。
我通过 main
.
使用带有变量的 std::cin
来使用此构造函数创建一个对象
#include <iostream>
#include "HRCalc_lib.h"
int main(){
std::string first, last;
int Bday, Bmonth, Byear;
std::cout << "enter your name (first last) & date of birth (dd mm yyyy) seperated by spaces" << std::endl;
std::cin >> first >> last >> Bday >> Bmonth >> Byear;
HeartRates person1(first, last, Bday, Bmonth, Byear);
//further code would be implemented here
return 0;
}
是否有更直接的方法来创建相同的对象而不需要在 main 中使用变量?
Is there a more direct way of creating the same object without the need for variables to store user inputs in main?
是的,有。您可以使用 运算符重载 。特别是,您可以重载 operator>>
,如下所示:
#include <iostream>
#include<string>
class HeartRates
{
private:
std::string first, last;
int day = 0, month = 0, year = 0;
//friend declaration for overloaded operator>>
friend std::istream& operator>>(std::istream& is, HeartRates& obj);
};
//implement overloaded operator<<
std::istream& operator>>(std::istream& is, HeartRates& obj)
{
is >> obj.first >> obj.last >> obj.day >> obj.month >> obj.year;
if(is)//check that input succeded
{
//do something here
}
else //input failed: give the object a default state
{
obj = HeartRates();
}
return is;
}
int main(){
HeartRates person1;
//NO NEED TO CREATE SEPARATE VARIABLES HERE AS YOU WANT
std::cout << "enter your name (first last) & date of birth (dd mm yyyy) seperated by spaces" << std::endl;
std::cin >> person1; //this uses overloaded operator>>
return 0;
}
上面程序的输出可见here.
这里写的时候:
std::cin >> person1;
我们正在使用重载 operator>>
,因此您不需要在 main
.
中创建单独的变量
我的一项作业涉及使用带参数的构造函数创建 class,其中发送用于创建对象的参数基于用户输入。
#include <iostream>
#include "HRCalc_lib.h"
HeartRates::HeartRates(const std::string &first, const std::string &last,
int day, int month, int year){
firstName = first;
lastName = last;
setBirthYear(year);
setBirthMonth(month);
setBirthDay(day);
}
注意:这是来自一个.cpp
文件,其中成员函数被完全写入。所有其他 class 语法都在头文件中。
我通过 main
.
std::cin
来使用此构造函数创建一个对象
#include <iostream>
#include "HRCalc_lib.h"
int main(){
std::string first, last;
int Bday, Bmonth, Byear;
std::cout << "enter your name (first last) & date of birth (dd mm yyyy) seperated by spaces" << std::endl;
std::cin >> first >> last >> Bday >> Bmonth >> Byear;
HeartRates person1(first, last, Bday, Bmonth, Byear);
//further code would be implemented here
return 0;
}
是否有更直接的方法来创建相同的对象而不需要在 main 中使用变量?
Is there a more direct way of creating the same object without the need for variables to store user inputs in main?
是的,有。您可以使用 运算符重载 。特别是,您可以重载 operator>>
,如下所示:
#include <iostream>
#include<string>
class HeartRates
{
private:
std::string first, last;
int day = 0, month = 0, year = 0;
//friend declaration for overloaded operator>>
friend std::istream& operator>>(std::istream& is, HeartRates& obj);
};
//implement overloaded operator<<
std::istream& operator>>(std::istream& is, HeartRates& obj)
{
is >> obj.first >> obj.last >> obj.day >> obj.month >> obj.year;
if(is)//check that input succeded
{
//do something here
}
else //input failed: give the object a default state
{
obj = HeartRates();
}
return is;
}
int main(){
HeartRates person1;
//NO NEED TO CREATE SEPARATE VARIABLES HERE AS YOU WANT
std::cout << "enter your name (first last) & date of birth (dd mm yyyy) seperated by spaces" << std::endl;
std::cin >> person1; //this uses overloaded operator>>
return 0;
}
上面程序的输出可见here.
这里写的时候:
std::cin >> person1;
我们正在使用重载 operator>>
,因此您不需要在 main
.