Library Management System Code 1

PHOTO EMBED

Wed May 15 2024 17:30:46 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #c++

#include <iostream>

using namespace std;

class Book{
    int BookID;
    string Title;
    string Author;
    double price;
    static int numOfBooks;
    
    public:
    //Parameter Constructor
    Book(int BookID, string Title, string Author, double price){
        this->BookID = BookID;
        this->Title = Title;
        this->Author = Author;
        this->price = price;
        numOfBooks ++;
    }
    int getBookID(){
        return BookID;
    }
    string getTitle(){
        return Title;
    }
    string getAuthor(){
        return Author;
    }
    double getPrice(){
        return price;
    }
    //Static Function
   static int getTotalNumOfBooks(){
        return numOfBooks;
    }
};
int Book::numOfBooks = 0;

class Library{
    Book *book;
    string name;
    string address;
    
    public:
    Library(string name, string address){
        this->name = name;
        this->address = address;
    }
    
    void setBook(Book *book){
        this->book = book;
    }
    Book* getBook(){
        return book;
    }
    string getName(){
        return name;
    }
    void setName(string name){
        this->name = name;
    }
    string getAddress(){
        return address;
    }
    void setAddress(string address){
        this->address = address;
    }
};

//Class User
class Users{
    
    public:
    string name;
    string address;
    string email;
    int phoneNum;
    
    Users(string n, string addre,string em, int pN){
        name = n;
        address = addre;
        email = em;
        phoneNum = pN;
    }
    
    //Virtual Function & Overiding function
    virtual void display(){
    }
    
};
//Derived Class from Base class User
class Student: public Users{
    
    int studentID;
    public:
    Student(int studentID, string name, string address, string email, int phoneNum):Users(name, address, email, phoneNum){
        this->studentID = studentID;
    }
    
    int getStudentID(){
        return studentID;
    }
    
    //Function Overloading, Same Name but different arguments.
    void print(string name){
        cout<<"student Name: "<<name<<endl;
    }
    void print(string name, int studentID){
        cout<<"Student Name: "<<name<<endl;
        cout<<"Student ID: "<<studentID<<endl;
    }
    //Default arguments
    void print(string name, string email, int studentID = 1111){
        cout<<"Student Name: "<<name<<endl;
        cout<<"Student Email: "<<email<<endl;
        cout<<"Student ID: "<<studentID<<endl;
    }
    
    void display(){
        cout<<"\n_____Student Info:____ "<<endl;
        cout<<"ID: "<<studentID<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Address: "<<address<<endl;
        cout<<"Email: "<<email<<endl;
        cout<<"Phone Number: "<<phoneNum<<endl;
    }
    
    //Friend Function
    friend void globalFunction(Student &stud);
};
class Staff: public Users{
    int staffID;
    public:
    Staff(int staffID, string name, string address, string email, int phoneNum):Users(name, address, email, phoneNum){
        this->staffID = staffID;
    }
    int getStaffID(){
        return staffID;
    }
    
    void display(){
        cout<<"\n______Staff Info:____ "<<endl;
        cout<<"ID: "<<staffID<<endl;
        cout<<"Name: "<<name<<endl;
        cout<<"Address: "<<address<<endl;
        cout<<"Email: "<<email<<endl;
        cout<<"Phone Number: "<<phoneNum<<endl;
    }
    friend void globalFunction(Staff &staf);
};

//Friend Function implementation
void globalFunction(Student &stud, Staff &staf){
    cout<<"\nAccessing Student ID: "<<stud.getStudentID()<<endl;
    cout<<"Accessing Staff ID: "<<staf.getStaffID()<<endl;
}

int main() {

    cout<<"_____Library Management System._____"<<endl;
    
    Library lib("University Library","Uskudar");
    cout<<"\n____Library Info____"<<endl;
    cout<<"Name: "<<lib.getName()<<endl;
    cout<<"Address: "<<lib.getAddress()<<endl;
    
    lib.setBook(new Book(1, "Java", "James", 20.99));
    cout<<"____Book Info____"<<endl;
    cout<<"Book ID: "<<lib.getBook()->getBookID()<<endl;
    cout<<"Book Title: "<<lib.getBook()->getTitle()<<endl;
    cout<<"Book Author: "<<lib.getBook()->getAuthor()<<endl;
    cout<<"Book Price: "<<lib.getBook()->getPrice()<<endl;
    //Calling static function in the Book class
    cout<<"Total Books in the Library are: "<<Book::getTotalNumOfBooks()<<endl;
    
    lib.setBook(new Book(2, "Math", "Mike", 24.99));
    cout<<"____Book Info____"<<endl;
    cout<<"Book ID: "<<lib.getBook()->getBookID()<<endl;
    cout<<"Book Title: "<<lib.getBook()->getTitle()<<endl;
    cout<<"Book Author: "<<lib.getBook()->getAuthor()<<endl;
    cout<<"Book Price: "<<lib.getBook()->getPrice()<<endl;
    
    cout<<"Total Books in the Library are: "<<Book::getTotalNumOfBooks()<<endl;
    
    Student s1(2023, "Mohamed","Istanbul","Student@gmail.com", 11111);
    Users *user;
    user = & s1;
    user->display();
    
    Staff stff(3034,"Staff1","Istnabul","Staff@gmail.com", 9999);
    user = &stff;
    user->display();
    
    //Friend Function
    globalFunction(s1,stff);
    
    //Calling Overloading Function in the Student class
    cout<<"_____"<<endl;
    s1.print("Musa");
    s1.print("Musa",5555);
    //Calling default arguments in the student class
    s1.print("Yahya", "yahya@emial.com");

    return 0;
}
___________________________________________________________________________
//OUTPUT:

_____Library Management System._____

____Library Info____
Name: University Library
Address: Uskudar
____Book Info____
Book ID: 1
Book Title: Java
Book Author: James
Book Price: 20.99
Total Books in the Library are: 1
____Book Info____
Book ID: 2
Book Title: Math
Book Author: Mike
Book Price: 24.99
Total Books in the Library are: 2

_____Student Info:____ 
ID: 2023
Name: Mohamed
Address: Istanbul
Email: Student@gmail.com
Phone Number: 11111

______Staff Info:____ 
ID: 3034
Name: Staff1
Address: Istnabul
Email: Staff@gmail.com
Phone Number: 9999

Accessing Student ID: 2023
Accessing Staff ID: 3034
_____
student Name: Musa
Student Name: Musa
Student ID: 5555
Student Name: Yahya
Student Email: yahya@emial.com
Student ID: 1111
content_copyCOPY