Advertisement
35657

Untitled

May 14th, 2024
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1.  
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9.  
  10. // Рабочий
  11. class Worker {
  12. public:
  13.  
  14.     void SetSpeciality(string speciality) {
  15.         speciality_ = speciality;
  16.     }
  17.     string GetSpeciality() const {
  18.         return speciality_;
  19.     }
  20.  
  21.     void Speak() {
  22.         cout << "Привет, я рабочий" << endl;
  23.     }
  24.  
  25. private:
  26.     string speciality_;
  27. };
  28.  
  29.  
  30. // Студент
  31. class Student {
  32. public:
  33.  
  34.     void SetFaculty(string faculty) {
  35.         faculty_ = faculty;
  36.     }
  37.     string GetFaculty() const {
  38.         return faculty_;
  39.     }
  40.  
  41.     void Speak() {
  42.         cout << "Привет, я студент" << endl;
  43.     }
  44.  
  45. private:
  46.     string faculty_;
  47. };
  48.  
  49.  
  50. //
  51. class ExternalStudent : public Worker, public Student  {
  52.  
  53. };
  54.  
  55.  
  56. int main() {
  57.     setlocale(LC_ALL, "ru");
  58.  
  59.     ExternalStudent ex;
  60.  
  61.     ex.SetSpeciality("Builder");
  62.     ex.SetFaculty("Programming");
  63.  
  64.     cout << ex.GetSpeciality() << " " << ex.GetFaculty() << endl;
  65.  
  66.     //ex.Speak(); // не скомпилируется
  67.  
  68.     ((Student)ex).Speak(); // так скомпилируется - запускаем Speak от класса Student
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement