-4

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;
};
5
  • 1
    Supporting information. Commented Jan 9, 2022 at 17:53
  • 1
    A default constructor is a constructor without any required parameters. The End. Which part of this is unclear to you, and what exactly is unclear? Commented Jan 9, 2022 at 17:55
  • Constructor is a generic term. In C++ there are different types of constructors like default constructor, parameterized constructor, copy constructor, move constructor etc. Commented Jan 9, 2022 at 17:56
  • Does this answer your question? Default vs. Implicit constructor in C++ Commented Jan 9, 2022 at 17:59
  • Side note: If you offer people a way to make an object that does not contain all the information the object should hold, an incomplete object, someone will forget to add the missing information later and sooner or later you'll have a bug. The object will be fully constructed and won't contain anything immediately fatal like an uninitialized pointer, but it sucks to have to wonder whether or not an object was fully filled out by the time it gets to you. You can't always do everything ahead of time and supply perfect, complete objects from the get-go, but if you can, do it. Commented Jan 9, 2022 at 18:13

3 Answers 3

2

If you don't define any constructor, you get a constructor that takes no arguments.

As soon as you defined a constructor that has arguments, your no-args constructor retired to the North Pole.

So now you must write HeartRate("first", "last", 1, 1, 2001)

If you don't want to write that, delete the parameter list from your constructor and just set whatever you want for initial values.

Sign up to request clarification or add additional context in comments.

1 Comment

OMG - thank you! It works! See my book just adds all these extra words that confuse me. 👏🏽
2

Default constructor is any constructor that can be called with no arguments.

  • MyClass() - default
  • MyClass(int x) - not default
  • MyClass(int x = 42) - default

If you don't define any constructor for your class, a compiler generates a default constructor automatically. It looks more or less like MyClass() {}.

HeartRates patient; tries to call a default constructor, since you didn't provide any arguments (that would be HeartRates patient(...);).

2 Comments

OMG - thank you! It works! See my book just adds all these extra words that confuse me. 👏🏽
@rudycito You got to google the meaning of words you don't understand. Or find a better book, if there are too many of those.
0

The difference between a default constructor and "normal" constructor is simpler than it may seem. As long as you don't specify the constructor in your class, when creating an object of an class, it always will call the default constructor, which, if you haven't implemented one, will do nothing but creating an object of that specific class with nothing initialized. If you now have implemented your own constructor, with some parameters, the only way to construct an object of that class will require you to call the implemented constructor. Your default constructor is then overwritten.

You can imagine it as a function you only defined. Something like this:

(the function foo is in this example as your constructor)

There is always a

void foo();

function

Whenever you call foo() it will call that specific function doing nothing. And the function foo() is alway given. If you now declare a second foo function like this:

void foo(std::string name)
{
   std::cout << "This is the name " << name << std::endl;
}

and you call your foo function, it will then call the new foo function that you have implemented.

Long story short, if you only have created a class with no customized constructor, you always have a default constructor that is been called when creating an object, but not really initializing something. Whenever you write your own customized constructor, it will overwrite the default constructor.

1 Comment

Unfortunately it's not as simple as Whenever you create a class, you always have an default constructor. There are several times that you DON'T get a default constructor. The most common is the one the asker ran into: You specify another non-default constructor. Since you get back to this later in the answer, you should remove this sentence.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.