KEMBAR78
Learn C++ - Classes - Objects Cheatsheet - Codecademy | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
43 views2 pages

Learn C++ - Classes - Objects Cheatsheet - Codecademy

Classes in C++ allow developers to create user-defined data types that encapsulate both data (attributes) and functionality (methods). A class serves as a blueprint for objects and future inherited classes. Classes contain public and private members, with public members accessible everywhere and private members only accessible within the class. Constructors enable control over object creation and destructors handle object destruction to prevent memory leaks.

Uploaded by

Vatsal
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Learn C++ - Classes - Objects Cheatsheet - Codecademy

Classes in C++ allow developers to create user-defined data types that encapsulate both data (attributes) and functionality (methods). A class serves as a blueprint for objects and future inherited classes. Classes contain public and private members, with public members accessible everywhere and private members only accessible within the class. Constructors enable control over object creation and destructors handle object destruction to prevent memory leaks.

Uploaded by

Vatsal
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Learn C++

Classes & Objects


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

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

};

Access Control Operators


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


public int population;

private
public:
public members are accessible everywhere; void add_resident() {
private members can only be accessed from within population++;
the same instance of the class or from friends classes. }

private:
bool is_capital;

};

Objects
In C++, an object is an instance of a class that
encapsulates data and functionality pertaining to that City nyc;
data.

/
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);

};

Destructor
For a C++ class, a destructor is a special method that
handles object destruction, generally focused on City::~City() {
preventing memory leaks. Class destructors don’t take
arguments as input and their names are always // Any final cleanup
preceded by a tilde ~ .
}

You might also like