21просмотров
8 декабря 2023 г.
Score: 23
//ООП
//Модификатор доступа:'protected'
//Наследрвание
#include <iostream>
#include <string>
using namespace std;
class Human{ //С помощью protected можно использовать типы данных в других унаследованных классах.
protected: string lastName; string name; int age; public: void SetLastName(const string lastName){this->lastName=lastName;} string GetLastName(){return this->lastName;} void SetName(const string name){this->name=name;} string GetName(){return this->name;} void SetAge(const int age){this->age=age;} int GetAge(){return this->age;} };
//Чтобы унаследоваться от другого класса, необходимо указать новому классу, от кого он будет наследоваться. Это делается с помощью ключевого слова "class", за которым следует название нового класса, затем двоеточие, модификатор доступа и имя унаследованного класса.
class Job : public Human
{
protected: int workExperience; int salary; string education;
public: void SetWorkExperience(const int workExperience){this->workExperience=workExperience;} int GetWorkExperience(){return this->workExperience;} void SetSalary(const int salary){this->salary=salary;} int GetSalary(){return this->salary;} void SetEducation(const string education){this->education=education;} string GetEducation(){return this->education;}
};
class Teacher:public Job{
private: int TeamLeader;
public: void SetTeamLeader(const int TeamLeader){this->TeamLeader=TeamLeader;} int GetTeamLeader(){return this->TeamLeader;} void printInfoHuman() { cout<<"Фамили: "<<lastName<<endl; cout<<"Имя: "<<name<<endl; cout<<"Год: "<<age<<endl; cout<<"Опыт работы: "<<workExperience<<endl; cout<<"Зарплата: "<<salary<<endl; cout<<"Образование: "<<education<<endl; cout<<"Номер группы: "<<TeamLeader<<endl; }
};
class Programmer:public Job
{
private: string subgroupOfProgrammers; string programmerLevel;
public: void SetSubgroupOfProgrammers(const string subgroupOfProgrammers){this->subgroupOfProgrammers=subgroupOfProgrammers;} string GetSubgroupOfProgrammers(){return this->subgroupOfProgrammers;} void SetProgrammerLevel(const string programmerLevel){this->programmerLevel=programmerLevel;} string GetProgrammerLevel(){return this->programmerLevel;}
}; int main(){ Programmer programmer; Teacher teacher; programmer.SetLastName("Иванов"); programmer.SetSalary(200000); cout<<programmer.GetLastName()<<endl; cout<<programmer.GetSalary()<<endl; teacher.SetAge(30); teacher.SetName("Николай"); teacher.SetLastName("Неиванов"); teacher.SetSalary(100000); teacher.SetTeamLeader(2000345); teacher.SetEducation("МГУ"); teacher.SetWorkExperience(6); teacher.printInfoHuman();
}
//Смотрите файл "class_3.cpp" для объяснения каждой строки кода.
#ооп #наследование #модификатор_доступа #protected