Air University
Assignment #01
Subject: Programming Fundamentals
Submitted By
Submitted To
Submission Date:
🧾 Assignment Title:
Object-Oriented Programming in C++ — Student Management System
🎯 Objective:
To demonstrate the use of classes, objects, inheritance, polymorphism, encapsulation, and
abstraction in a C++ program.
🧩 Problem Statement:
Design a simple Student Management System using Object-Oriented Programming concepts in
C++.
The system should store student information, calculate grades, and display reports.
🧠 Concepts Used:
1. Encapsulation — using private data members with getter/setter functions.
2. Inheritance — GraduateStudent and UndergraduateStudent inherit from Student.
3. Polymorphism — overriding the displayInfo() function.
4. Abstraction — using an abstract base class Person.
💻 Code Implementation
#include <iostream>
#include <string>
using namespace std;
// ===============================
// ABSTRACT CLASS (Abstraction)
// ===============================
class Person {
protected:
string name;
int age;
public:
Person(string n = "", int a = 0) : name(n), age(a) {}
// Pure virtual function (makes this class abstract)
virtual void displayInfo() const = 0;
};
// ===============================
// BASE CLASS (Encapsulation)
// ===============================
class Student : public Person {
private:
string studentID;
float marks;
public:
Student(string n = "", int a = 0, string id = "", float m = 0.0)
: Person(n, a), studentID(id), marks(m) {}
void setMarks(float m) { marks = m; }
float getMarks() const { return marks; }
string getID() const { return studentID; }
// Virtual function for polymorphism
virtual void displayInfo() const override {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Student ID: " << studentID << endl;
cout << "Marks: " << marks << endl;
}
// Calculate grade based on marks
char calculateGrade() const {
if (marks >= 85) return 'A';
else if (marks >= 70) return 'B';
else if (marks >= 60) return 'C';
else if (marks >= 50) return 'D';
else return 'F';
}
};
// ===============================
// DERIVED CLASS 1
// ===============================
class UndergraduateStudent : public Student {
string department;
public:
UndergraduateStudent(string n, int a, string id, float m, string dept)
: Student(n, a, id, m), department(dept) {}
void displayInfo() const override {
cout << "----- Undergraduate Student -----" << endl;
Student::displayInfo();
cout << "Department: " << department << endl;
cout << "Grade: " << calculateGrade() << endl;
cout << "---------------------------------\n";
}
};
// ===============================
// DERIVED CLASS 2
// ===============================
class GraduateStudent : public Student {
string researchTopic;
public:
GraduateStudent(string n, int a, string id, float m, string topic)
: Student(n, a, id, m), researchTopic(topic) {}
void displayInfo() const override {
cout << "--------- Graduate Student ---------" << endl;
Student::displayInfo();
cout << "Research Topic: " << researchTopic << endl;
cout << "Grade: " << calculateGrade() << endl;
cout << "-----------------------------------\n";
}
};
// ===============================
// MAIN FUNCTION
// ===============================
int main() {
// Create objects using pointers to demonstrate polymorphism
Person* students[2];
students[0] = new UndergraduateStudent("Ali Ahmed", 20, "UG101", 78.5, "Computer
Science");
students[1] = new GraduateStudent("Sara Khan", 24, "GR202", 88.0, "Artificial Intelligence");
cout << "\n===== STUDENT REPORT =====\n\n";
// Polymorphic behavior
for (int i = 0; i < 2; i++) {
students[i]->displayInfo();
cout << endl;
}
// Free memory
for (int i = 0; i < 2; i++) {
delete students[i];
}
return 0;
}
📘 Explanation:
Concept Example in Code
Encapsulation Private data (marks, studentID) with public setters/getters
Inheritance UndergraduateStudent and GraduateStudent inherit from Student
Polymorphism displayInfo() overridden in derived classes and called via base pointer
Abstraction Person is an abstract base class with pure virtual function displayInfo()
🧾 Sample Output:
===== STUDENT REPORT =====
----- Undergraduate Student -----
Name: Ali Ahmed
Age: 20
Student ID: UG101
Marks: 78.5
Department: Computer Science
Grade: B
---------------------------------
--------- Graduate Student ---------
Name: Sara Khan
Age: 24
Student ID: GR202
Marks: 88
Research Topic: Artificial Intelligence
Grade: A
-----------------------------------
🧮 Conclusion:
This program successfully demonstrates Object-Oriented Programming principles in C++
through a real-world example of a student management system.
It highlights data hiding, inheritance, function overriding, and abstraction, providing a clear
understanding of OOP in practice.