Advertisement
dllbridge

class простой пример

Nov 18th, 2022 (edited)
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1.  
  2. //   Пример, который совместим с языком Си:
  3.  
  4.  
  5. #include <stdio.h>
  6.  
  7.  
  8.  
  9.  
  10. ///////////////////////////////////////////////////////////////
  11. struct  TT                                                   //  
  12. {
  13.    
  14.     int a; 
  15.     int b; 
  16. };
  17.  
  18.  
  19.  
  20.  
  21. ///////////////////////////////////////////////////////////////
  22. int main()                                                   //
  23. {
  24.    
  25.     TT    x;
  26.        
  27.     x.a = 3;
  28.     x.b = 2;
  29.    
  30.     int S = x.a + x.b; 
  31.    
  32.     printf("S = %d\n", S);
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. /*
  46. //  Пример, который НЕсовместим с языком Си:
  47.  
  48. #include <stdio.h>
  49.  
  50.  
  51.  
  52. ///////////////////////////////////////////////////////////////
  53. class  TT                                                   //  
  54. {
  55.    
  56.     public:
  57.    
  58.     int a; 
  59.     int b;
  60.    
  61.     int summ();
  62.     int mult();
  63. };
  64.  
  65.  
  66.  
  67.  
  68. ///////////////////////////////////////////////////////////////
  69. int main()                                                   //
  70. {
  71.    
  72.    
  73.     TT  x, x1, x2, arr[31];
  74.    
  75.    
  76.     x1.a = 3;
  77.     x1.b = 2;
  78.    
  79.     int S = x1.mult();
  80.     int S1= x1.summ();     
  81.    
  82.     printf("S1 = %d\n", S1);
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. //////////////////////////////////////////////////////
  90. int TT::summ()
  91. {
  92.        
  93.     return a + b;  
  94. }
  95.    
  96. //////////////////////////////////////////////////////
  97. int TT::mult()
  98. {
  99.        
  100.     return a * b;  
  101. }
  102.  
  103.  
  104. */
  105.  
  106. //                                                     Венгерская нотация
  107. /*
  108. #include <iostream>
  109. using namespace std;
  110.  
  111. /////////////////////////////////////                                                Определение класса с префиксом C
  112. class CPerson
  113. {
  114.    
  115.     private:
  116.  
  117.       string  m_name;                                                              // Переменные-члены с префиксом m_
  118.       int     m_age;
  119.       bool    m_isEmployed;
  120.    
  121.     public:
  122.  
  123.       CPerson(string name, int age, bool isEmployed)
  124.       {
  125.           m_name       =       name;
  126.           m_age        =        age;
  127.           m_isEmployed = isEmployed;
  128.       }
  129.    
  130.  
  131.       void PrintInfo()
  132.       {
  133.           cout << "Name: "     <<  m_name                        << endl;
  134.           cout << "Age: "      <<  m_age                         << endl;
  135.           cout << "Employed: " << (m_isEmployed ? "Yes" : "No")  << endl;
  136.       }
  137. };
  138.  
  139.  
  140.  
  141. ////////////////////////////////////////////////////////////////////
  142. int main()                                                        //  
  143. {
  144.    
  145.     setlocale(LC_ALL, "rus");  
  146.                                        
  147.     CPerson person1("Иван Петров"  , 30, true );                  // Создание объектов класса с осмысленными именами
  148.     CPerson person2("Анна Сидорова", 25, false);
  149.    
  150.     person1.PrintInfo();                                          //                          Использование объектов
  151.     person2.PrintInfo();
  152.    
  153. return 0;
  154. }
  155. */
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement