Classes

Published Jun 4, 2021Updated Dec 21, 2022
Contribute to Docs

In C++, a 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
};

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;
};

All contributors

Looking to contribute?

Learn C++ on Codecademy