Classes

Classes

A C++ class is a user-defined data type that encapsulates information and behavior about an object. It serves as a blueprint for future inherited classes.

class Person {
};

Class Members

A class is comprised of class members:

  • Attributes, also known as member data, consist of information about an instance of the class.
  • Methods, also known as member functions, are functions that can be used with an instance of the class.
class City {
// Attribute
int population;
public:
// Method
void add_resident() {
population++;
}
};

Access Control Operators

C++ classes have access control operators that designate the scope of class members:

  • public members are accessible everywhere.
  • private members can only be accessed from within the same instance of the class or from friends classes.
class City {
int population;
public:
void add_resident() {
population++;
}
private:
bool is_capital;
};
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn C++ on Codecademy

Edit this page on GitHub

Contributors