#include <iostream>
#include <string>
#include <vector>
using namespace std;
class CollegeStudent {
private:
string name;
int age;
string major;
double gpa;
vector<string> courses;
public:
// Constructors
CollegeStudent() : name("Unknown"), age(0), major("Undeclared"), gpa(0.0) {}
CollegeStudent(string studentName, int studentAge, string studentMajor, double studentGPA)
: name(studentName), age(studentAge), major(studentMajor), gpa(studentGPA) {}
// Member functions
void enrollCourse(const string &course) {
courses.push_back(course);
cout << name << " has enrolled in " << course << "." << endl;
}
void updateGPA(double newGPA) {
gpa = newGPA;
cout << name << "'s GPA has been updated to " << gpa << "." << endl;
}
void displayStudentInfo() const {
cout << "Student Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Major: " << major << endl;
cout << "GPA: " << gpa << endl;
cout << "Enrolled Courses: ";
for (const auto &course : courses) {
cout << course << " ";
}
cout << endl;
}
// Accessor and Mutator functions
string getName() const { return name; }
void setName(const string &newName) { name = newName; }
int getAge() const { return age; }
void setAge(int newAge) { age = newAge; }
string getMajor() const { return major; }
void setMajor(const string &newMajor) { major = newMajor; }
double getGPA() const { return gpa; }
void setGPA(double newGPA) { gpa = newGPA; }
};
int main() {
// Create instances of CollegeStudent
CollegeStudent student1("Alice Smith", 20, "Computer Science", 3.8);
CollegeStudent student2("Bob Johnson", 22, "Mathematics", 3.6);
// Enroll courses
student1.enrollCourse("Data Structures");
student1.enrollCourse("Algorithms");
student2.enrollCourse("Calculus");
student2.enrollCourse("Linear Algebra");
// Update GPA
student1.updateGPA(3.9);
student2.updateGPA(3.7);
// Display student information
cout << "\nStudent 1 Information:" << endl;
student1.displayStudentInfo();
cout << "\nStudent 2 Information:" << endl;
student2.displayStudentInfo();
return 0;
}
Explanation
- Attributes:
name,age,major,gpa, andcoursesrepresent the characteristics of a college student.
- Constructors:
- Default constructor initializes attributes to default values.
- Parameterized constructor allows initialization with specific values.
- Member Functions:
enrollCourse: Adds a course to the student’s list of courses.updateGPA: Updates the student’s GPA.displayStudentInfo: Outputs all information about the student.
- Accessors and Mutators:
getName,getAge,getMajor,getGPAretrieve the values of attributes.setName,setAge,setMajor,setGPAmodify the attributes.
- Example Usage:
- The
mainfunction creates two student objects, enrolls them in courses, updates their GPAs, and displays their information.
- The
Sample Output
Alice Smith has enrolled in Data Structures.
Alice Smith has enrolled in Algorithms.
Bob Johnson has enrolled in Calculus.
Bob Johnson has enrolled in Linear Algebra.
Alice Smith's GPA has been updated to 3.9.
Bob Johnson's GPA has been updated to 3.7.
Student 1 Information:
Student Name: Alice Smith
Age: 20
Major: Computer Science
GPA: 3.9
Enrolled Courses: Data Structures Algorithms
Student 2 Information:
Student Name: Bob Johnson
Age: 22
Major: Mathematics
GPA: 3.7
Enrolled Courses: Calculus Linear Algebra
This example can be expanded further to include additional features like handling scholarships, tracking extracurricular activities, or even simulating a college system with multiple students.
