I'm an aspiring software engineer and full-time CS student. During the holiday break, I have been working on exercises to be better at my craft (C++). There are topics that I just need some clarity on. I'm working on a Target Heart Rate Calculator.
I have spent hours, days, trying to understand the concept of the constructor vs default constructor. I know that a constructor is required for every object created.
Before going any further, I want to test my code where it prompts the user to enter their first name and then return it. For once and for all, for the slow learners out there struggling including myself, can anyone please just explain this in Layman's terms with a visual explanation, please?
Below is my code. I'm receiving an error:
no matching function for call to 'HeartRates::HeartRates()'
main.cpp
int main() {
HeartRates patient;
cout << "First name: ";
string firstName;
cin >> firstName;
patient.setFirstName(firstName);
patient.getFirstName();
return 0;
HeartRate.h
// create a class called HeartRates
class HeartRates {
public:
// constructor receiving data
HeartRates(string personFirstName, string personLastName, int month, int day, int year) {
firstName = personFirstName;
lastName = personLastName;
birthMonth = month;
birthDay = day;
birthYear = year;
}
void setFirstName(string personFirstName) {
firstName = personFirstName;
}
string getFirstName() {
return firstName;
}
private:
// attributes
string firstName, lastName;
int birthMonth, birthDay, birthYear;
};
Thank you, everyone! Sheesh! My book adds all these extra words that aren't necessary and makes the reading hard. All it had to say was:
If you want to create an object to receive information from a user, make sure your constructor has empty (). If you have data to input manually, pass that data through your constructor's parameters.
I hope this was the guise of your explanation. I love this community - thank you so much! You all have no idea about my back story - basically transitioning from a 15 years marketing/advertising career to becoming a software engineer. You all have been so welcoming and it confirms I made a great decision to switch.✊
Here is my updated code:
main.cpp
int main() {
HeartRates patient;
cout << "First name: ";
string firstName;
cin >> firstName;
patient.setFirstName(firstName);
cout << patient.getFirstName();
return 0;
}
HeartRates.h
// create a class called HeartRates
class HeartRates {
public:
// constructor receiving data - THANKS STACKOVERFLOW COMMUNITY
HeartRates() {
firstName;
lastName;
birthMonth;
birthDay;
birthYear;
}
void setFirstName(string personFirstName) {
firstName = personFirstName;
}
string getFirstName() {
return firstName;
}
private:
// attributes
string firstName, lastName;
int birthMonth, birthDay, birthYear;
};