0% found this document useful (0 votes)
4 views

C

Basics of C++

Uploaded by

shakazul1101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C

Basics of C++

Uploaded by

shakazul1101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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