<aside>
💡 Key concepts:
- Basic data types: numbers and strings
- Classes and data
- Classes and functions
Learning outcomes:
- Select appropriate data types to represent a dataset in a C++ program
- Describe how a class can be used to combine multiple pieces of data into one unit
- Write a class with functions
</aside>
2.1. Concepts
Basic Data Types: Numbers and Strings
C++ has several built-in data types for representing numbers and strings:
- Integer types:
short
, int
, long
, long long
- Floating-point types:
float
, double
, long double
- Character types:
char
, wchar_t
, char16_t
, char32_t
- String type:
std::string
*signed**
and *unsigned**
:
When used with integers, signed
means that the variable can store positive and negative numbers, while unsigned
means that the variable can store only non-negative numbers. For example, an unsigned int
can store values between 0 and 4294967295, while a signed int
can store values between -2147483648 and 2147483647.
Classes and Data
A class is a user-defined data type that combines data members and member functions into a single unit. Classes are the foundation of object-oriented programming.
- Data members (also called instance variables or attributes) hold the state of an object.
- Member functions (also called methods) define the behavior of an object.
Classes and Functions
A class can have different types of member functions:
- Constructors: Special member functions that are called when an object is created. They initialize the object's data members.
- Destructor: A special member function that is called when an object is destroyed. It cleans up resources used by the object.
- Accessor functions: Member functions that provide read access to the object's data members.