C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

#include <iostream>

#include <string>

#include <vector>

class Student {

private:

std::string name;

int id;

std::vector<float> grades;

public:

// Constructor to initialize name, id, and grades

Student(const std::string& studentName, int studentId, const


std::vector<float>& studentGrades)

: name(studentName), id(studentId), grades(studentGrades) {}

// Getter functions

std::string getName() const { return name; }

int getId() const { return id; }

const std::vector<float>& getGrades() const { return grades; }

// Setter functions

void setName(const std::string& newName) { name = newName; }

void setId(int newId) { id = newId; }

void setGrades(const std::vector<float>& newGrades) { grades =


newGrades; }
// Display student information

void displayInfo() const {

std::cout << "Student Name: " << name << std::endl;

std::cout << "Student ID: " << id << std::endl;

std::cout << "Grades: ";

for (const float grade : grades) {

std::cout << grade << " ";

std::cout << std::endl;

// Add a grade to the student's grades vector

void addGrade(float grade) {

grades.push_back(grade);

// Calculate and return the average grade

float averageGrade() const {

if (grades.empty()) {

return 0.0; // Handle division by zero

float sum = 0.0;

for (const float grade : grades) {

sum += grade;

return sum / grades.size();

}
};

int main() {

// Example usage

std::vector<float> initialGrades = {85.5, 92.0, 78.5};

Student student1("Alice", 12345, initialGrades);

student1.displayInfo();

// Add a new grade

student1.addGrade(88.5);

student1.displayInfo();

// Calculate average grade

std::cout << "Average Grade: " << student1.averageGrade() <<


std::endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy