FAQ: Classes and Objects - Creating Objects

This community-built FAQ covers the “Creating Objects” exercise from the lesson “Classes and Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Creating Objects

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why do we not set title as a parameter for get_title() like we did with other functions? Is it because it’s already defined in the class scoop and isn’t needed as a parameter anymore?

Recall that parameters are optional when defining functions.

While the first method, add_title, assigns a value to title, get_title simply returns a value – the string “title.”

These could have been combined in a single function. And while the course presentation is showing us different methods (functions) that are part of the class, there is also a practical benefit to this approach demonstrated in the next part of the lesson.

Not gonna lie. Not quite sure I understand the point of functions. Anyone care to enlighten me?

Functions are used for repetitive code. If you remember from an earlier lesson which was the IT support we had to create functions to make our code more organized and less repetitive.

when using get_title() why do we need to use “std::cout<<” before calling it? I thought the get_title() function printed the title name?

Hello, @itsmariawood, and welcome to the forums.

If you look at the get_title() method in the song.cpp file, you’ll see there is only a single line of code inside the method:

std::string Song::get_title() {

  return title;

}

return hands a value back to the line of code that called the method. return does not print anything, it only returns values. std::cout<< is the command to print something to the console. The something printed in this case is the value returned by the method call which is the song title.

1 Like

Good day to all.

I’m stuck with the second point in the creating objets instructions. I don´t know what i’m doing bad, but i can’t find the solution.

Can you tell me what i am doing wrong, please?

Thanks!

1 Like

You have to pass in the title as a parameter. Since “add_title” is a function and not a parameter (i.e. int or bool are parameters), the title cannot be simply assigned with an “=”.

Instead you need to pass in the argument as: electric_relaxation.add_title(“Electric Relaxation”);