Advertisement
RobertDeMilo

WB4.1 Структура Date

Sep 5th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5. struct Day
  6. {
  7.     int value;
  8.  
  9.     explicit Day(int new_value)
  10.     {
  11.         value = new_value;
  12.     }
  13. };
  14.  
  15. struct Month
  16. {
  17.     int value;
  18.  
  19.     explicit Month(int new_value)
  20.     {
  21.         value = new_value;
  22.     }
  23. };
  24.  
  25. struct Year
  26. {
  27.     int value;
  28.  
  29.     explicit Year(int new_value)
  30.     {
  31.         value = new_value;
  32.     }
  33. };
  34.  
  35. struct Date
  36. {
  37.     int day;
  38.     int month;
  39.     int year;
  40.  
  41.     Date(Day new_day, Month new_month, Year new_year)
  42.     {
  43.         day = new_day.value;
  44.         month = new_month.value;
  45.         year = new_year.value;
  46.     }
  47. };
  48.  
  49. void PrintDate(const Date& date)
  50. {
  51.     cout << date.day << "." << date.month << "." << date.year << "\n";
  52. }
  53.  
  54. int main()
  55. {
  56.     //Date date = { 10,11,12 };
  57.     //Date date = { Day{10}, Month{11}, Year{12} };
  58.     //Date date = { {10}, {11}, {12} };
  59.     //Date date = { 10, 11, 12 };
  60.  
  61.     Date date = { Day{10}, Month{11}, Year{12} };
  62.     Date date = { Day(10), Month(11), Year(12) };
  63.     return 0;
  64. }
  65. ***************************************************************************************************************
  66. С помощью ключевого слова explicit научились писать структуры, у которых более читаемый способ создания
  67. по сути повторяем синтаксис именованных полей
Tags: Explicit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement