Petro_zzz

14_10

Oct 14th, 2022
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void test_const() {
  5.     const int y1 = 17;
  6.     //int& refY1 = y1;
  7.     int x = 17, y = 27;
  8.     const int* pcY1;
  9.     pcY1 = &y1;
  10.     pcY1 = &x;
  11.     cout << pcY1 << " " << *pcY1 << endl;
  12.     //*pcY1 = 18;
  13.     x++;
  14.     cout << pcY1 << " " << *pcY1 << endl;
  15.     const int* pM;
  16.     pM = &x;
  17.     pM = &y;
  18.     int* const pR = &x;
  19.     *pR = 37;
  20. }
  21.  
  22. void my_swap(int*x, int* y);
  23.  
  24. void test_my_swap() {
  25.     int x = 3, y = 5;
  26.     cout << "x:" << x << " " << &x << endl;
  27.     cout << "y:" << y << " " << &y << endl;
  28.     my_swap(&x, &y);
  29.     cout << "x:" << x << " " << &x << endl;
  30.     cout << "y:" << y << " " << &y << endl;
  31. }
  32.  
  33. template <typename T1, typename T2>
  34. void my_swap2(T1& s1, T2& s2) {
  35.     T1 temp = s1;
  36.     s1 =  (T1)s2;
  37.     s2 = temp;
  38. }
  39.  
  40. template <typename T>
  41. void my_swap(T& s1, T& s2) {
  42.     T temp = s1;
  43.     s1 = s2;
  44.     s2 = temp;
  45. }
  46.  
  47. void test_string() {
  48.     string name1 = "AB";
  49.     string name2 = "AA";
  50.     if (name1 < name2) {
  51.         cout << name1 << " " << name2 << endl;
  52.     }
  53.     else {
  54.         cout << name2 << " " << name1 << endl;
  55.     }
  56. }
  57.  
  58. void test_IvanVasiliy(){
  59.     string name1 = "Ivan";
  60.     string name2 = "Vasya";
  61.     my_swap(name1, name2);
  62.     cout << name1 << endl;
  63.     cout << name2 << endl;
  64. }
  65.  
  66. struct Car {
  67.     string name;
  68.     string color;
  69.     string number;
  70.     double price;
  71.     double volume;
  72.  
  73.     void show() {
  74.         cout << color << " " << name << " " << price;
  75.     }
  76. };
  77.  
  78. struct Cars {
  79.     Car* cars;
  80.     int num_cars = 0;
  81.  
  82.     void add(Car car) {
  83.         Car* buff = new Car[num_cars + 1];
  84.         for (int k = 0; k < num_cars; ++k) {
  85.             buff[k] = cars[k];
  86.         }
  87.         buff[num_cars] = car;
  88.         if (num_cars > 0)
  89.             delete[] cars;
  90.         cars = buff;
  91.         num_cars++;
  92.     }
  93.  
  94.     void show() {
  95.         for (int k = 0; k < num_cars; ++k) {
  96.             cars[k].show();
  97.             cout << endl;
  98.         }
  99.     }
  100. };
  101.  
  102. void main() {
  103.     Cars cars;
  104.     Car car{ "BMV", "Red", "a777po 161", 2.7, 4.4 };
  105.     cars.add(car);
  106.  
  107.     car.name = "VAZ";
  108.     car.color = "White";
  109.     car.number = "x132py 161";
  110.     car.price = 0.7;
  111.     car.volume = 1.6;  
  112.     cars.add(car);
  113.  
  114.     car.name = "VAZ";
  115.     car.color = "White";
  116.     car.number = "T132py 161";
  117.     car.price = 0.7;
  118.     car.volume = 1.6;
  119.     cars.add(car);
  120.  
  121.     cars.show();
  122. }
  123.  
  124. void my_swap(int* x, int* y) {
  125.     int tmp = *x;
  126.     cout << "x:" << x << endl;
  127.     cout << "y:" << y << endl;
  128.     *x = *y;
  129.     *y = tmp;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment