Student Class in C++ with Attributes for Name, Course, and Average Grade

In this exercise, you will implement a C++ class called "Student" with attributes such as name, course, and average grade. The class will have methods to set and get the values of these attributes. You will also implement a function to display the student's details. This task will help you practice object-oriented programming in C++ by creating a class with multiple attributes and member functions to manipulate and display the data.

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

 Copy 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

Share this C++ Exercise


More C++ Programming Exercises of Object-Oriented Programming in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.