Completely Solved C, C++ Programs Assignment.




C++ program to create a class of library syatem.

Filed Under:

Problem Statement :
Create a class library member having fields memberid, membername, address, ph_no. Derive two classes Student(rollno, stream) and Faculty(facultyid, designation).Use only one function name display() to display all the object details. Use virtual functions and call the function using only the base class pointer.

Logical Approach :
The above program is done using an Abstract Data Type (ADT) called class. A class is a mechanism to create objects. We have used inheritance feature in this program. The mechanism of deriving a new class(derived class) from an old one(base class) is called inheritance. This program is based on hierarchical inheritance. In this program a base class named Library_member is created ,
which contains the following data members :
 memberid
 membername[]
 address[]
 phone_no[]
and following member functions :
 get_libdata() - Used to input all general information about a member.
 getdata() - Pure virtual function, will be redefined in derived classes.
 display() - Pure virtual function,will be redefined in derived classes.
As this class contains at least one pure virtual function it is called abstract base class. No object of this class can be instantiated in main function.
Student is a class that is derived from class Library_member and contains the following data member :
 roll_no
 stream[]
No new member function is defined in this class. The two pure virtual functions of the abstract base class Library_member are redefined in this class.

Faculty is another class derived from class Library_member which contains following data members :
 facultyid
 designation[]
Like Student class No new member function is defined in this class. The two pure virtual functions of the abstract base class Library_member are redefined in this class.

Algorithm :
Creating an ADT with name Library_member

Step 1 : Creating an ADT Library_member
Step 2 : Declaring protected data members
int acc_blnc; //Declaring an integer
char membername[]; //Declaring a character array
char address[]; //Declaring a character array
char phone_no[]; //Declaring a character array
Step 3 : Declaring public member functions
 get_libdata()
 virtual getdata()=0; //Pure virtual function
 virtual display()=0; //Pure virtual function

This function is used to input all general information about a member

get_libdata()
{
Read member id
Read member name
Read member address
Read phone number
}

Creating an ADT with name Student

Step 1 : Creating an ADT Student
Step 2 : Declaring private data members
int roll_no; //Declaring an integer
char stream[]; //Declaring a character array
Step 3 : Declaring public member functions
 getdata()
 display()

Redefinition of virtual function getdata() in ADT Student

getdata() //Pure virtual function redefined
{
Library_member::get_libdata();
//Read student information
Read roll_no
Read stream
}


Redefinition of virtual function display() in ADT Student

display()//Pure virtual function redefined
{
//Printing student information
Print memberid
Print membername
Print address
Print phone_no
Print roll_no
Print stream
}

Creating an ADT with name Faculty

Step 1 : Creating an ADT Faculty derived from abstract base class Library_member
Step 2 : Declaring private data members
int facultyid; //Declaring an integer
char designation[]; //Declaring a character array
Step 3 : Declaring public member functions
 getdata()
 display()

Redefinition of virtual function getdata() in ADT Faculty

getdata() //Pure virtual function redefined
{
Library_member::get_libdata();
//Read faculty information
Read Faculty Id
Read designation
}

Redefinition of virtual function display() in ADT Faculty

display() //Pure virtual function redefined
{
//Printing Faculty information
Print memberid
Print membername
Print address
Print phone_no
Print facultyid
Print designation
}

Main procedure

main()
{
Library_member* lib[9999];
n=0;
do
{
Read student or faculty(s/f) in ch
if(ch=='s')
lib[n]=new object of Student;
else
lib[n]=new object of Faculty;
lib[n]->getdata();
n=n+1;
Read user’s choice to enter another member in ch
}while(ch=='Y' OR ch=='y');
for( i=1 to n)
lib[i]->display();
}


Program listing :
//C++ program to implement run time polymorphism by using virtual functions

#include<iostream>
using namespace std;
//Create an abstract base class Library_member
class Library_member
{
protected:
int memberid;
char membername[40];
char address[60];
char phone_no[15];
public:
void get_libdata()
{
cout<<"\nEnter member id:";
cin>>memberid;
cout<<"\nEnter member name:";
cin.ignore();
cin.getline(membername,40);
cout<<"\nEnter member address:";
cin.getline(address,60);
cout<<"\nEnter phone number:";
cin.getline(phone_no,15,'\n');
}
virtual void getdata()=0;//Pure virtual function
virtual void display()=0;//Pure virtual function

};
//Create a class Student derived from the abstract base class Library_member
class Student:public Library_member
{
private:
int roll_no;
char stream[20];
public:
void getdata()//Pure virtual function redefined
{
Library_member::get_libdata();
cout<<"\n\nEnter student information=>";
cout<<"\nEnter roll number:";
cin>>roll_no;
cout<<"\nEnter stream:";
cin.ignore();
cin.getline(stream,20);
}
void display()//Pure virtual function redefined
{
cout<<"\n\nStudent information displayed\n------------------------------------------------";
cout<<"\nMember id:"<<memberid;
cout<<"\nName:"<<membername;
cout<<"\nAddress:"<<address;
cout<<"\nPhone Number:"<<phone_no;
cout<<"\nRoll Number:"<<roll_no;
cout<<"\nStream:"<<stream;
}
};
//Create a class Faculty derived from the abstract base class Library_member
class Faculty:public Library_member
{
private:
int facultyid;
char designation[25];
public:
void getdata()//Pure virtual function redefined
{
Library_member::get_libdata();
cout<<"\n\nEnter faculty information=>";
cout<<"\nEnter Faculty Id:";
cin>>facultyid;
cout<<"\nEnter designation:";
cin.ignore();
cin.getline(designation,25);
}
void display()//Pure virtual function redefined
{
cout<<"\n\nFaculty information displayed\n---------------------------------------------------";
cout<<"\nMember id:"<<memberid;
cout<<"\nName:"<<membername;
cout<<"\nAddress:"<<address;
cout<<"\nPhone Number:"<<phone_no;
cout<<"\nFaculty Id:"<<facultyid;
cout<<"\nDesignation:"<<designation;
}
};

int main()
{
Library_member* lib[9999];
int n=0;
char ch;
do
{
cout<<"Enter student or faculty(s/f):";
cin>>ch;
if(ch=='s')
lib[n]=new Student;
else
lib[n]=new Faculty;
lib[n++]->getdata();
cout<<"Do you want to enter another member(Y/N)?:";
cin>>ch;
}while(ch=='Y'||ch=='y');
for(int i=0;i<n;i++)
lib[i]->display();
return 0;
}


Output:
Enter student or faculty(s/f):f
Enter member id:1200
Enter member name:Abinash Guha
Enter member address:9,Bidhan Sarani,Kolkata-700006
Enter phone number:9876550943
Enter faculty information=>
Enter Faculty Id:201
Enter designation:12000

Do you want to enter another member(Y/N)?:Y
Enter student or faculty(s/f):s
Enter member id:1201
Enter member name:Ankita Kundu
Enter member address:43,Bagbazar street,Kolkata-700005
Enter phone number:(033)2574-0921
Enter student information=>
Enter roll number:176
Enter stream:Computer Science

Do you want to enter another member(Y/N)?:Y
Enter student or faculty(s/f):s
Enter member id:1202
Enter member name:Ranjan Das
Enter member address:29,Surya Sen Street,Kolkata-700003
Enter phone number:9763128890
Enter student information=>
Enter roll number:321
Enter stream:Philosophy

Do you want to enter another member(Y/N)?:y
Enter student or faculty(s/f):f
Enter member id:1203
Enter member name:Ashok Paul
Enter member address:90,Sovabazar Street,Kolkata-700006
Enter phone number:9087213455
Enter faculty information=>
Enter Faculty Id:298
Enter designation:16000

Do you want to enter another member(Y/N)?:n


Faculty information displayed
---------------------------------------------------
Member id:1200
Name:Abinash Guha
Address:9,Bidhan Sarani,Kolkata-700006
Phone Number:9876550943
Faculty Id:201
Designation:12000

Student information displayed
------------------------------------------------
Member id:1201
Name:Ankita Kundu
Address:43,Bagbazar street,Kolkata-700005
Phone Number:(033)2574-0921
Roll Number:176
Stream:Computer Science

Student information displayed
------------------------------------------------
Member id:1202
Name:Ranjan Das
Address:29,Surya Sen Street,Kolkata-700003
Phone Number:9763128890
Roll Number:321
Stream:Philosophy

Faculty information displayed
---------------------------------------------------
Member id:1203
Name:Ashok Paul
Address:90,Sovabazar Street,Kolkata-700006
Phone Number:9087213455
Faculty Id:298
Designation:16000


Discussions :
&#61548; Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

&#61548; All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.

&#61548; C++ strongly supports the concept of reusability. Inheritance is an way to reuse classes.

&#61548; There are many types or forms of inheritance- Single, multiple, multilevel, hierarchical and hybrid.

&#61548; In this program all derivation of derived classes from base class are made public.

&#61548; A virtual function equated to zero is called a pure virtual function. It is a function derived in a base class that has no definition relative to the base class. It only serves as a placeholder.

&#61548; C++ supports run time polymorphism with the help of virtual functions. In run time polymorphism,an appropriate member function is selected while the program is running. It is also called late or dynamic binding because the appropriate function is selected dynamically at run time.




Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments