Вот программа на C++, демонстрирующая поиск сведений об учениках с помощью классов:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int age;
string address;
public:
// Constructor
Student(string name, int age, string address) {
this->name = name;
this->age = age;
this->address = address;
}
// Method to display student details
void displayDetails() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Address: " << address << endl;
}
// Method to search for a student by name
bool searchByName(string searchName) {
return (name == searchName);
}
};
int main() {
// Create an array of student objects
Student students[] = {
Student("John", 20, "123 Main St"),
Student("Jane", 19, "456 Elm St"),
Student("Alice", 21, "789 Oak St")
};
// Get the search name from the user
string searchName;
cout << "Enter the name of the student you want to search: ";
getline(cin, searchName);
// Search for the student by name
bool found = false;
for (int i = 0; i < 3; i++) {
if (students[i].searchByName(searchName)) {
found = true;
students[i].displayDetails();
break;
}
}
// If student not found, display a message
if (!found) {
cout << "Student not found." << endl;
}
return 0;
}
Эта программа создает класс Student
с такими атрибутами, как имя, возраст и адрес. Он включает в себя конструктор для инициализации сведений об ученике и метод displayDetails()
для печати сведений об ученике. Кроме того, он имеет метод searchByName()
, который проверяет, соответствует ли заданное имя имени учащегося. Программа использует массив объектов Student
для хранения информации о нескольких студентах и предлагает пользователю ввести имя для поиска конкретного студента. Если студент найден, отображаются сведения о нем; в противном случае отображается сообщение «Студент не найден».