0% found this document useful (0 votes)
4 views3 pages

Learn C++_ Classes & Objects Cheatsheet _ Codecademy

This document is a cheatsheet for learning C++ classes and objects, detailing key concepts such as class members, constructors, objects, and access control operators. It explains the structure of a class, the purpose of constructors and destructors, and how to manage access to class members. The document includes code examples to illustrate these concepts.

Uploaded by

Federico Massaro
Copyright
© © All Rights Reserved
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)
4 views3 pages

Learn C++_ Classes & Objects Cheatsheet _ Codecademy

This document is a cheatsheet for learning C++ classes and objects, detailing key concepts such as class members, constructors, objects, and access control operators. It explains the structure of a class, the purpose of constructors and destructors, and how to manage access to class members. The document includes code examples to illustrate these concepts.

Uploaded by

Federico Massaro
Copyright
© © All Rights Reserved
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/ 3

23/10/24, 09:53 Learn C++: Classes & Objects Cheatsheet | Codecademy

Cheatsheets / Learn C++

Classes & Objects

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.
Methods, also known as member functions, are // Attribute
functions that can be used with an instance of int population;
the class.

public:
// Method
void add_resident() {
population++;
}

};

Constructor

For a C++ class, a constructor is a special kind of #include "city.hpp"


method that enables control regarding how the objects
of a class should be created. Different class
constructors can be specified for the same class, but class City {
each constructor signature must be unique.

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
23/10/24, 09:53 Learn C++: Classes & Objects Cheatsheet | Codecademy

Objects

In C++, an object is an instance of a class that City nyc;


encapsulates data and functionality pertaining to that
data.

Class

A C++ class is a user-defined data type that class Person {


encapsulates information and behavior about an object.
It serves as a blueprint for future inherited classes.
};

Access Control Operators

C++ classes have access control operators that class City {


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

private:
bool is_capital;

};

Destructor

For a C++ class, a destructor is a special method that City::~City() {


handles object destruction, generally focused on
preventing memory leaks. Class destructors don’t take
arguments as input and their names are always // Any final cleanup
preceded by a tilde ~ .

https://www.codecademy.com/learn/learn-c-plus-plus/modules/learn-cpp-classes-and-objects/cheatsheet 2/3
23/10/24, 09:53 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

You might also like