Advertisement
JmihPodvalbniy

Untitled

Dec 19th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | Software | 0 0
  1. // дз от 18.12.2024г.
  2.  
  3. // 1)
  4. #include <iostream>
  5.  
  6. class Employee {
  7. private:
  8.  std::string name;
  9.  double salary;
  10.  
  11. public:
  12.  Employee(std::string name, double salary) : name(name), salary(salary) {}
  13.  
  14.  void displayInfo() const {
  15.  std::cout << "Имя сотрудника: " << name << ", Зарплата: " << salary << std::endl;
  16.  }
  17. };
  18.  
  19. class Manager : public Employee {
  20. private:
  21.  double bonus;
  22.  
  23. public:
  24.  Manager(std::string name, double salary, double bonus) : Employee(name, salary), bonus(bonus) {}
  25.  
  26.  void displayInfo() const {
  27.  Employee::displayInfo();
  28.  std::cout << "Бонус менеджера: " << bonus << std::endl;
  29.  }
  30. };
  31.  
  32. class Developer : public Employee {
  33. private:
  34.  int linesOfCode;
  35.  
  36. public:
  37.  Developer(std::string name, double salary, int linesOfCode) : Employee(name, salary), linesOfCode(linesOfCode) {}
  38.  
  39.  void displayInfo() const {
  40.  Employee::displayInfo();
  41.  std::cout << "Количество строк кода: " << linesOfCode << std::endl;
  42.  }
  43. };
  44.  
  45. int main() {
  46.  Employee emp("John Doe", 5000.0);
  47.  emp.displayInfo();
  48.  
  49.  Manager mgr("Jane Smith", 6000.0, 1000.0);
  50.  mgr.displayInfo();
  51.  
  52.  Developer dev("Mike Brown", 7000.0, 10000);
  53.  dev.displayInfo();
  54.  
  55.  return 0;
  56. }
  57.  
  58. // 2)
  59. #include <iostream>
  60.  
  61. class Student {
  62. private:
  63.  std::string name;
  64.  int age;
  65.  static int studentCount;
  66.  
  67. public:
  68.  Student(std::string name, int age) : name(name), age(age), studentCount(++studentCount) {}
  69.  
  70.  ~Student() {
  71.  studentCount--;
  72.  }
  73.  
  74.  static int getStudentCount() {
  75.  return studentCount;
  76.  }
  77.  
  78.  void displayInfo() const {
  79.  std::cout << "Имя студента: " << name << ", Возраст: " << age << std::endl;
  80.  }
  81. };
  82.  
  83. int Student::studentCount = 0;
  84.  
  85. int main() {
  86.  Student s1("Alice", 20);
  87.  Student s2("Bob", 21);
  88.  Student s3("Charlie", 22);
  89.  
  90.  std::cout << "Количество созданных объектов: " << Student::getStudentCount() << std::endl;
  91.  
  92.  s1.displayInfo();
  93.  s2.displayInfo();
  94.  s3.displayInfo();
  95.  
  96.  // Удаляем один объект
  97.  delete s2;
  98.  
  99.  std::cout << "Количество объектов после удаления: " << Student::getStudentCount() << std::endl;
  100.  
  101.  return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement