Pages

E-mail us at prgms4starters@gmail.com for any queries or to share your programs. Please mention the program definition in the subject line.

In case you need help in finishing a program, do mention "incomplete pgm" in the subject line. Our team of experts will do their best to help you.

Click the link to follow this page for updates and reminders: FOLLOW THIS SITE

You can also visit our Facebook page by clicking HERE

**don't forget to participate in the polls at the bottom of the page

Friday, November 5, 2010

Library Management System using Virtual Functions in C++

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

int isbn2,index;
int search(int isbn);
int nstaff=0,nstud=0,bkid=0;
class library
{
public:
virtual int returnbook(){return 0;}
virtual int takebook(){return 0;}
virtual void addbook(){}
virtual void addstaff(){}
virtual void adds(){}
virtual int isb(){return 0;}
virtual string str(){return"";}
virtual int sto(){return 0;}
};

library *stud[100],*sta[100],*bks[100];

class library_store:public library
{
public:
string bookname;
int isbn;
int stock;
library_store():stock(0)
{ }

int isb(){return isbn;}
string str(){return bookname;}
int sto(){ return stock;}

int returnbook()
{
stock++;
return 1;
}
int takebook()
{
if(stock>0)
{
stock--;
return 1;
}
else
{
cout<<"\nThe books are not available!!!";
return -1;
}
}
void addbook()
{
cout<<"Enter name of the book: ";
cin>>bookname;
cout<<"Enter book number: ";
cin>>isbn;
cout<<"Enter number of books available: ";
cin>>stock;
}

};

class staff:public library
{
public:
string name;
int bookstaken;


int returnbook()
{
int days;
cout<<"\nEnter book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nEnter the number of days since the book was borrowed: ";
cin>>days;
cout<<"\nExtra fees to be paid: Rs."<<(days-10)*1;
return bks[index]->returnbook();
}
else
return -1;
}
int takebook()
{
cout<<"\nEnter Book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nPlease return the book within 10 days!!!";
return bks[index]->takebook();
}
else
return -1;
}
void addstaff()
{
cout<<"\nEnter the name of the staff:";
cin>>name;
}
};

class students:public library
{
public:
string name;

int returnbook()
{
int days;
cout<<"\nEnter book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nEnter the number of days since the book was borrowed: ";
cin>>days;
cout<<"\nExtra fees to be paid: Rs."<<(days-5)*1;
return bks[index]->returnbook();
}
else
return -1;
}
int takebook()
{
cout<<"\nEnter the book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nPlease return the book within 10 days!!!";
return bks[index]->takebook();
}
else
return -1;
}
void adds()
{
cout<<"\nEnter the name of the student:";
cin>>name;
}
};

void display()
{
cout<<"\nThe available books are:\n\nISBN\t-\tName\t-\tStock\n";
for(int i=0;i<bkid;i++)
{
cout<<"\n"<<bks[i]->isb()<<"\t-\t"<<bks[i]->str()<<"\t-\t"<<bks[i]->sto();
}
}

void staf()
{
int ch,id,rep=1;
system("cls");

cout<<"\nEnter staff library number: ";
cin>>id;
id--;
while(rep==1)
{
system("cls");
cout<<"\nSTAFF\n-----\n";
cout<<"\n1.Display books\n2.Take book\n3.Return book\n4.Go to main menu\n";
cin>>ch;
switch(ch)
{
case 1:
display();
break;
case 2:
if(sta[id]->takebook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...";
break;
case 3:
if(sta[id]->returnbook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...\n";
break;
case 4:
rep=0;
break;
}
getch();
}
}

void student()
{
int ch,id,cont=1;
system("cls");
cout<<"\nSTUDENT\n--------\n";
cout<<"Enter library number: ";
cin>>id;
id--;
while(cont==1)
{
system("cls");
cout<<"\n1.Display books\n2.Take books\n3.Return book\n4.Go to main menu\n";
cin>>ch;
switch(ch)
{
case 1: display();
break;

case 2: if(stud[id]->takebook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...\n";
break;

case 3: if(stud[id]->returnbook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...\n";
break;

case 4: cont=0;
break;
}
getch();
}
}

void admin()
{
int ch,rep=1;
system("cls");
while(rep==1)
{
system("cls");
cout<<"\nDATABASE\n---------\n\n1.Add staff\n2.Add student\n3.Add books\n4.Display\n5.Exit\n";
cin>>ch;
switch(ch)
{
case 1: sta[nstaff]=new staff;
sta[nstaff]->addstaff();
nstaff++;
cout<<"\nThe Library number is: "<<nstaff;
break;

case 2: stud[nstud]=new students;
stud[nstud]->adds();
nstud++;
cout<<"\nThe Library number is: "<<nstud;
break;

case 3: bks[bkid]=new library_store;
bks[bkid]->addbook();
bkid++;
break;

case 4: display();
break;

case 5: rep=0;
break;
}
cout<<"\nPress Enter to continue:";
getch();
}
}

int search(int isbn)
{
for(int i=0;i<bkid;i++)
{
if(bks[i]->isb()==isbn)
return i;
}
cout<<"No book found!!!";
return -1;
}

void main()
{
int choice,cont=1;
while(cont==1)
{
cout<<"\n1.Administrator\n2.Staff\n3.Student\n4.Exit\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: admin();
break;

case 2: staf();
break;

case 3: student();
break;

case 4: cont=0;
break;

default: cout<<"\n\nInvalid Entry!!";
}
}
}

No comments:

Post a Comment

We have noticed that 20% of our visitors are still using internet explorer. For a better and faster surfing experience we recommend that you switch to either Google Chrome or Mozilla Firefox. We cannot provide you the downloads but here are the links to sites where you can download them from

Google Chrome - click here to go to site

Mozilla Firefox - click here to go to site

Where are you from

Do you think this site was helpful

Should we put up more C or C++ programs