Learn C++ - Classes & Objects Cheatsheet
Learn C++ - Classes & Objects Cheatsheet
Class Members
public:
// Method
void add_resident() {
population++;
}
};
Constructor
std::string name;
int population;
public:
City(std::string new_name, int new_pop);
};
https://www.codecademy.com/learn/learn-c-plus-plus/modules/learn-cpp-classes-and-objects/cheatsheet 1/3
2/2/24, 6:52 PM Learn C++: Classes & Objects Cheatsheet | Codecademy
Objects
Class
C++ classes have access control operators that designate class City {
the scope of class members:
public
private int population;
public members are accessible everywhere;
private members can only be accessed from within public:
the same instance of the class or from friends classes.
void add_resident() {
population++;
}
private:
bool is_capital;
};
Destructor
https://www.codecademy.com/learn/learn-c-plus-plus/modules/learn-cpp-classes-and-objects/cheatsheet 2/3
2/2/24, 6:52 PM Learn C++: Classes & Objects Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-c-plus-plus/modules/learn-cpp-classes-and-objects/cheatsheet 3/3