Class 12 – 30 Marks ( 1 -Project ) and ( 20 -Practical )

Practical File

Computer Science Practical File

Submitted By: ________________________

Class: ____________   Roll No: ____________

Session: ____________

🔹 Step 1: Certificate

                         CERTIFICATE

This is to certify that __________________________ (Student Name)
of Class ______ has successfully completed the Computer Science 
Practical File as prescribed by the BSEB curriculum for the 
academic session _______.

The practical file consists of 17 programs in C++ and 3 SQL queries 
covering topics like Arrays, Structures, Stacks, Queues, File 
Handling, Computational Problems, and SQL commands.

Signature of Teacher                           
(____________________)  
  
Signature of Principal
(____________________)

Date: __ / __ / __

                           ACKNOWLEDGEMENT

I would like to express my sincere gratitude to my Computer Science 
teacher ______________________ for her/his valuable guidance and 
support throughout the completion of this practical file.  

I also extend my thanks to my school, parents, and friends who 
encouraged me and provided help whenever required.  

This practical file has given me the opportunity to explore concepts 
of C++ and SQL in depth and apply them practically.

🔹 Step 3: Index

S. No.Program TitleLanguagePage No.
1Linear Search in ArrayC++1
2Binary Search in ArrayC++3
3Bubble SortC++5
4Insertion SortC++7
5Merge Two ArraysC++9
6Insert Element in ArrayC++11
7Delete Element in ArrayC++13
8Matrix MultiplicationC++15
9Array of Structures (Students)C++17
10Array of Objects (Employees)C++19
11Stack using ArrayC++21
12Stack using Linked ListC++23
13Queue using ArrayC++25
14Circular Queue using ArrayC++27
15Queue using Linked ListC++29
16File Handling (Text File)C++31
17Fibonacci / FactorialC++33
18Create & Insert in TableSQL35
19Select with WHERE & ORDER BYSQL37
20Aggregate FunctionsSQL39

🔹 Program 1: Linear Search in Array

Aim:
To search an element in a 1D array using the Linear Search technique.

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int key, i, flag = 0;

    cout << "Enter element to search: ";
    cin >> key;

    for (i = 0; i < 5; i++) {
        if (arr[i] == key) {
            cout << "Element found at position " << i + 1 << endl;
            flag = 1;
            break;
        }
    }

    if (flag == 0)
        cout << "Element not found!" << endl;

    return 0;
}
Enter element to search: 30
Element found at position 3

Explanation:
This program traverses the array sequentially and compares each element with the key. If found, it prints the position; otherwise, it shows “not found”.

🔹 Program 2: Binary Search in Array

Aim:
To search an element in a sorted 1D array using the Binary Search technique.

#include <iostream>
using namespace std;

int main() {
    int arr[6] = {10, 20, 30, 40, 50, 60};
    int key, low = 0, high = 5, mid;
    bool found = false;

    cout << "Enter element to search: ";
    cin >> key;

    while (low <= high) {
        mid = (low + high) / 2;
        if (arr[mid] == key) {
            cout << "Element found at position " << mid + 1 << endl;
            found = true;
            break;
        }
        else if (arr[mid] < key) {
            low = mid + 1;
        }
        else {
            high = mid - 1;
        }
    }

    if (!found)
        cout << "Element not found!" << endl;

    return 0;
}
Enter element to search: 40
Element found at position 4

Explanation:
Binary Search works on sorted arrays. The middle element is compared with the key:

  • If equal → element found.
  • If key < mid → search left half.
  • If key > mid → search right half.
    This reduces time complexity to O(log n).

🔹 Program 3: Bubble Sort

Aim:
To sort elements of an array in ascending order using Bubble Sort.

Program (C++):

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {50, 10, 40, 20, 30};
    int n = 5;

    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    cout << "Sorted array: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

Sample Output:

Sorted array: 10 20 30 40 50

Explanation:
Bubble sort repeatedly swaps adjacent elements if they are in the wrong order. After each pass, the largest element moves to the end.


🔹 Program 4: Insertion Sort

Aim:
To sort elements of an array using Insertion Sort.

Program (C++):

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {25, 10, 30, 5, 20};
    int n = 5;

    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }

    cout << "Sorted array: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

Sample Output:

Sorted array: 5 10 20 25 30

Explanation:
Insertion sort builds the sorted part of the array one element at a time by shifting larger elements right and inserting the key in the correct position.


🔹 Program 5: Merge Two Arrays

Aim:
To merge two arrays into a single array.

Program (C++):

#include <iostream>
using namespace std;

int main() {
    int arr1[3] = {1, 2, 3};
    int arr2[3] = {4, 5, 6};
    int merged[6];

    for (int i = 0; i < 3; i++)
        merged[i] = arr1[i];

    for (int i = 0; i < 3; i++)
        merged[i + 3] = arr2[i];

    cout << "Merged array: ";
    for (int i = 0; i < 6; i++) {
        cout << merged[i] << " ";
    }
    cout << endl;

    return 0;
}

Sample Output:

Merged array: 1 2 3 4 5 6

Explanation:
The program copies all elements of the first array into a new array, then appends elements of the second array to form the merged array.

🔹 Program 6: Insert Element in Array

Aim:
To insert an element at a given position in an array.

Program (C++):

#include <iostream>
using namespace std;

int main() {
    int arr[10] = {10, 20, 30, 40, 50};
    int n = 5, pos, val;

    cout << "Enter position to insert (1-6): ";
    cin >> pos;
    cout << "Enter value: ";
    cin >> val;

    for (int i = n; i >= pos; i--) {
        arr[i] = arr[i - 1];
    }
    arr[pos - 1] = val;
    n++;

    cout << "Array after insertion: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

Sample Output:

Enter position to insert (1-6): 3
Enter value: 99
Array after insertion: 10 20 99 30 40 50

Explanation:
Elements are shifted one position to the right from the insertion point, and the new element is placed at the given position.


🔹 Program 7: Delete Element in Array

Aim:
To delete an element from a given position in an array.

Program (C++):

#include <iostream>
using namespace std;

int main() {
    int arr[10] = {10, 20, 30, 40, 50};
    int n = 5, pos;

    cout << "Enter position to delete (1-5): ";
    cin >> pos;

    for (int i = pos - 1; i < n - 1; i++) {
        arr[i] = arr[i + 1];
    }
    n--;

    cout << "Array after deletion: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

Sample Output:

Enter position to delete (1-5): 2
Array after deletion: 10 30 40 50

Explanation:
The element at the given position is removed by shifting all subsequent elements one step to the left.


🔹 Program 8: Matrix Multiplication (2D Array)

Aim:
To multiply two matrices using 2D arrays.

Program (C++):

#include <iostream>
using namespace std;

int main() {
    int a[2][2] = {{1, 2}, {3, 4}};
    int b[2][2] = {{5, 6}, {7, 8}};
    int c[2][2];

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            c[i][j] = 0;
            for (int k = 0; k < 2; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    cout << "Resultant Matrix:" << endl;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cout << c[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Sample Output:

Resultant Matrix:
19 22
43 50

Explanation:
Matrix multiplication is done by multiplying rows of the first matrix with columns of the second matrix and summing the products.

🔹 Program 9: Array of Structures (Student Records)

Aim:
To store and display student records using an array of structures.

Program (C++):

#include <iostream>
using namespace std;

struct Student {
    int roll;
    char name[20];
    float marks;
};

int main() {
    Student s[3];
    for (int i = 0; i < 3; i++) {
        cout << "Enter roll, name and marks of student " << i + 1 << ": ";
        cin >> s[i].roll >> s[i].name >> s[i].marks;
    }

    cout << "\nStudent Records:\n";
    for (int i = 0; i < 3; i++) {
        cout << s[i].roll << " " << s[i].name << " " << s[i].marks << endl;
    }

    return 0;
}

Sample Output:

Enter roll, name and marks of student 1: 1 Ram 85
Enter roll, name and marks of student 2: 2 Shyam 90
Enter roll, name and marks of student 3: 3 Mohan 75

Student Records:
1 Ram 85
2 Shyam 90
3 Mohan 75

Explanation:
A structure groups related data together. Here, an array of structures stores multiple students’ details.


🔹 Program 10: Array of Objects (Employee Data)

Aim:
To store and display employee details using an array of objects.

Program (C++):

#include <iostream>
using namespace std;

class Employee {
    int id;
    char name[20];
    float salary;

public:
    void getData() {
        cout << "Enter id, name and salary: ";
        cin >> id >> name >> salary;
    }
    void showData() {
        cout << id << " " << name << " " << salary << endl;
    }
};

int main() {
    Employee e[3];
    for (int i = 0; i < 3; i++) {
        e[i].getData();
    }

    cout << "\nEmployee Records:\n";
    for (int i = 0; i < 3; i++) {
        e[i].showData();
    }

    return 0;
}

Sample Output:

Enter id, name and salary: 101 Amit 50000
Enter id, name and salary: 102 Neha 60000
Enter id, name and salary: 103 Raj 55000

Employee Records:
101 Amit 50000
102 Neha 60000
103 Raj 55000

Explanation:
An array of objects allows storing multiple employee records, each with id, name, and salary.


🔹 Program 11: Stack using Array

Aim:
To implement stack operations (push, pop, display) using arrays.

Program (C++):

#include <iostream>
using namespace std;

#define MAX 5
int stack[MAX], top = -1;

void push(int val) {
    if (top == MAX - 1)
        cout << "Stack Overflow!" << endl;
    else
        stack[++top] = val;
}

void pop() {
    if (top == -1)
        cout << "Stack Underflow!" << endl;
    else
        cout << "Popped element: " << stack[top--] << endl;
}

void display() {
    if (top == -1)
        cout << "Stack is empty!" << endl;
    else {
        cout << "Stack elements: ";
        for (int i = top; i >= 0; i--)
            cout << stack[i] << " ";
        cout << endl;
    }
}

int main() {
    push(10);
    push(20);
    push(30);
    display();
    pop();
    display();
    return 0;
}

Sample Output:

Stack elements: 30 20 10 
Popped element: 30
Stack elements: 20 10

Explanation:
A stack follows LIFO (Last In First Out) principle. Push adds elements, Pop removes the top element, and Display prints the stack.

🔹 Program 12: Stack using Linked List

Aim:
To implement stack operations (push, pop, display) using a linked list.

Program (C++):

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};
Node* top = NULL;

void push(int val) {
    Node* newNode = new Node;
    newNode->data = val;
    newNode->next = top;
    top = newNode;
    cout << val << " pushed into stack" << endl;
}

void pop() {
    if (top == NULL)
        cout << "Stack Underflow!" << endl;
    else {
        cout << "Popped element: " << top->data << endl;
        Node* temp = top;
        top = top->next;
        delete temp;
    }
}

void display() {
    if (top == NULL)
        cout << "Stack is empty!" << endl;
    else {
        cout << "Stack elements: ";
        Node* temp = top;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
}

int main() {
    push(10);
    push(20);
    push(30);
    display();
    pop();
    display();
    return 0;
}

Sample Output:

10 pushed into stack
20 pushed into stack
30 pushed into stack
Stack elements: 30 20 10 
Popped element: 30
Stack elements: 20 10

Explanation:
The linked list allows dynamic memory allocation. Push inserts at the head, Pop deletes from the head, ensuring LIFO order.


🔹 Program 13: Queue using Array

Aim:
To implement queue operations (enqueue, dequeue, display) using arrays.

Program (C++):

#include <iostream>
using namespace std;

#define MAX 5
int queue[MAX], front = -1, rear = -1;

void enqueue(int val) {
    if (rear == MAX - 1)
        cout << "Queue Overflow!" << endl;
    else {
        if (front == -1) front = 0;
        queue[++rear] = val;
    }
}

void dequeue() {
    if (front == -1 || front > rear)
        cout << "Queue Underflow!" << endl;
    else
        cout << "Dequeued element: " << queue[front++] << endl;
}

void display() {
    if (front == -1 || front > rear)
        cout << "Queue is empty!" << endl;
    else {
        cout << "Queue elements: ";
        for (int i = front; i <= rear; i++)
            cout << queue[i] << " ";
        cout << endl;
    }
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    display();
    dequeue();
    display();
    return 0;
}

Sample Output:

Queue elements: 10 20 30 
Dequeued element: 10
Queue elements: 20 30

Explanation:
A queue follows FIFO (First In First Out) principle. Enqueue adds elements at the rear, Dequeue removes from the front.


🔹 Program 14: Circular Queue using Array

Aim:
To implement circular queue operations using arrays.

Program (C++):

#include <iostream>
using namespace std;

#define MAX 5
int cqueue[MAX], front = -1, rear = -1;

void enqueue(int val) {
    if ((front == 0 && rear == MAX - 1) || (front == rear + 1))
        cout << "Circular Queue Overflow!" << endl;
    else {
        if (front == -1) front = 0;
        rear = (rear + 1) % MAX;
        cqueue[rear] = val;
    }
}

void dequeue() {
    if (front == -1)
        cout << "Circular Queue Underflow!" << endl;
    else {
        cout << "Dequeued element: " << cqueue[front] << endl;
        if (front == rear) {
            front = rear = -1;
        } else {
            front = (front + 1) % MAX;
        }
    }
}

void display() {
    if (front == -1)
        cout << "Circular Queue is empty!" << endl;
    else {
        cout << "Circular Queue elements: ";
        int i = front;
        while (true) {
            cout << cqueue[i] << " ";
            if (i == rear) break;
            i = (i + 1) % MAX;
        }
        cout << endl;
    }
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    display();
    dequeue();
    display();
    return 0;
}

Sample Output:

Circular Queue elements: 10 20 30 
Dequeued element: 10
Circular Queue elements: 20 30

Explanation:
A circular queue efficiently reuses empty spaces by wrapping around. It avoids wastage of space seen in a simple linear queue.

🔹 Program 15: Queue using Linked List

Aim:
To implement queue operations (enqueue, dequeue, display) using a linked list.

Program (C++):

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};
Node* front = NULL;
Node* rear = NULL;

void enqueue(int val) {
    Node* newNode = new Node;
    newNode->data = val;
    newNode->next = NULL;
    if (rear == NULL) {
        front = rear = newNode;
    } else {
        rear->next = newNode;
        rear = newNode;
    }
    cout << val << " enqueued into queue" << endl;
}

void dequeue() {
    if (front == NULL)
        cout << "Queue Underflow!" << endl;
    else {
        cout << "Dequeued element: " << front->data << endl;
        Node* temp = front;
        front = front->next;
        if (front == NULL) rear = NULL;
        delete temp;
    }
}

void display() {
    if (front == NULL)
        cout << "Queue is empty!" << endl;
    else {
        cout << "Queue elements: ";
        Node* temp = front;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    display();
    dequeue();
    display();
    return 0;
}

Sample Output:

10 enqueued into queue
20 enqueued into queue
30 enqueued into queue
Queue elements: 10 20 30 
Dequeued element: 10
Queue elements: 20 30

Explanation:
In a linked list implementation, rear adds elements (enqueue) and front removes elements (dequeue), following FIFO order.


🔹 Program 16: File Handling (Read & Write in Text File)

Aim:
To write data to a text file and then read it back.

Program (C++):

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream fout("data.txt");
    fout << "Hello, this is a C++ file handling example.";
    fout.close();

    ifstream fin("data.txt");
    string line;
    cout << "Reading from file:" << endl;
    while (getline(fin, line)) {
        cout << line << endl;
    }
    fin.close();

    return 0;
}

Sample Output:

Reading from file:
Hello, this is a C++ file handling example.

Explanation:
The program uses ofstream to write data to a file and ifstream to read the contents back.


🔹 Program 17: Fibonacci Series

Aim:
To generate Fibonacci series up to n terms.

Program (C++):

#include <iostream>
using namespace std;

int main() {
    int n, t1 = 0, t2 = 1, nextTerm;
    cout << "Enter number of terms: ";
    cin >> n;

    cout << "Fibonacci Series: ";
    for (int i = 1; i <= n; i++) {
        cout << t1 << " ";
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
    }
    cout << endl;
    return 0;
}

Sample Output:

Enter number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8

Explanation:
The Fibonacci series starts with 0 and 1. Each new term is the sum of the previous two terms.

🔹 SQL Program 18: Create Table & Insert Values

Aim:
To create a table Student and insert records into it.

SQL Code:

CREATE TABLE Student (
    Roll INT PRIMARY KEY,
    Name VARCHAR(20),
    Marks INT,
    Course VARCHAR(20)
);

INSERT INTO Student VALUES (1, 'Amit', 85, 'CS');
INSERT INTO Student VALUES (2, 'Neha', 92, 'IT');
INSERT INTO Student VALUES (3, 'Raj', 76, 'CS');

Sample Output:

Table created.
3 rows inserted.

Explanation:
A table Student is created with columns Roll, Name, Marks, and Course. Then 3 rows are inserted using INSERT INTO.


🔹 SQL Program 19: SELECT with WHERE & ORDER BY

Aim:
To display records of students with conditions and sorting.

SQL Code:

SELECT * FROM Student WHERE Marks > 80 ORDER BY Marks DESC;

Sample Output:

Roll | Name | Marks | Course
-----+------+-------+--------
2    | Neha | 92    | IT
1    | Amit | 85    | CS

Explanation:
The query selects students with marks greater than 80 and sorts them in descending order of marks using ORDER BY.


🔹 SQL Program 20: Aggregate Functions

Aim:
To use aggregate functions on the Student table.

SQL Code:

SELECT COUNT(*) AS Total_Students FROM Student;
SELECT MAX(Marks) AS Highest, MIN(Marks) AS Lowest FROM Student;
SELECT AVG(Marks) AS Average_Marks FROM Student;
SELECT Course, AVG(Marks) FROM Student GROUP BY Course;

Sample Output:

Total_Students
3

Highest | Lowest
92      | 76

Average_Marks
84.3

Course | AVG(Marks)
CS     | 80.5
IT     | 92

Explanation:
Aggregate functions perform calculations on data:

  • COUNT counts total rows.
  • MAX & MIN find highest and lowest marks.
  • AVG finds average marks.
  • GROUP BY calculates average marks per course.

Project File:

📘 Student Report Card Management System

📖 Introduction

The Student Report Card Management System is a C++ project developed using the principles of Object-Oriented Programming (OOP) and file handling techniques. The main objective of this project is to provide an efficient way of managing student academic records such as roll number, name, marks, and grade.

Traditionally, student records are maintained manually, which is time-consuming, error-prone, and difficult to update. This project aims to automate the process by storing student records in a binary data file, making them permanent, secure, and easily retrievable.

The system is designed with a menu-driven interface to allow users (teachers or administrators) to:

  • Add new student records
  • Display all stored records
  • Search for a student by roll number
  • Delete a student’s record

The project makes use of the following C++ concepts:

  • Classes and Objects for encapsulating student details and functionalities
  • Constructors and Functions for modular programming
  • File Handling (ifstream, ofstream, fstream) for permanent data storage
  • Conditional Statements & Loops for user interaction
  • Switch-Case Menu System for smooth navigation

This project is a practical example of how C++ Object-Oriented Technology can be applied in real-life applications, providing both academic learning and problem-solving experience.

📑 Documentation (for Project Report)

🔹 Objectives:

  • To implement a Student Report Card System using C++ OOP concepts.
  • To manage records using binary file handling.

🔹 Tools Used:

  • Programming Language: C++
  • File Handling: ifstream, ofstream, fstream
  • OOP Concepts: Classes, Objects, Functions

🔹 Advantages:

  • Easy to store and retrieve student records.
  • Uses file handling for permanent storage.
  • Implements Encapsulation and Abstraction via OOP.

🔹 Features:

  • Add new student records (Roll No, Name, Class, Marks)
  • Display all student records
  • Search student by Roll No
  • Update student details
  • Delete student records
  • Store data permanently in a binary file

✅ C++ Project Code

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

class Student {
    int roll;
    char name[30];
    int marks;
    char grade;

public:
    void getData() {
        cout << "Enter Roll No: ";
        cin >> roll;
        cout << "Enter Name: ";
        cin.ignore();
        cin.getline(name, 30);
        cout << "Enter Marks: ";
        cin >> marks;
        calculateGrade();
    }

    void showData() {
        cout << setw(5) << roll << setw(20) << name 
             << setw(10) << marks << setw(8) << grade << endl;
    }

    int getRoll() { return roll; }

    void calculateGrade() {
        if (marks >= 90) grade = 'A';
        else if (marks >= 75) grade = 'B';
        else if (marks >= 60) grade = 'C';
        else if (marks >= 40) grade = 'D';
        else grade = 'F';
    }
};

// Function prototypes
void addStudent();
void displayStudents();
void searchStudent(int);
void deleteStudent(int);

int main() {
    int choice, roll;
    do {
        cout << "\n===== Student Report Card Management =====";
        cout << "\n1. Add Student";
        cout << "\n2. Display All Students";
        cout << "\n3. Search Student";
        cout << "\n4. Delete Student";
        cout << "\n5. Exit";
        cout << "\nEnter your choice: ";
        cin >> choice;

        switch (choice) {
        case 1: addStudent(); break;
        case 2: displayStudents(); break;
        case 3:
            cout << "Enter Roll No to search: ";
            cin >> roll;
            searchStudent(roll);
            break;
        case 4:
            cout << "Enter Roll No to delete: ";
            cin >> roll;
            deleteStudent(roll);
            break;
        case 5: cout << "Exiting program...\n"; break;
        default: cout << "Invalid choice!\n";
        }
    } while (choice != 5);
    return 0;
}

// Function to add student
void addStudent() {
    Student s;
    ofstream fout("student.dat", ios::binary | ios::app);
    s.getData();
    fout.write((char*)&s, sizeof(s));
    fout.close();
    cout << "Student record added successfully!\n";
}

// Function to display all students
void displayStudents() {
    Student s;
    ifstream fin("student.dat", ios::binary);
    if (!fin) {
        cout << "No data found!\n";
        return;
    }
    cout << setw(5) << "Roll" << setw(20) << "Name"
         << setw(10) << "Marks" << setw(8) << "Grade" << endl;
    while (fin.read((char*)&s, sizeof(s))) {
        s.showData();
    }
    fin.close();
}

// Function to search student by roll
void searchStudent(int roll) {
    Student s;
    ifstream fin("student.dat", ios::binary);
    bool found = false;
    while (fin.read((char*)&s, sizeof(s))) {
        if (s.getRoll() == roll) {
            cout << "Student found!\n";
            cout << setw(5) << "Roll" << setw(20) << "Name"
                 << setw(10) << "Marks" << setw(8) << "Grade" << endl;
            s.showData();
            found = true;
            break;
        }
    }
    fin.close();
    if (!found) cout << "Student not found!\n";
}

// Function to delete student by roll
void deleteStudent(int roll) {
    Student s;
    ifstream fin("student.dat", ios::binary);
    ofstream fout("temp.dat", ios::binary);
    bool found = false;

    while (fin.read((char*)&s, sizeof(s))) {
        if (s.getRoll() != roll) {
            fout.write((char*)&s, sizeof(s));
        } else {
            found = true;
        }
    }

    fin.close();
    fout.close();
    remove("student.dat");
    rename("temp.dat", "student.dat");

    if (found) cout << "Record deleted successfully!\n";
    else cout << "Student not found!\n";
}

📌 Sample Output

===== Student Report Card Management =====
1. Add Student
2. Display All Students
3. Search Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter Roll No: 101
Enter Name: Amit
Enter Marks: 88
Student record added successfully!

===== Student Report Card Management =====
2
Roll    Name                Marks   Grade
101     Amit                88      B

📝 Explanation of the Project

1. Object-Oriented Design

  • We created a class Student with attributes:
    • roll (Roll Number)
    • name (Student Name)
    • marks (Marks scored)
    • grade (Calculated Grade: A/B/C/D/F)
  • The class has functions:
    • getData() → input details
    • showData() → display details
    • calculateGrade() → compute grade automatically

👉 This ensures Encapsulation (data + functions together) and Abstraction (we only expose meaningful operations).


2. File Handling (Permanent Storage)

  • Student records are saved in a binary file (student.dat) so that data is not lost when the program ends.
  • Functions use file streams:
    • ofstream fout("student.dat", ios::binary | ios::app)append new student
    • ifstream fin("student.dat", ios::binary)read student data
    • remove("student.dat"); rename("temp.dat", "student.dat");delete student safely

👉 This makes the system persistent (data is stored permanently).


3. Menu-Driven Program

The program runs in a loop until the user chooses Exit.
Menu options:

  1. Add Student
  2. Display All Students
  3. Search Student (by Roll No)
  4. Delete Student
  5. Exit

4. Functions and Their Working

🔹 addStudent()

  • Creates a Student object.
  • Takes input from the user.
  • Writes it into student.dat (binary format).

🔹 displayStudents()

  • Reads all student objects from student.dat.
  • Calls showData() for each record.

🔹 searchStudent(int roll)

  • Reads records one by one.
  • Compares roll number with given value.
  • If found, displays the record.

🔹 deleteStudent(int roll)

  • Reads all records from student.dat.
  • Copies all records except the one to be deleted into a temporary file temp.dat.
  • Replaces the old file with the new one.

5. Grade Calculation

  • calculateGrade() assigns grade based on marks:
    • >=90 → A
    • >=75 → B
    • >=60 → C
    • >=40 → D
    • <40 → F

6. Sample Workflow

1. User selects "Add Student"
   → Enter Roll: 101, Name: Amit, Marks: 88
   → Grade calculated = B
   → Record saved in student.dat

2. User selects "Display All Students"
   → Reads all records
   → Displays Roll | Name | Marks | Grade

3. User selects "Search Student" (Roll = 101)
   → Finds record, displays details.

4. User selects "Delete Student" (Roll = 101)
   → Copies all records except 101 into temp.dat
   → Replaces student.dat with temp.dat
   → Record deleted successfully.

✅ In short:

  • OOP manages student details logically.
  • File Handling ensures records are saved permanently.
  • Menu System makes it user-friendly.