Group
Object-Oriented Programming in C++
Objective
1. Define a class called "Student" with private attributes: name (string), course (string), and average grade (double).
2. Add a constructor to initialize the student’s name, course, and average grade.
3. Implement getter and setter functions for each attribute.
4. Add a member function to display the student's details, including the name, course, and average grade.
5. In the main function, create an object of the Student class and display the details.
Create a Student class with attributes for name, course, and average grade.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the standard input/output library
#include <string> // Include the string library
using namespace std;
// Define the 'Student' class
class Student {
private:
string name; // Private attribute for the student's name
string course; // Private attribute for the student's course
double averageGrade; // Private attribute for the student's average grade
public:
// Constructor to initialize the 'Student' object with name, course, and average grade
Student(string n, string c, double avg) {
name = n; // Set the name to the passed value
course = c; // Set the course to the passed value
averageGrade = avg; // Set the average grade to the passed value
}
// Getter function to retrieve the name
string getName() {
return name;
}
// Getter function to retrieve the course
string getCourse() {
return course;
}
// Getter function to retrieve the average grade
double getAverageGrade() {
return averageGrade;
}
// Member function to display the student's details
void displayDetails() {
cout << "Student Name: " << name << endl; // Print the student's name
cout << "Course: " << course << endl; // Print the student's course
cout << "Average Grade: " << averageGrade << endl; // Print the student's average grade
}
};
int main() {
// Create an object of the 'Student' class with sample data
Student student1("John Doe", "Computer Science", 88.5);
// Display the student's details
student1.displayDetails();
return 0; // Return 0 to indicate successful execution
}
Output
Student Name: John Doe
Course: Computer Science
Average Grade: 88.5