Grupo
Programación Orientada a Objetos en C++
Ojetivo
1. Define una clase llamada "Estudiante" con atributos privados: nombre (string), curso (string) y calificación promedio (double).
2. Agrega un constructor para inicializar el nombre, el curso y la calificación promedio del estudiante.
3. Implementa funciones getter y setter para cada atributo.
4. Agrega una función miembro para mostrar los detalles del estudiante, incluyendo el nombre, el curso y la calificación promedio.
5. En la función principal, crea un objeto de la clase Estudiante y muestra los detalles.
Crea una clase Estudiante con atributos para nombre, curso y calificación promedio.
Ejemplo de Código C++
Mostrar Código C++
#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
}
Salida
Student Name: John Doe
Course: Computer Science
Average Grade: 88.5
Comparte este ejercicio C++