You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|**Fundamental**| A basic type built into the core C++ language |`int`, `std::nullptr_t`|
1324
+
|**Compound**| A type defined in terms of other types |`int&`, `double*`, `std::string`, `Fraction`|
1325
+
|**User-defined**| A class type or enumerated type <br> (Includes those defined in the standard library or implementation) <br> (In casual use, typically used to mean program-defined types) |`std::string`, `Fraction`|
1326
+
|**Program-defined**| A class type or enumerated type <br> (Excludes those defined in standard library or implementation) | — |
1327
+
1320
1328
-`Compound data types` (also called composite data type) are data types that can be constructed from fundamental data types (or other compound data types).
1321
-
-
1322
1329
### 16.1. lvalues and rvalues
1323
1330
-`lvalues` is an expression that evaluates to an identifiable object or function (or bit-field). Can be accessed via an identifier, reference, or pointer, and typically have a lifetime longer than a single `expression` or `statement`.
1324
1331
-`rvalues` is an expression that evaluate to a value. Only exist within the scope of the expression in which they are used.
- `program-defined-types` are types that programmers create themself.
1650
+
> In C++, struct, class, and union automatically create a new type name, so you don’t need to prefix variables with the keywords struct or union as in C.
1651
+
### 18.1. Enumerations
1652
+
- `enum` is a compound types where every possible value is defined as a symbolic constant.
1653
+
- Named starting with a capital letter. Named enumerators starting with a lower case letter.
1654
+
- `unscoped-enum`: put their enumerator names into the same scope as the enumeration definition itself
1655
+
- `scoped-enum`: keep their enumerators inside the enum’s own scope.Using `enum class` keyworld.
1656
+
- `using enum <EnumName>` statement imports all the emnumerators from an enum into the current scope.
1657
+
- putting your enumerations inside a named scope region (such as a namespace or class) so the enumerators don’t pollute the global namespace.
1658
+
- Specify the base type of an enumeration only when necessary.
1659
+
- e.g.
1660
+
```cpp
1661
+
#include <iostream>
1662
+
#include <cstdint> // for uint8_t
1663
+
1664
+
// Good naming style:
1665
+
// Enum name: Capitalized
1666
+
// Enumerator names: lowercase
1667
+
1668
+
enum Color {
1669
+
red,
1670
+
green,
1671
+
blue
1672
+
};
1673
+
1674
+
// Scoped enum — enumerators are inside the enum’s scope
1675
+
enum class Shape {
1676
+
circle,
1677
+
square,
1678
+
triangle
1679
+
};
1680
+
1681
+
// Scoped enum inside a namespace — prevents name pollution
- A **struct** is a *class type* (just like `classes` or `union`), allows us to bundle multiple variables together into a single type. As such, anything that applies to class types applies to structs.
1726
+
-**Defining structs** using `struct` keywords.
1727
+
-**Access struct members**:
1728
+
- Use member selection operator(dot operator) `.` for reference/object.
1729
+
- Use arrow operator `->` for pointers. `ptr->id = (*ptr).id`
1730
+
-**Initialization**:
1731
+
- using brace-initialization `{}` or by defining default member values.
1732
+
- should provide a default value for all members
1733
+
-**Passing and returning structs**:
1734
+
- Passingy reference (efficient and avoids copying)
1735
+
- Passing temporary
1736
+
- Create a struct variable and return
1737
+
- Returning a temporary (unnamed/anonymous) object
1738
+
-**Struct size and data structure alignment**:the size of a struct will be at least as large as the size of all the variables it contains. But it could be larger! For performance reasons, the compiler will sometimes add gaps into structures this is called **padding**. We can minimize padding by defining your members in **decreasing order of size**.(e.g., double → int → char).
1739
+
1740
+
- e.g.
1741
+
```cpp
1742
+
#include<iostream>
1743
+
#include<string>
1744
+
usingnamespacestd;
1745
+
1746
+
// Define a struct
1747
+
structSensorData {
1748
+
double voltage{0.0}; // Default initialization
1749
+
int id{0};
1750
+
char status{'N'}; // 'N' = normal, 'E' = error
1751
+
string label{"Unknown"};
1752
+
1753
+
// Member function
1754
+
void print() const { // Const class objects and const member functions
- a **class template** is a template definition for instantiating class types (structs, classes, or unions). Class template argument deduction (CTAD) is a C++17 feature that allows the compiler to deduce the template type arguments from an initializer.
0 commit comments