#include <iostream>
#include <string>
class Student {
private:
std::string name;
int age;
float score;
public:
Student(const std::string& n, int a, float s) {
name = n;
age = a;
score = s;
std::cout << "Constructor called, student " << name << " has been created." << std::endl;
}
~Student() {
std::cout << "Destructor called, student " << name << " is about to be destroyed." << std::endl;
}
void setName(const std::string& n) {
name = n;
}
void setAge(int a) {
if (a > 0) age = a;
}
void setScore(float s) {
if (s >= 0 && s <= 100) score = s;
}
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
float getScore() const {
return score;
}
void display() const {
std::cout << "Name: " << name << ", Age: " << age << ", Score: " << score << std::endl;
}
bool isPassed() const {
return score >= 60;
}
};
int main() {
Student stu1("Zhang San", 20, 85.5);
stu1.display();
Student stu2("Li Si", 19, 58.0);
stu2.display();
if (stu2.isPassed()) {
std::cout << stu2.getName() << " passed." << std::endl;
} else {
std::cout << stu2.getName() << " failed." << std::endl;
}
stu2.setScore(75.5);
stu2.setName("Li Si (after retake)");
std::cout << "After modification ";
stu2.display();
Student* stu3 = new Student("Wang Wu", 22, 90.0);
stu3->display();
delete stu3;
return 0;
}