FAQ: Classes and Objects - Class Members

This community-built FAQ covers the “Class Members” 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 Class Members

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!

Moderators - there is a bug in this exercise.

The following is shown with the correct code:
2019-12-06_16-22-26

I recommend going through it to see for yourself.

Here’s some screenshots of my code that generates the “Does Song have public: in it?” error.


When you view the solution it’s the exact same code but works…

2 Likes

If I remove the comment

// methods

it works…

2 Likes

Unfortunately unhelpful error message. Same happened to me (I didn’t declare the parameter’s type).

Same for me on that exercise… Frustrating as I had it all correct and was staring at it for an hour wondering what I had done wrong.

This was a poorly-done lesson.

The example for creating a class has the keyword “class”, as in:

class Song {

but the first hint says:

`Song` should be structured like this:
Song {

So does the keyword “class” belong there or not?

====

Instruction #2 says to make a new method:

`.add_title()` , a `void` method which accepts a `new_title` for a song. (Make sure this parameter has a different name than `title` .)

with no discussion about the leading dot.

Worse, we’re left to guess about the type for this method’s parameter. Is new_title a new type we haven’t come across before? It obviously isn’t supposed to be the name of the parameter, because the next line tells us to use (i.e., choose) a name other than “title”. If my choice of a name is part of the assignment, then this “new_title” phrase you’ve given me must be something different, such as that new type I’m wondering about, that I don’t remember reading about, and can’t find any information about. (The “Show solution” option eventually showed me that this was indeed supposed to be the name of the parameter. Why then tell me to use some name other than “title” as the parameter, if you’re going to tell me what name to use?)

====

The resources on this lesson’s page do not provide enough information to sort out these issues, so the only real solution is to choose the option to “Show the solution”. But that shows the solution for all of the steps in this lesson, which means the third step in this lesson gets completely skipped; I didn’t have a chance to even begin to look at that part of the challenge.

====

Later, after I had written most of this comment, I went back and looked at the third instruction’s solution (as I hadn’t yet had a chance to even begin to think about it / look at it before the lesson was over for me (see above)), and I see that I would have been confused about it because of the scope of “title”. There really should have been some discussion about the scope of attributes within a class; I saw

title = new_title

and wondered, “Where did ‘title’ come from?! It’s not defined/declared here in this method. And how does it get out of here back to the method/function call? There’s no “return title” statement. Is it a global variable?”

So i went back and looked around, and was reminded that it is in the attribute section of the “class Song”, and then realized those attributes must have global scope in all things “Song”-ish. Some discussion on these matters would have been helpful.

====

This whole exercise (as well as the previous one, but not as badly) feels like the author was in a hurry to leave work that day, and settled with doing a poorly-executed rush job.

6 Likes

I found this resource on the internet which will be helpful to understand classes easily

4 Likes

Agreed. This lesson felt like I skipped a couple lessons. There were so many things unexplained. It didn’t make sense.

what is the actual solution for part 2?? I’m still getting the error:
“Does ‘Song’ have a member function ‘.add_title()’ declared?”

Here’s my code

`#include

// add the Song class here:

class Song{

std::string title;

public:

void add_title(std::string new_title){
title=new_title;
}

std::string get_title(){
return;
}

};`