Learn C++ - Classes & Objects Cheatsheet - Codecademy
Learn C++ - Classes & Objects Cheatsheet - Codecademy
Class Members
A class is comprised of class members:
class City {
● Attributes, also known as member data, consist of
information about an instance of the class.
// Attribute
● Methods, also known as member functions, are int population;
functions that can be used with an instance of the
class. public:
// Method
void add_resident() {
population++;
}
};
Constructor
For a C++ class, a constructor is a special kind of method
that enables control regarding how the objects of a class #include "city.hpp"
should be created. Different class constructors can be
specified for the same class, but each constructor class City {
signature must be unique.
std::string name;
int population;
public:
City(std::string new_name, int new_pop);
};
Objects
In C++, an object is an instance of a class that
encapsulates data and functionality pertaining to that City nyc;
data.
● public
int population;
● private
public:
public members are accessible everywhere; private
void add_resident() {
members can only be accessed from within the same
population++;
instance of the class or from friends classes.
}
private:
bool is_capital;
};
Class
A C++ class is a user-defined data type that encapsulates
information and behavior about an object. It serves as a class Person {
blueprint for future inherited classes.
};