FAQ: Classes and Objects - Access Control: Public and Private

This community-built FAQ covers the “Access Control: Public and Private” 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 Access Control: Public and Private

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!

I don’t understand the essence of access modifiers such as public or private in classes if for example everything in a class is private , meaning class members are limited to the scope of the class.
What is the point if object instances have no access to its class members ?
Please correct me if I am misunderstanding this concept or at least clarification. Thank you.

Hello,

My apologies if the questions are very basic (I’m used to Python and just beginning in C++) or off-topic.

This is the code of the song.hpp file:

#include

// add the Song class here:
class Song {

std::string title;

public:
void add_title(std::string new_title);
std::string get_title();

};

In the song.cpp file I was expecting that the “Song ::add_title” method (see code ) I needed to return the “title” but it is not. In that case when I call the method how will the function call return a string?

// add Song method definitions here:
void Song::add_title(std::string new_title) {

  title = new_title;

}

The second question is related with this other method :
In this case I don’t pass any parameter but the function returns “title”.

std::string Song::get_title() {

  return title;

}

My guess is that the “title” variable was defined previously in the Song class so any method inside that class can use that variable. Is this correct?

Thank you in advance.
iroupa

the method won’t return a string, void means no value is returned.

Hi stetim94.

Thank you for your help.
I understand that void means that no value is returned but what I would like to mean in my question is how does the function assigns title to the variable if the function doesn’t return any value?

title is an attribute (also known as member data), as explained in this lesson:

https://www.codecademy.com/courses/learn-c-plus-plus/lessons/cpp-classes-and-objects/exercises/cpp-class-members

we can assign to attributes like title, which is very different from return (handing back data to caller)

if we where to do this:

void Song::add_title(std::string new_title) {

  title = new_title;
  return tilte
}

we would have a problem.

If I understood it correctly, your explanation answers my first (no return case ) and second question (return inside function), which means that if I define a method like the get_title inside a class and I want to use it main to get that attribute I will always need to use return right?

Thank you for your explanation

for example, this:

#include <iostream>
#include "song.hpp"

int main() {

  Song electric_relaxation;
  
  electric_relaxation.add_artist("A Tribe Called Quest");
  std::cout << electric_relaxation.get_artist();
  
}

we print (cout) the artist, by printing the returned data

but we could also make a method which does the printing for us, and then doesn’t have a return value (void).

Its important to understand what return does, we can also just return a string if we want:

#include <iostream>
#include "song.hpp"

int main() {

  Song electric_relaxation;
  
  electric_relaxation.add_artist("A Tribe Called Quest");
  std::cout << electric_relaxation.hello_world();
  
}
#include <string>

class Song {
  
  std::string title;
  std::string artist;

public:
  void add_title(std::string new_title);
  std::string get_title();
  
  void add_artist(std::string new_artist);
  std::string get_artist();
  
  std::string hello_world();
  
};
#include "song.hpp"

// add Song method definitions here:
void Song::add_title(std::string new_title) {

  title = new_title;

}

std::string Song::get_title() {

  return title;

}

void Song::add_artist(std::string new_artist) {

  artist = new_artist;

}

std::string Song::get_artist() {

  return artist;

}

std::string Song::hello_world(){
  return "hello world";
}

Thank you for your detailed example and my apologies if the questions were to basic but as I referred previously I’m just begining with C++ and OOP.

I’m not sure what you mean, but if you mean that what is the point of private it’s so that your program doesn’t get messed up by accidentally altering it and so it can be more organized from all the other code. Please let me know if you need more help.

Does all c++ functions need to be declared with a return type as such:
[return_type] [function_name] () {
}
Because I did not specify any return type (not even void) and the exercise gave me a pass. That’s why I wonder

yes, return type is always required (or void). If either the exercise or compiler gives an error, its generally not good

don’t just assume because the lesson doesn’t give an error that everything is fine. Writing air tide validation is a very time consuming task.

Well, in exercise 5 about constructors. The constructor function has no return type it is just declared as:
Class_name(…);
It has not return type in it’s declaration.

constructors are the exception. That is because constructors should never return any values. So in a sense its void, despite being explicitly mentioned by the developer.

1 Like

add_title() is only for modifying the title. It just needs to update the private variable artist. So it doesn’t need a return type.
get_title() on the other hand is used to get (retrieve) the private variable artist. So it’s return type is string

The title variable is only declared in the Song class. You give it a value by using the add_title() method. The methods can access the private variable title as they are defined in the same scope as title. So to change title, instead of accessing it directly, we use a function (getter/setter) to access it.

Hope there’s no errors in this explanation