The function setName allows you to change the name even after the constructor has set name's value. Apart from that the code is identical. Just remember to #include <string>
Are there any advantages to one or the other? The first code gives you an easy way to fix a misspelled name. The second code on the other hand is more unchangeable if you don't want someone changing the value of name.
Thank you!
So without having the setName function we wont be able to change the name later one and will be able to change it once and when the object is created.
I'm new to c++ and this forum and i am already loving getting fast responses here.
also note that the constructor (example 1) reused set name, this is good. code reuse is better than repeating the code usually, even if trivial. That way if the logic changes you can fix it in one place (maybe you want to do nothing if they try to set it to empty string as a new requirement, for a silly example).