33
Total Posts
3
Categories
24/7
Updated

C++

Write a C++ program to display Names, Roll No., and grades of 3 students who have appeared in the examination. Declare the class of name, Roll No. And grade. Create array of class objects. Read and display the contents of the array.

Published on Dec 22, 2025
#include <iostream>
#include <string>
using namespace std;

class Student {
public:
    string name;
    int rollNo;
    char grade;

    void readData() {
        cout << "Enter Name: ";
        cin.ignore();
        getline(cin, name);

        cout << "Enter Roll Number: ";
        cin >> rollNo;

        cout << "Enter Grade: ";
        cin >> grade;
    }

    void displayData() {
        cout << "Name: " << name << endl;
        cout << "Roll No: " << rollNo << endl;
        cout << "Grade: " << grade << endl;
    }
};

int main() {
    Student students[3];

    cout << "Enter details of 3 students:\n";
    for (int i = 0; i < 3; i++) {
        cout << "\nStudent " << i + 1 << ":\n";
        students[i].readData();
    }

    cout << "\n--- Student Details ---\n";
    for (int i = 0; i < 3; i++) {
        cout << "\nStudent " << i + 1 << ":\n";
        students[i].displayData();
    }

    return 0;
}

Documentation

  • A class Student is declared with:

    • name

    • rollNo

    • grade

  • An array of class objects is created for 3 students.

  • Data is read from the user and then displayed.

 

Input

Enter details of 3 students:

Student 1:
Enter Name: Rahul
Enter Roll Number: 101
Enter Grade: A

 

Student 2:
Enter Name: Neha
Enter Roll Number: 102
Enter Grade: B

 

Student 3:
Enter Name: Amit
Enter Roll Number: 103
Enter Grade: A

 

Output

--- Student Details ---

Student 1:
Name: Rahul
Roll No: 101
Grade: A

 

Student 2:
Name: Neha
Roll No: 102
Grade: B

 

Student 3:
Name: Amit
Roll No: 103
Grade: A