+++++Абстракция данных
+++ Создайте класс с именем Chair. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе
Download 57.24 Kb.
|
dasturlash
+++ Создайте класс с именем Chair. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе
#include #include class Chair { private: std::string color; int height; public: void setColor(std::string chairColor) { color = chairColor; }std::string getColor() { return color; } void setHeight(int chairHeight) { height = chairHeight; } int getHeight() { return height; } }; int main() { Chair chair1; chair1.setColor("Red"); chair1.setHeight(100); std::cout << "Цвет стула: " << chair1.getColor() << std::endl; std::cout << "Высота стула: " << chair1.getHeight() << std::endl; return 0; } +++ Создайте класс с именем Chair. Пусть будет 2 метода, 5 свойств, 2 конструктора и 1 деструктор #include #include class Chair { private: std::string color; int height; bool hasBack; bool isAdjustable; int weight; public: // Конструктор по умолчанию Chair() { color = ""; height = 0; hasBack = false; isAdjustable = false; weight = 0; std::cout << "Вызван конструктор по умолчанию" << std::endl; } // Конструктор с параметрами Chair(std::string chairColor, int chairHeight, bool chairHasBack, bool chairIsAdjustable, int chairWeight) { color = chairColor; height = chairHeight; hasBack = chairHasBack; isAdjustable = chairIsAdjustable; weight = chairWeight; std::cout << "Вызван конструктор с параметрами" << std::endl; } // Деструктор ~Chair() { std::cout << "Вызван деструктор" << std::endl; } void setColor(std::string chairColor) { color = chairColor; } std::string getColor() { return color; } void setHeight(int chairHeight) { height = chairHeight; } int getHeight() { return height; } void setHasBack(bool chairHasBack) { hasBack = chairHasBack; } bool getHasBack() { return hasBack; } void setIsAdjustable(bool chairIsAdjustable) { isAdjustable = chairIsAdjustable; } bool getIsAdjustable() { return isAdjustable; } void setWeight(int chairWeight) { weight = chairWeight; } int getWeight() { return weight; } }; int main() { Chair chair1; // Использование конструктора по умолчанию chair1.setColor("Red"); chair1.setHeight(100); chair1.setHasBack(true); chair1.setIsAdjustable(true); chair1.setWeight(10); std::cout << "Цвет стула: " << chair1.getColor() << std::endl; std::cout << "Высота стула: " << chair1.getHeight() << std::endl; std::cout << "Стул имеет спинку: " << (chair1.getHasBack() ? "Да" : "Нет") << std::endl; std::cout << "Стул регулируемый: " << (chair1.getIsAdjustable() ? "Да" : "Нет") << std::endl; std::cout << "Вес стула: " << chair1.getWeight() << std::endl; std::cout << std::endl; Chair chair2("Blue", 90, false, false, 8); // Использование конструктора с параметрами std::cout << "Цвет стула: " << chair2.getColor() << std::endl; std::cout << "Высота стула: " << chair2.getHeight() << std::endl; std::cout << "Стул имеет спинку: " << (chair2.getHasBack() ? "Да" : "Нет") << std::endl; std::cout << "Стул регулируемый: " << (chair2.getIsAdjustable() ? "Да" : "Нет") << std::endl; std::cout << "Вес стула: " << chair2.getWeight() << std::endl; return 0; } +++Создайте класс с именем Chair. Пусть будет три метода и три свойства. Отправьте название отдела через метод getKafedra() #include #include class Chair { private: std::string color; int height; std::string kafedra; public: void setColor(std::string chairColor) { color = chairColor; } std::string getColor() { return color; } void setHeight(int chairHeight) { height = chairHeight; } int getHeight() { return height; } void setKafedra(std::string chairKafedra) { kafedra = chairKafedra; } std::string getKafedra() { return kafedra; } }; int main() { Chair chair1; chair1.setColor("Red"); chair1.setHeight(100); chair1.setKafedra("Mathematics"); std::cout << "Цвет стула: " << chair1.getColor() << std::endl; std::cout << "Высота стула: " << chair1.getHeight() << std::endl; std::cout << "Отдел: " << chair1.getKafedra() << std::endl; return 0; } ++++Создайте класс с именем Chair. Пусть он содержит два разных конструктора #include #include class Chair { private: std::string color; int height; public: Chair(std::string chairColor, int chairHeight) { color = chairColor; height = chairHeight; } Chair() { color = "Black"; height = 0; } void setColor(std::string chairColor) { color = chairColor; } std::string getColor() { return color; } void setHeight(int chairHeight) { height = chairHeight; } int getHeight() { return height; } }; int main() { Chair chair1("Red", 100); +++ Создайте класс с именем City. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе #include using namespace std; class City { public: City(string name, int population) { this->name = name; this->set_population(population); } int get_population() { return population; } void set_population(int population) { this->population = population; } float calculate_density(float area) { return population / area; } private: string name; int population; }; int main() { City city("Москва", 12692466); cout << city.get_population() << endl; // Выводит 12692466 city.set_population(13000000); cout << city.get_population() << endl; // Выводит 13000000 cout << city.calculate_density(2511) << endl; return 0; } ++++Создайте класс с именем Country. Пусть этот класс содержит 5 свойства. Используйте эти свойства через 2 объекта. #include using namespace std; class Country { public: string name; string capital; int population; float area; string language; }; int main() { // Создание первого объекта Country country1; country1.name = "Россия"; country1.capital = "Москва"; country1.population = 144500000; country1.area = 17098246; country1.language = "русский"; // Создание второго объекта Country country2; country2.name = "Франция"; country2.capital = "Париж"; country2.population = 66990000; country2.area = 551695; country2.language = "французский"; // Использование свойств объектов cout << "Название страны: " << country1.name << endl; cout << "Столица: " << country1.capital << endl; cout << "Население: " << country1.population << endl; cout << "Площадь: " << country1.area << " км²" << endl; cout << "Основной язык: " << country1.language << endl; cout << "--------------------------------------" << endl; cout << "Название страны: " << country2.name << endl; cout << "Столица: " << country2.capital << endl; cout << "Население: " << country2.population << endl; cout << "Площадь: " << country2.area << " км²" << endl; cout << "Основной язык: " << country2.language << endl; return 0; }++++Создайте класс с именем Director и создайте для него дружественную функцию showInfo. Пусть Class состоит из 4 полей и пусть его данные выводятся с помощью функции showInfo. #include using namespace std; class Director { private: string name; string company; int age; float salary; public: Director(string name, string company, int age, float salary) { this->name = name; this->company = company; this->age = age; this->salary = salary; } friend void showInfo(Director director); }; void showInfo(Director director) { cout << "Имя: " << director.name << endl; cout << "Компания: " << director.company << endl; cout << "Возраст: " << director.age << " лет" << endl; cout << "Зарплата: " << director.salary << " $" << endl; } int main() { Director director("Иванов Иван", "ООО Рога и Копыта", 45, 5000); showInfo(director); return 0; } +++Создайте класс с именем Университет. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе #include #include class University { private: std::string name; int numStudents; public: University(std::string universityName, int num) { name = universityName; numStudents = num; } void displayInfo() { std::cout << "University Name: " << name << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; } void setName(std::string universityName) { name = universityName; } std::string getName() { return name; } void setNumStudents(int num) { numStudents = num; } int getNumStudents() { return numStudents; } }; int main() { University university("Harvard University", 10000); university.displayInfo(); university.setName("Stanford University"); university.setNumStudents(15000); std::cout << "New University Name: " << university.getName() << std::endl; std::cout << "New Number of Students: " << university.getNumStudents() << std::endl; return 0; } +++Создайте класс с именем Университет. Пусть будет три метода и три свойства. Отправьте адрес университета с помощью метода getAddress() #include #include class University { private: std::string name; int numStudents; std::string address; public: University(std::string universityName, int num, std::string universityAddress) { name = universityName; numStudents = num; address = universityAddress; } void displayInfo() { std::cout << "University Name: " << name << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; std::cout << "University Address: " << address << std::endl; { void setName(std::string universityName) { name = universityName; } std::string getName() { return name; } void setNumStudents(int num) { numStudents = num; } int getNumStudents() { return numStudents; } void setAddress(std::string universityAddress) { address = universityAddress; } std::string getAddress() { return address; } }; int main() { University university("Harvard University", 10000, "123 Main St"); university.displayInfo(); university.setName("Stanford University"); university.setNumStudents(15000); university.setAddress("456 Elm St"); std::cout << "New University Name: " << university.getName() << std::endl; std::cout << "New Number of Students: " << university.getNumStudents() << std::endl; std::cout << "New University Address: " << university.getAddress() << std::endl return 0; } +++Создайте класс с именем Университет. Пусть он содержит два разных конструктора #include #include class University { private: std::string name; int numStudents; std::string address; public: University() { name = ""; numStudents = 0; address = ""; } University(std::string universityName, int num, std::string universityAddress) { name = universityName; numStudents = num; address = universityAddress; } void displayInfo() { std::cout << "University Name: " << name << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; std::cout << "University Address: " << address << std::endl; } }; int main() { University university1; university1.displayInfo(); University university2("Harvard University", 10000, "123 Main St"); university2.displayInfo(); return 0; }+++Создайте класс с именем факультет. Имеют 2 метода и 5 свойств. Отправьте название факультета с помощью метода getName() #include #include class Faculty { private: std::string name; int numProfessors; int numStudents; std::string dean; std::string address; public: Faculty(std::string facultyName, int professors, int students, std::string facultyDean, std::string facultyAddress) { name = facultyName; numProfessors = professors; numStudents = students; dean = facultyDean; address = facultyAddress; } std::string getName() { return name; } void displayInfo() { std::cout << "Faculty Name: " << name << std::endl; std::cout << "Number of Professors: " << numProfessors << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; std::cout << "Dean: " << dean << std::endl; std::cout << "Faculty Address: " << address << std::endl; } }; int main() { Faculty faculty("Computer Science", 10, 200, "John Smith", "456 Main St"); std::string facultyName = faculty.getName(); std::cout << "Faculty Name: " << facultyName << std::endl; faculty.displayInfo(); return 0; } +++Создайте класс с именем факультет. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе #include #include class Faculty { private: std::string name; int numProfessors; int numStudents; std::string dean; std::string address; public: Faculty(std::string facultyName, int professors, int students, std::string facultyDean, std::string facultyAddress) { name = facultyName; numProfessors = professors; numStudents = students; dean = facultyDean; address = facultyAddress; } std::string getName() { return name; } void displayInfo() { std::cout << "Faculty Name: " << name << std::endl; std::cout << "Number of Professors: " << numProfessors << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; std::cout << "Dean: " << dean << std::endl; std::cout << "Faculty Address: " << address << std::endl; } }; int main() { Faculty faculty("Computer Science", 10, 200, "John Smith", "456 Main St"); std::string facultyName = faculty.getName(); std::cout << "Faculty Name: " << facultyName << std::endl; faculty.displayInfo(); return 0; } +++Создайте класс с именем факультет. Пусть он содержит два разных конструктора #include #include class Faculty { private: std::string name; int numProfessors; int numStudents; std::string dean; std::string address; public: Faculty(std::string facultyName, int professors, int students, std::string facultyDean, std::string facultyAddress) { name = facultyName; numProfessors = professors; numStudents = students; dean = facultyDean; address = facultyAddress; } Faculty() { name = ""; numProfessors = 0; numStudents = 0; dean = ""; address = ""; } std::string getName() { return name; } void displayInfo() { std::cout << "Faculty Name: " << name << std::endl; std::cout << "Number of Professors: " << numProfessors << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; std::cout << "Dean: " << dean << std::endl; std::cout << "Faculty Address: " << address << std::endl; } }; int main() { Faculty faculty1("Computer Science", 10, 200, "John Smith", "456 Main St"); std::string facultyName1 = faculty1.getName(); std::cout << "Faculty Name: " << facultyName1 << std::endl; faculty1.displayInfo(); Faculty faculty2; std::string facultyName2 = faculty2.getName(); std::cout << "Faculty Name: " << facultyName2 << std::endl; displayInfo() faculty2.displayInfo(); return 0; } +++Создайте класс с именем факультет. Пусть он содержит два разных конструктора. Используйте указатель this внутри класса #include #include class Faculty { private: std::string name; int numProfessors; int numStudents; std::string dean; std::string address; public: Faculty(std::string name, int numProfessors, int numStudents, std::string dean, std::string address) { this->name = name; this->numProfessors = numProfessors; this->numStudents = numStudents; this->dean = dean; this->address = address; } Faculty() { this->name = ""; this->numProfessors = 0; this->numStudents = 0; this->dean = ""; this->address = ""; } std::string getName() { return this->name } void displayInfo() { std::cout << "Faculty Name: " << this->name << std::endl; std::cout << "Number of Professors: " << this->numProfessors << std::endl; std::cout << "Number of Students: " << this->numStudents << std::endl; std::cout << "Dean: " << this->dean << std::endl; std::cout << "Faculty Address: " << this->address << std::endl; } }; int main() { Faculty faculty1("Computer Science", 10, 200, "John Smith", "456 Main St"); std::string facultyName1 = faculty1.getName(); std::cout << "Faculty Name: " << facultyName1 << std::endl; faculty1.displayInfo(); Faculty faculty2; std::string facultyName2 = faculty2.getName(); std::cout << "Faculty Name: " << facultyName2 << std::endl; faculty2.displayInfo() return 0; } ++ Создайте класс с именем Школа. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе #include #include class School { private: std::string name; int numTeachers; int numStudents; std::string principal; std::string address; public: School(std::string name, int numTeachers, int numStudents, std::string principal, std::string address) { this->name = name; this->numTeachers = numTeachers; this->numStudents = numStudents; this->principal = principal; this->address = address; } std::string getName() { return this->name; } void displayInfo() { std::cout << "School Name: " << this->name << std::endl; std::cout << "Number of Teachers: " << this->numTeachers << std::endl; std::cout << "Number of Students: " << this->numStudents << std::endl; std::cout << "Principal: " << this->principal << std::endl; std::cout << "School Address: " << this->address << std::endl; } }; int main() { School school1("ABC School", 50, 500, "Jane Doe", "123 Main St"); std::string schoolName1 = school1.getName(); std::cout << "School Name: " << schoolName1 << std::endl; school1.displayInfo(); School school2; std::string schoolName2 = school2.getName(); std::cout << "School Name: " << schoolName2 << std::endl; school2.displayInfo(); return 0; } +++ Создайте класс с именем Школа. Пусть будет 2 метода, 5 свойств, 2 конструктора и 1 деструктор #include #include class School { private: std::string name; int numTeachers; int numStudents; std::string principal; std::string address; public: School() { name = ""; numTeachers = 0; numStudents = 0; principal = ""; address = ""; } School(std::string name, int numTeachers, int numStudents, std::string principal, std::string address) { this->name = name; this->numTeachers = numTeachers; this->numStudents = numStudents; this->principal = principal; this->address = address; } ~School() { std::cout << "School object destroyed" << std::endl; } std::string getName() { return name; } int getNumTeachers() { return numTeachers; } int getNumStudents() { return numStudents; } std::string getPrincipal() { return principal; } std::string getAddress() { return address; } void displayInfo() { std::cout << "School Name: " << name << std::endl; std::cout << "Number of Teachers: " << numTeachers << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; std::cout << "Principal: " << principal << std::endl; std::cout << "School Address: " << address << std::endl; } }; int main() { School school1("ABC School", 50, 500, "Jane Doe", "123 Main St"); std::string schoolName1 = school1.getName(); int numTeachers1 = school1.getNumTeachers(); int numStudents1 = school1.getNumStudents(); std::string principal1 = school1.getPrincipal(); std::string address1 = school1.getAddress(); std::cout << "School Name: " << schoolName1 << std::endl; std::cout << "Number of Teachers: " << numTeachers1 << std::endl; std::cout << "Number of Students: " << numStudents1 << std::endl; std::cout << "Principal: " << principal1 << std::endl; std::cout << "School Address: " << address1 << std::endl; school1.displayInfo(); School school2; std::string schoolName2 = school2.getName(); int numTeachers2 = school2.getNumTeachers(); int numStudents2 = school2.getNumStudents(); std::string principal2 = school2.getPrincipal(); std::string address2 = school2.getAddress(); std::cout << "School Name: " << schoolName2 << std::endl; std::cout << "Number of Teachers: " << numTeachers2 << std::endl; std::cout << "Number of Students: " << numStudents2 << std::endl; std::cout << "Principal: " << principal2 << std::endl; std::cout << "School Address: " << address2 << std::endl; school2.displayInfo(); return 0; } ++ Создайте класс с именем Школа. Пусть он содержит два метода и два свойства. . Отправьте номер студента с помощью метода getNumber() #include #include class School { private: std::string name; int numStudents; public: School() { name = ""; numStudents = 0; } School(std::string name, int numStudents) { this->name = name; this->numStudents = numStudents; } std::string getName() { return name; } int getNumStudents() { return numStudents; } int getNumber() { return numStudents; } }; int main() { School school("ABC School", 500); std::string schoolName = school.getName(); int numStudents = school.getNumStudents(); std::cout << "School Name: " << schoolName << std::endl; std::cout << "Number of Students: " << numStudents << std::endl; int studentNumber = school.getNumber(); std::cout << "Student Number: " << studentNumber << std::endl; return 0; } +++ Создайте класс с именем Школа. Пусть он содержит два разных конструктора #include #include class School { private: std::string name; int numStudents; public: School() { name = ""; numStudents = 0; } School(std::string name, int numStudents) { this->name = name; this->numStudents = numStudents; } std::string getName() { return name; } int getNumStudents() { return numStudents; } }; int main() { School school1; std::string schoolName1 = school1.getName(); int numStudents1 = school1.getNumStudents(); std::cout << "School Name 1: " << schoolName1 << std::endl; std::cout << "Number of Students 1: " << numStudents1 << std::endl; School school2("ABC School", 500); std::string schoolName2 = school2.getName(); int numStudents2 = school2.getNumStudents(); std::cout << "School Name 2: " << schoolName2 << std::endl; std::cout << "Number of Students 2: " << numStudents2 << std::endl; return 0; } +++ Создайте класс с именем Fakultet. Пусть этот класс содержит 5 свойства. Используйте эти свойства через 2 объекта #include using namespace std; class Fakultet { public: string name; string dean; int numOfStudents; int numOfProfessors; string address; }; int main() { // Создание первого объекта Fakultet fakultet1; fakultet1.name = "Факультет информационных технологий"; fakultet1.dean = "Иванов Иван Иванович"; fakultet1.numOfStudents = 500; fakultet1.numOfProfessors = 50; fakultet1.address = "Москва, ул. Ленина, д. 10"; // Создание второго объекта Fakultet fakultet2; fakultet2.name = "Факультет экономики и управления"; fakultet2.dean = "Петров Петр Петрович"; fakultet2.numOfStudents = 400; fakultet2.numOfProfessors = 40; fakultet2.address = "Санкт-Петербург, пр. Невский, д. 20"; // Использование свойств объектов cout << "Название факультета: " << fakultet1.name << endl; cout << "Декан: " << fakultet1.dean << endl; cout << "Количество студентов: " << fakultet1.numOfStudents << endl; cout << "Количество профессоров: " << fakultet1.numOfProfessors << endl; cout << "Адрес: " << fakultet1.address << endl; cout << "--------------------------------------" << endl; cout << "Название факультета: " << fakultet2.name << endl; cout << "Декан: " << fakultet2.dean << endl; cout << "Количество студентов: " << fakultet2.numOfStudents << endl; cout << "Количество профессоров: " << fakultet2.numOfProfessors << endl; cout << "Адрес: " << fakultet2.address << endl; return 0; } +++ Создайте класс с именем Football_Club. Пусть этот класс содержит 5 свойства. . Используйте эти свойства через 3 объекта. #include #include using namespace std; class Football_Club { public: string name; string country; int foundedYear; string coach; int numberOfPlayers; }; int main() { // Создание первого объекта Football_Club club1; club1.name = "FC Barcelona"; club1.country = "Spain"; club1.foundedYear = 1899; club1.coach = "Ronald Koeman"; club1.numberOfPlayers = 28; // Создание второго объекта Football_Club club2; club2.name = "Real Madrid"; club2.country = "Spain"; club2.foundedYear = 1902; club2.coach = "Carlo Ancelotti"; club2.numberOfPlayers = 26; // Создание третьего объекта Football_Club club3; club3.name = "FC Bayern Munich"; club3.country = "Germany"; club3.foundedYear = 1900; club3.coach = "Julian Nagelsmann"; club3.numberOfPlayers = 27; // Использование свойств объектов cout << "Название клуба: " << club1.name << endl; cout << "Страна: " << club1.country << endl; cout << "Год основания: " << club1.foundedYear << endl; cout << "Тренер: " << club1.coach << endl; cout << "Количество игроков: " << club1.numberOfPlayers << endl; cout << "--------------------------------------" << endl; cout << "Название клуба: " << club2.name << endl; cout << "Страна: " << club2.country << endl; cout << "Год основания: " << club2.foundedYear << endl; cout << "Тренер: " << club2.coach << endl; cout << "Количество игроков: " << club2.numberOfPlayers << endl; cout << "--------------------------------------" << endl; cout << "Название клуба: " << club3.name << endl; cout << "Страна: " << club3.country << endl; cout << "Год основания: " << club3.foundedYear << endl; cout << "Тренер: " << club3.coach << endl; cout << "Количество игроков: " << club3.numberOfPlayers << endl; return 0; }. +++Создайте класс с именем Football_team. Пусть этот класс содержит 5 свойства. Используйте эти свойства через 3 объекта #include #include class Footballteam { public: std::string name; std::string country; int foundedyear; int stadiumcapacity; int numberofplayers; }; int main() { // Создание первого объекта Footballteam team1; team1.name = "Real Madrid"; team1.country = "Spain"; team1.foundedyear = 1902; team1.stadiumcapacity = 81044; team1.numberofplayers = 26; // Создание второго объекта Footballteam team2; team2.name = "Barcelona"; team2.country = "Spain"; team2.foundedyear = 1899; team2.stadiumcapacity = 99354; team2.numberofplayers = 25; // Создание третьего объекта Footballteam team3; team3.name = "Manchester United"; team3.country = "England"; team3.foundedyear = 1878; team3.stadiumcapacity = 74319; team3.numberofplayers = 28; // Вывод информации о каждой команде std::cout << "Team 1:" << std::endl; std::cout << "Name: " << team1.name << std::endl; std::cout << "Country: " << team1.country << std::endl; std::cout << "Founded Year: " << team1.foundedyear << std::endl; std::cout << "Stadium Capacity: " << team1.stadiumcapacity << std::endl; std::cout << "Number of Players: " << team1.numberofplayers << std::endl; std::cout << "\nTeam 2:" << std::endl; std::cout << "Name: " << team2.name << std::endl; std::cout << "Country: " << team2.country << std::endl; std::cout << "Founded Year: " << team2.foundedyear << std::endl; std::cout << "Stadium Capacity: " << team2.stadiumcapacity << std::endl; std::cout << "Number of Players: " << team2.numberofplayers << std::endl; std::cout << "\nTeam 3:" << std::endl; std::cout << "Name: " << team3.name << std::endl; std::cout << "Country: " << team3.country << std::endl; std::cout << "Founded Year: " << team3.foundedyear << std::endl; std::cout << "Stadium Capacity: " << team3.stadiumcapacity << std::endl; std::cout << "Number of Players: " << team3.numberofplayers << std::endl; return 0; } +++ Создайте класс с именем GeometricShape. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе #include class GeometricShape { private: int width; int height; public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } int getArea() { return width * height; } }; int main() { GeometricShape shape; shape.setWidth(5); shape.setHeight(4); int area = shape.getArea(); std::cout << "Area: " << area << std::endl; return 0; } ++ Создайте класс с именем Library. Имеют 2 метода и 5 свойств. Отправка лидера библиотеки с помощью метода getLeader() #include #include class Library { private: std::string name; std::string location; int yearFounded; int numberOfBooks; std::string leader; public: Library(std::string n, std::string loc, int year, int numBooks, std::string lead) { name = n; location = loc; yearFounded = year; numberOfBooks = numBooks; leader = lead; } void setLeader(std::string lead) { leader = lead; } std::string getLeader() { return leader; } }; int main() { Library library("Central Library", "New York", 1854, 50000, "John Smith"); library.setLeader("Jane Johnson"); std::cout << "Leader of the library: " << library.getLeader() << std::endl; return 0; } ++ Создайте класс с именем Library. Объявляйте методы и свойства, используя спецификаторы public и private. Используйте их в основной программе #include #include class Library { private: std::string name; std::string location; int yearFounded; int numberOfBooks; std::string leader; public: void setName(std::string n) { name = n; } void setLocation(std::string loc) { location = loc; } void setYearFounded(int year) { yearFounded = year; } void setNumberOfBooks(int numBooks) { numberOfBooks = numBooks; } void setLeader(std::string lead) { leader = lead; } std::string getName() { return name; } std::string getLocation() { return location; } int getYearFounded() { return yearFounded; } int getNumberOfBooks() { return numberOfBooks; } std::string getLeader() { return leader; } }; int main() { Library library; library.setName("Central Library"); library.setLocation("New York"); library.setYearFounded(1854); library.setNumberOfBooks(50000); library.setLeader("John Smith"); std::cout << "Library Name: " << library.getName() << std::endl; std::cout << "Location: " << library.getLocation() << std::endl; std::cout << "Year Founded: " << library.getYearFounded() << std::endl; std::cout << "Number of Books: " << library.getNumberOfBooks() << std::endl; std::cout << "Leader: " << library.getLeader() << std::endl; return 0; } Download 57.24 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling