Department of Electrical Engineering
Riphah College of Science & Technology
Faculty of Engineering & Applied Sciences
Riphah International University, Lahore
Program: B.Sc. Electrical engineering Semester: II
Subject: CSL-212 Object Oriented Programming Date: ……………….
Experiment 15: OPEN-ENDED LAB
OBJECTIVES:
(i) To use the concept of virtual, pure virtual and friend functions in a real-life
implement of classes
Name:
SAP:
No. Lab Evaluation Scheme Obtained
Marks
1 Understanding and Ability to Conduct Experiment. (0-5)
2 Implementation and Results (0-5)
Total
No. Lab Report Scheme Obtained
Marks
1 Report Content (0-5)
2 Code/Output Presentation and Conclusion (0-5)
Total
Remarks (if any): ………………………………….
Name & Signature of faculty: …………………………………
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Introduction:
This is a comprehensive student management system designed to simplify the process of managing
student records. The system provides a range of features, including the ability to add new students,
view existing student records, search for specific students, and delete student records as needed. Built
using the C++ programming language, this console-based application is easy to use and provides a
user-friendly interface for managing student data.
Code (with comments)
#include <iostream>
using namespace std;
// Abstract base class with pure virtual functions
class Student
{
protected:
string name;
int rollNo;
string course;
int marks;
public:
virtual void addStudentRecord() = 0; // Pure virtual function
virtual void viewStudentRecord() = 0; // Pure virtual function
int getRollNo()
{
return rollNo;
}
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
string getName()
{
return name;
}
int getMarks()
{
return marks;
}
// Friend function declaration
friend void displayRecordCount(StudentManagementSystem sms);
};
class StudentManagementSystem : public Student
{
private:
Student students[10]; // Array to store 10 student records
int count; // Count of student records
public:
StudentManagementSystem()
{
count = 0;
}
void addStudentRecord() override
{
if (count < 10)
{
students[count].addStudentRecord(); // Calls the derived class method
count++;
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
else
{
cout << "Cannot add more records." << endl;
}
}
void viewStudentRecord(int rollNo) {
for (int i = 0; i < count; i++) {
if (students[i].getRollNo() == rollNo)
{
students[i].viewStudentRecord(); // Calls the derived class method
return;
}
}
cout << "Student record not found." << endl;
}
void viewAllStudentRecords() {
for (int i = 0; i < count; i++) {
students[i].viewStudentRecord(); // Calls the derived class method
cout << endl;
}
}
void searchStudentRecord(string name)
{
for (int i = 0; i < count; i++)
{
if (students[i].getName() == name)
{
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
students[i].viewStudentRecord(); // Calls the derived class method
return;
}
}
cout << "Student record not found." << endl;
}
// Friend function definition
friend void displayRecordCount(StudentManagementSystem sms);
};
// Friend function implementation
void displayRecordCount(StudentManagementSystem sms) {
cout << "Total student records: " << sms.count << endl;
}
int main() {
StudentManagementSystem sms;
int choice;
while (choice != 6) {
cout << "\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
cout << "Student Management System" << endl;
cout << "1. Add Student Record" << endl;
cout << "2. View Student Record" << endl;
cout << "3. View All Student Records" << endl;
cout << "4. Search Student Record" << endl;
cout << "5. Display Record Count" << endl;
cout << "6. Exit" << endl;
cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\
n";
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
if (choice == 1)
{
sms.addStudentRecord();
}
else if (choice == 2)
{
int rollNo;
cout << "Enter student roll number: ";
cin >> rollNo;
cout << endl << endl;
sms.viewStudentRecord(rollNo);
}
else if (choice == 3)
{
sms.viewAllStudentRecords();
}
else if (choice == 4)
{
string name;
cout << "Enter student name: ";
cin >> name;
cout << endl << endl;
sms.searchStudentRecord(name);
}
else if (choice == 5)
{
displayRecordCount(sms); // Display the count using the friend function
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
}
else {
cout << endl << "Exited" << endl;
}
}
return 0;
}
Conclusion
This lab successfully illustrates the fundamental concepts of object-oriented programming in
C++, including class inheritance, abstract classes with pure virtual functions, and the use of
friend functions. The implemented Student Management System effectively manages student
records without using reference operators, demonstrating practical applications of these
concepts in a real-world scenario. The exercise reinforces the importance of object-oriented
principles in structuring and managing data in a clean, modular, and maintainable manner.
Object Oriented Programming 2nd Semester-EE RCST Lahore