xyz
xyz
TECHNOLOGY (IT)
SESSION 2024-25
SEMESTER : 2
Submitted by :
Name : KSHEERA SAGAR SHIVANSH
Scholar No : 24U030057
Subject : OBJECT ORIENTED
PROGRAMMING
Q1) write a program to create a banking system using C++
1)create bank account structure - account holders name account number and balance
2)write functions deposit money & withdraw money & display account details and use switch case inside
do while loop and allow user to deposit /withdraw/view balance until they choose to exit
CODE :
#include <iostream>
#include <string>
using namespace std;
struct BankAccount
{
string name;
int accountNumber;
double balance;
};
int main()
{
BankAccount account;
cout << "Enter account holder's name: ";
getline(cin, account.name);
cout << "Enter account number: ";
cin >> account.accountNumber;
cout << "Enter initial balance: ";
cin >> account.balance;
int choice;
double amount;
do
{
cout << "\n===== Banking System Menu =====" << endl;
cout << "1. Deposit Money" << endl;
cout << "2. Withdraw Money" << endl;
cout << "3. View Account Details" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nEnter amount to deposit: ";
cin >> amount;
deposit(account, amount);
break;
case 2:
cout << "\nEnter amount to withdraw: ";
cin >> amount;
withdraw(account, amount);
break;
case 3:
displayAccountDetails(account);
break;
case 4:
cout << "\nExiting the banking system. Thank you!\n";
break;
default:
cout << "\nInvalid choice. Please try again.\n";
}
} while (choice != 4);
return 0;
}
OUTPUT :
Enter account holder's name: SHIVANSH
Enter account number: 156982
Enter initial balance: 20000
CODE :
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
int bookID;
string title;
string authorName;
bool isIssued;
string issuedTo;
public:
Book() {
bookID = 0;
title = "Untitled";
authorName = "Unknown";
isIssued = false;
issuedTo = "N/A";
}
void displayDetails() {
cout << "Book ID: " << bookID << endl;
cout << "Title: " << title << endl;
cout << "Author: " << authorName << endl;
cout << "Status: " << (isIssued ? "Issued to " + issuedTo : "Available")
<< endl;
}
void returnBook() {
if (isIssued) {
isIssued = false;
issuedTo = "N/A";
cout << "Book successfully returned" << endl;
} else {
cout << "The book is not issued currently" << endl;
}
}
int getBookID() {
return bookID;
}
};
void addBook(Book books[], int &count, int id, string title, string author) {
books[count].setBookDetails(id, title, author);
count++;
}
int main() {
const int MAX_BOOKS = 100;
Book books[MAX_BOOKS];
int bookCount = 0;
int choice;
do {
cout << "\nLibrary Management System Menu:\n";
cout << "1. Add a new book\n";
cout << "2. Issue a book\n";
cout << "3. Return a book\n";
cout << "4. Display all book details\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int id;
string title, author;
cout << "Enter Book ID: ";
cin >> id;
cin.ignore();
cout << "Enter Title: ";
getline(cin, title);
cout << "Enter Author: ";
getline(cin, author);
addBook(books, bookCount, id, title, author);
cout << "Book added successfully!" << endl;
break;
}
case 2: {
int id;
cout << "Enter Book ID to issue: ";
cin >> id;
int index = findBookIndex(books, bookCount, id);
if (index != -1) {
string person;
cout << "Enter Person's Name: ";
cin >> person;
books[index].issueBook(person);
} else {
cout << "Book ID not found!" << endl;
}
break;
}
case 3: {
int id;
cout << "Enter Book ID to return: ";
cin >> id;
int index = findBookIndex(books, bookCount, id);
if (index != -1) {
books[index].returnBook();
} else {
cout << "Book ID not found!" << endl;
}
break;
}
case 4: {
for (int i = 0; i < bookCount; ++i) {
cout << "\nBook " << i + 1 << " details:\n";
books[i].displayDetails();
}
break;
}
case 5:
cout << "Exiting program...\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 5);
return 0;
}
OUTPUT :
===== Library Management System =====
1. Add a new book
2. Issue a book
3. Return a book
4. Display all books
5. Exit
Enter your choice: 1
Enter Book ID: 101
Enter Title: The Alchemist
Enter Author Name: Paulo Coelho
Book added successfully!
Member variables:
string name;
string roll; float marks;
static int studentCount;
Member functions:
Void setStudentDetails(string name, string rollno, float marks); // initialize student details
Void display(); // Function to display student details void display()
static void showStudentCount() // Static function to display total number of students
void compare(Student s); // Function to compare two students' marks
CODE :
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string roll;
float marks;
static int studentCount;
public:
Student() {
studentCount++;
}
void display() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << roll << endl;
cout << "Marks: " << marks << endl;
}
void compare(Student s) {
if (marks > s.marks) {
cout << name << " has higher marks (" << marks << ") than " << s.name
<< " (" << s.marks << ")" << endl;
} else if (marks < s.marks) {
cout << s.name << " has higher marks (" << s.marks << ") than " <<
name << " (" << marks << ")" << endl;
} else {
cout << name << " and " << s.name << " have equal marks: " << marks <<
endl;
}
}
};
int Student::studentCount = 0;
int main() {
int n;
cout << "Enter number of students: ";
cin >> n;
cin.ignore();
Student students[100];
Student::showStudentCount();
if (n >= 2) {
cout << "\n--- Comparing first two students ---\n";
students[0].compare(students[1]);
}
return 0;
}
OUTPUT :
Enter number of students: 2
Student 1:
Name: RAM
Roll Number: 50
Marks: 99
Student 2:
Name: RAJU
Roll Number: 39
Marks: 80
Q4) Student Grade Management System using C++. Implement a constructor to initialize the student's
roll number, name, and marks. Calculate total and average marks in the constructor. Implement a
destructor that displays a message when a student object is deleted.
Member variables:
int rollNumber;
string Name;
int marks[3]; (array of integers for 3 subjects of a particular student)
int totalMarks; (Calculate total marks of a particular student)
float averageMarks; (Average marks of a student / average of 3 subjects)
static int CountGrade[4]; // Count number of students for Each Grade (CountGrade[0] will contains count
of students who gets grade A, CountGrade[1] for grade B, CountGrade[2] for Grade C, CountGrade[3] for
grade F)
Member functions:
Student(5 parameters) // To initialize students details
char getGrade() // To determine grade of students based on their averageMarks
Assigning grades based on averageMarks:
100 → Grade A
75 - 89 → Grade B
50 - 74 → Grade C
< 50 → Grade F
Display() // Display details of all students one by one
Static void getStudentCount() // Will print number of students for each grade
In Main Section:
Create array of objects to store details of multiple students
Call parameterized constructor for each array index
Student Array[100]; // use one default empty constructor in class
For(i=0 to N){
Student S(arguments) // temporary object for parameterized constructor
Array[i] = S;
}
CODE :
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int rollNumber;
string Name;
int marks[3];
int totalMarks;
float averageMarks;
static int CountGrade[4];
public:
Student() {
rollNumber = 0;
Name = "";
totalMarks = 0;
averageMarks = 0.0;
}
Student(int roll, string name, int m1, int m2, int m3) {
rollNumber = roll;
Name = name;
marks[0] = m1;
marks[1] = m2;
marks[2] = m3;
totalMarks = m1 + m2 + m3;
averageMarks = totalMarks / 3.0;
~Student() {
cout << "Student with Roll No. " << rollNumber << " is deleted." << endl;
}
char getGrade() {
if (averageMarks == 100)
return 'A';
else if (averageMarks >= 75 && averageMarks <= 89)
return 'B';
else if (averageMarks >= 50 && averageMarks < 75)
return 'C';
else
return 'F';
}
void display() {
cout << "\nRoll Number: " << rollNumber << endl;
cout << "Name: " << Name << endl;
cout << "Marks: " << marks[0] << ", " << marks[1] << ", " << marks[2] <<
endl;
cout << "Total Marks: " << totalMarks << endl;
cout << "Average Marks: " << averageMarks << endl;
cout << "Grade: " << getGrade() << endl;
}
int main() {
int n;
cout << "Enter number of students: ";
cin >> n;
cin.ignore();
Student Array[100];
cout << "\nEnter details for Student " << i + 1 << ":\n";
cout << "Roll Number: ";
cin >> roll;
cin.ignore();
cout << "Name: ";
getline(cin, name);
cout << "Enter marks in 3 subjects: ";
cin >> m1 >> m2 >> m3;
Student::getStudentCount();
return 0;
}
OUTPUT :
Enter number of students: 2
Enter details for Student 1:
Roll Number: 1
Name: Alice
Enter marks in 3 subjects: 95 89 76
Roll Number: 1
Name: Alice
Marks: 95, 89, 76
Total Marks: 260
Average Marks: 86.6667
Grade: B
Roll Number: 2
Name: Bob
Marks: 30, 45, 55
Total Marks: 130
Average Marks: 43.3333
Grade: F
Grade Statistics:
Grade A: 0
Grade B: 1
Grade C: 0
Grade F: 1
Student with Roll No. 2 is deleted.
Student with Roll No. 1 is deleted.
Q5) Write a C++ program to initialize and display students’ data by using all three types of constructors.
Member variables:
string name;
int age;
float marks[3];
Member functions:
Default constructor // Make default value as follows (name:”unknown”, age = 0, marks = {0,0,0})
Parameterized constructor // Initialize variable with data
Copy constructor // Copy data of one to another object
Display() // Member function to display the details of students
Destructor // automatically called (print message about object destruction)
In Main Section:
Create array of objects to store details of multiple students (will call default constructor)
Display the data of all students
Call parameterized constructor with arguments
Display the data of all students
Copy objects of students to other objects
Display the data of all students
CODE :
#include <iostream>
#include <string>
public:
Student() {
name = "unknown";
age = 0;
for (int i = 0; i < 3; i++)
marks[i] = 0.0;
}
Student(string n, int a, float m1, float m2, float m3) {
name = n;
age = a;
marks[0] = m1;
marks[1] = m2;
marks[2] = m3;
}
void display() {
cout << "\nName: " << name;
cout << "\nAge: " << age;
cout << "\nMarks: ";
for (int i = 0; i < 3; i++)
cout << marks[i] << " ";
cout << endl;
}
~Student() {
cout << "Destructor called for student: " << name << endl;
}
};
int main() {
const int SIZE = 2;
s1.display();
s2.display();
copy1.display();
copy2.display();
cout << "\n--- Program End ---\n";
return 0;
}
OUTPUT :
--- Creating students using default constructor ---
Student 1 details:
Name: unknown
Age: 0
Marks: 0 0 0
Student 2 details:
Name: unknown
Age: 0
Marks: 0 0 0
Name: Bob
Age: 19
Marks: 70 75 72.5
Define a class complex that has two private data members: real and imag for storing the real and
imaginary parts of a complex number.
Include:
A constructor to initialize the complex number (with default values of 0 for both parts).
A member function input() to take input from the user for the real and imaginary parts.
A member function display() to print the complex number in the standard form (a + bi or a - bi).
CODE :
#include <iostream>
using namespace std;
class complex {
private:
int real;
int imag;
public:
complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
void input() {
cout << "Enter real and imaginary parts: ";
cin >> real >> imag;
}
void display() const {
cout << real;
if (imag >= 0) {
cout << " + " << imag << "i" << endl;
} else {
cout << " - " << -imag << "i" << endl;
}
}
int main() {
complex c1, c2;
cout << "Enter the 1st complex number:\n";
c1.input();
cout << "Enter the 2nd complex number:\n";
c2.input();
return 0;
}
OUTPUT :
Enter the 1st complex number:
Enter real and imaginary parts: 2 9
Enter the 2nd complex number:
Enter real and imaginary parts: 6 1
Addition: 8 + 10i
Subtraction: -4 + 8i
Multiplication: 3 + 56i
Q7) Design a C++ program for a library management system to manage book borrowings.
Fine Calculation:
No fine for the first 7 days.
₹2 per day after 7 days.
Task:
Take input for the student's details and borrowed book information.
Display user details, borrowed book name, and fine (if applicable).
In Main Function:
1) Create one object of Base Class (Call parameterized constructor)
2) Call display function and calculate fine inside display()
3) Call returnBook();
4) Call display();
CODE :
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
int userID;
public:
Person(string n, int id) {
name = n;
userID = id;
}
public:
Student(string n, int id, string book, int days)
: Person(n, id) {
bookBorrowed = book;
daysBorrowed = days;
}
int fine = 0;
if (daysBorrowed > 7) {
fine = (daysBorrowed - 7) * 2;
}
cout << "Fine: ₹" << fine << endl;
}
void returnBook() {
cout << "\nReturning book: " << bookBorrowed << endl;
bookBorrowed = "";
daysBorrowed = 0;
}
};
int main() {
string name, book;
int userID, days;
s.returnBook();
return 0;
}
OUTPUT :
Enter student name: Aryan Mehta
Enter user ID: 202
Enter book name: Data Structures in C++
Enter number of days the book is borrowed: 12
In Main Function:
1) Create two objects of Complex class and initialize data using parameterized constructor
2) Perform the mentioned operations
CODE :
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
private:
int real;
int imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
int main() {
Complex c1(7, 5);
Complex c2(2, 3);
return 0;
}
OUTPUT :
First Complex Number: 7 + 5i
Second Complex Number: 2 + 3i
Addition: 9 + 8i
Subtraction: 5 + 2i
Multiplication: 1 + 31i
Division: 2 + -1i
Modulus (Real % Real, Imag % Imag): 1 + 2i
Q9) Area-Based Shape Sorting and Visualization
Objective:
Use runtime polymorphism to design a system that:
Stores various types of shapes (Circle, Rectangle, Triangle, etc.).
Calculates area and perimeter dynamically using virtual functions.
Sorts shapes by area.
Problem Statement:
Design a base class Shape with the following:
Pure virtual functions:
o double area()
o double perimeter()
o void display()
CODE :
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual void display() const = 0;
virtual ~Shape() {}
};
int main() {
vector<Shape*> shapes;
int choice;
do {
cout << "\nMenu:\n1. Add Circle\n2. Add Rectangle\n3. Add Triangle\n4.
Display All\n5. Sort by Area\n0. Exit\nChoice: ";
cin >> choice;
if (choice == 1) {
double r;
cout << "Enter radius: ";
cin >> r;
shapes.push_back(new Circle(r));
} else if (choice == 2) {
double l, w;
cout << "Enter length and width: ";
cin >> l >> w;
shapes.push_back(new Rectangle(l, w));
} else if (choice == 3) {
double a, b, c;
cout << "Enter sides a, b, c: ";
cin >> a >> b >> c;
if (a + b > c && a + c > b && b + c > a) {
shapes.push_back(new Triangle(a, b, c));
} else {
cout << "Invalid triangle sides!" << endl;
}
} else if (choice == 4) {
cout << "\nDisplaying all shapes:\n";
for (const auto& shape : shapes) {
shape->display();
}
} else if (choice == 5) {
sort(shapes.begin(), shapes.end(), compareByAreaDesc);
cout << "\nShapes sorted by descending area:\n";
for (const auto& shape : shapes) {
shape->display();
}
}
return 0;
}
OUTPUT :
Menu:
1. Add Circle
2. Add Rectangle
3. Add Triangle
4. Display All
5. Sort by Area
0. Exit
Choice: 1
Enter radius: 4
Menu:
1. Add Circle
2. Add Rectangle
3. Add Triangle
4. Display All
5. Sort by Area
0. Exit
Choice: 2
Enter length and width: 4 8
Menu:
1. Add Circle
2. Add Rectangle
3. Add Triangle
4. Display All
5. Sort by Area
0. Exit
Choice: 3
Enter sides a, b, c: 3 4 5
Menu:
1. Add Circle
2. Add Rectangle
3. Add Triangle
4. Display All
5. Sort by Area
0. Exit
Choice: 4
Menu:
1. Add Circle
2. Add Rectangle
3. Add Triangle
4. Display All
5. Sort by Area
0. Exit
Choice: 5
Menu:
1. Add Circle
2. Add Rectangle
3. Add Triangle
4. Display All
5. Sort by Area
0. Exit
Choice: 0