Classes

Christine_Yang269 total contributions
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 {// Attributeint population;public:// Methodvoid 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
- Christine_Yang269 total contributions
- garanews209 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- Christine_Yang
- garanews
- christian.dinh
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.