One of my assignments involve creating a class using a constructor with parameters, where the arguments sent to create the object are based on user input.
#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);
}
Note: This is from a .cpp file where member functions are fully written. All other class syntax is in a header file.
I approached creating an object with this constructor using std::cin with variables through main.
#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;
}
Is there a more direct way of creating the same object without the need for variables in main?