Skip to content

Commit 377f3b9

Browse files
committed
learncpp enum, struct, class template
1 parent be98353 commit 377f3b9

File tree

1 file changed

+238
-1
lines changed

1 file changed

+238
-1
lines changed

content/posts/cpp-learn.md

Lines changed: 238 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,8 +1317,15 @@ int main()
13171317
<br>
13181318

13191319
## 16. Compound Types: References and Pointers
1320+
1321+
| Type | Meaning | Examples |
1322+
|-----------------|-------------------------------------------------------------------------------------------------------------------|---------------------------------------|
1323+
| **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+
13201328
- `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-
-
13221329
### 16.1. lvalues and rvalues
13231330
- `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`.
13241331
- `rvalues` is an expression that evaluate to a value. Only exist within the scope of the expression in which they are used.
@@ -1639,6 +1646,236 @@ https://www.learncpp.com/cpp-tutorial/type-deduction-with-pointers-references-an
16391646
https://www.learncpp.com/cpp-tutorial/stdoptional/
16401647
16411648
## 18. Compound Types: Enums and Structs
1649+
- `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
1682+
namespace Game {
1683+
enum class Direction {
1684+
up,
1685+
down,
1686+
left,
1687+
right
1688+
};
1689+
}
1690+
1691+
// Scoped enum with explicit base type
1692+
enum class Status : uint8_t {
1693+
ok = 0,
1694+
error = 1,
1695+
unknown = 2
1696+
};
1697+
1698+
int main() {
1699+
// Using unscoped enum
1700+
Color c = red; // direct access (same scope)
1701+
std::cout << "Color value: " << c << "\n";
1702+
1703+
// Using scoped enum
1704+
Shape s = Shape::circle; // must use scope name
1705+
if (s == Shape::circle)
1706+
std::cout << "Shape is circle\n";
1707+
1708+
// Scoped enum inside namespace
1709+
Game::Direction dir = Game::Direction::up;
1710+
if (dir == Game::Direction::up)
1711+
std::cout << "Direction is up\n";
1712+
1713+
// Scoped enum with base type
1714+
Status st = Status::ok;
1715+
if (st == Status::ok)
1716+
std::cout << "Status OK (base type uint8_t)\n";
1717+
1718+
return 0;
1719+
}
1720+
```
1721+
1722+
- **Union**: https://www.geeksforgeeks.org/cpp/cpp-unions/
1723+
1724+
### 18.2. Struct
1725+
- 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+
using namespace std;
1745+
1746+
// Define a struct
1747+
struct SensorData {
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
1755+
cout << "Sensor " << id
1756+
<< " [" << label << "] "
1757+
<< "Voltage: " << voltage
1758+
<< " Status: " << status << endl;
1759+
}
1760+
};
1761+
1762+
// Function that accepts struct by reference
1763+
void updateVoltage(SensorData &data, double newV) {
1764+
data.voltage = newV;
1765+
}
1766+
1767+
// Function returning a temporary struct
1768+
SensorData makeSensor(int id, double v, const string &label) {
1769+
return {v, id, 'N', label};
1770+
}
1771+
1772+
int main() {
1773+
// Initialization using braces
1774+
SensorData s1{3.3, 1, 'N', "Temperature"};
1775+
s1.print();
1776+
1777+
// Pointer access
1778+
SensorData *ptr = &s1;
1779+
ptr->status = 'E';
1780+
ptr->print();
1781+
1782+
// Passing by reference
1783+
updateVoltage(s1, 4.8);
1784+
s1.print();
1785+
1786+
// Returning temporary struct
1787+
SensorData s2 = makeSensor(2, 5.0, "Pressure");
1788+
s2.print();
1789+
1790+
cout << "Size of struct = " << sizeof(SensorData) << " bytes" << endl;
1791+
return 0;
1792+
}
1793+
```
1794+
1795+
### 18.3. Class template
1796+
- 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.
1797+
- Using class template in a function:
1798+
- e.g.
1799+
```cpp
1800+
#include <iostream>
1801+
1802+
template <typename T>
1803+
struct Pair
1804+
{
1805+
T first{};
1806+
T second{};
1807+
};
1808+
1809+
template <typename T>
1810+
constexpr T max(Pair<T> p)
1811+
{
1812+
return (p.first < p.second ? p.second : p.first);
1813+
}
1814+
1815+
int main()
1816+
{
1817+
Pair<int> p1{ 5, 6 }; // instantiates Pair<int> and creates object p1
1818+
std::cout << p1.first << ' ' << p1.second << '\n';
1819+
1820+
Pair<double> p2{ 1.2, 3.4 }; // instantiates Pair<double> and creates object p2
1821+
std::cout << p2.first << ' ' << p2.second << '\n';
1822+
1823+
Pair<double> p3{ 7.8, 9.0 }; // creates object p3 using prior definition for Pair<double>
1824+
std::cout << p3.first << ' ' << p3.second << '\n';
1825+
1826+
std::cout << max<int>(p1) << " is larger\n"; // explicit call to max<int>
1827+
1828+
return 0;
1829+
}
1830+
1831+
// Compiler ===================================================================================
1832+
#include <iostream>
1833+
1834+
// A declaration for our Pair class template
1835+
// (we don't need the definition any more since it's not used)
1836+
template <typename T>
1837+
struct Pair;
1838+
1839+
// Explicitly define what Pair<int> looks like
1840+
template <> // tells the compiler this is a template type with no template parameters
1841+
struct Pair<int>
1842+
{
1843+
int first{};
1844+
int second{};
1845+
};
1846+
1847+
// Explicitly define what Pair<double> looks like
1848+
template <> // tells the compiler this is a template type with no template parameters
1849+
struct Pair<double>
1850+
{
1851+
double first{};
1852+
double second{};
1853+
};
1854+
1855+
int main()
1856+
{
1857+
Pair<int> p1{ 5, 6 }; // instantiates Pair<int> and creates object p1
1858+
std::cout << p1.first << ' ' << p1.second << '\n';
1859+
1860+
Pair<double> p2{ 1.2, 3.4 }; // instantiates Pair<double> and creates object p2
1861+
std::cout << p2.first << ' ' << p2.second << '\n';
1862+
1863+
Pair<double> p3{ 7.8, 9.0 }; // creates object p3 using prior definition for Pair<double>
1864+
std::cout << p3.first << ' ' << p3.second << '\n';
1865+
1866+
return 0;
1867+
}
1868+
```
1869+
1870+
- **Using class templates in multiple files**:Just like function templates, class templates are typically defined in header files
1871+
1872+
- **Alias templates**: is a template that can be used to instantiate type aliases.
1873+
- e.g.
1874+
```cpp
1875+
// Alias templates must be defined in global scope
1876+
template <typename T>
1877+
using Coord = Pair<T>; // Coord is an alias for Pair<T>
1878+
```
16421879

16431880
## 19. OOP
16441881
- Classes

0 commit comments

Comments
 (0)