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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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.

1 Like

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?

2 Likes

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.

3 Likes

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”);

2 Likes

In the 7th Line where you have done title = "XYZ’
I t should be title = (“XYZ”)

I basically did the same thing and it worked on the codecademy site but in VSCode it doesnt work and the error is:

undefined reference to `Song::add_title(std::__cxx11::basic_string<char, std::char_traits, std::allocator >)’

undefined reference to `Song::get_titleabi:cxx11

collect2.exe: error: ld returned 1 exit status

When i try to run it manualy with:
g++ music.cpp song.cpp -o main.exe

nothing happens

Can we access the attributes of a class?
In the exercise, I think it gonna be quicker if we print out the attribute itself (which already be given value) instead of printing return value of a method.

We mainly prefer to use functions to return values and the main() to show printed values in console. Is a good practice that you will have to deal preferably.

Hi all,
so sad Classes & Objects are very very poorly explained anyway I will find another resource to understand, it motivates me more