Search⌘ K

Encapsulation

Explore how encapsulation hides data within C++ classes by using private variables and public getter and setter methods. Learn to design interfaces that control access to class members, making your code easier to maintain and modify.

We'll cover the following...

A Real Life Example

For the sake of explanation, we’ll start off by creating a simple movie class which contains three members:

C++
class Movie{
string title;
int year;
string genre;
public:
Movie(){
title = "";
year = -1;
genre = "";
}
Movie(string t, int y, string g){
title = t;
year = y;
genre = g;
}
};

There must be a way to interact with the title, year and genre ...