Completely Solved C, C++ Programs Assignment.




C++ program to implement inherited class:book shop-sells books-video tapes

Filed Under:

Problem Statement :
Consider an example of book shop which sells books and video tapes. It is modeled by book and tape classes. These two classes are inherited from the base class called media. The media class has common data members such as title and publication. The book class has data members for storing a number of pages in a book and tape class has the playing time in a tape. Each class will have the member functions such as read ( ) and show ( ). In a base class, these members have to be defined as virtual functions. Write a program which models this class hierarchy and processes their objects using pointers to the base class only. (Use Virtual Functions and all classes must be template based (int and double data to be tested).

Logical Approach :
The above program has been done using an ADT (Abstract Data Type) called class. Class is a mechanism to create objects. We have also used inheritance feature in this program. Inheritance is the mechanism of deriving a new class (derived class) from an old one (base class). This program uses hierarchical inheritance. Generic class has also been used in this program i.e. the program has been generalized for Integer, Float and Double data types . In this program a base class called media is created. It contains the following data members :
1. title[]
2. publication
3. price
and following member functions :
• read() – pure virtual function, to be defined in derived classes.
• show() – pure virtual function, to be defined 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.

book is a class derived from media class and contains the following data members :
• pages
No new member functions are defined in this class. The two pure virtual functions of the abstract base class media are redefined in this class.

tape is another class derived from media class and contains the following data members :
• time
No new member functions are defined in this class. The two pure virtual functions of the abstract base class media are redefined in this class.

Algorithm :
Creating a template ADT with name media of type T

Step 1: Creating a template ADT media of type T
Step 2: Declaring protected data members
title //character array to store media title
publication //integer variable to store no of publications
price //T type variable to store price of media
Step 3: Declaring public member functions
• read() //pure virtual function
• show() //pure virtual function

Creating a template ADT with name book of type T

Step 1: Creating a template ADT book of type T
Step 2: Declaring private data members
pages //integer variable to store no of pages
Step 3: Declaring public member functions
 read()
 show()


Redefinition of virtual function read() in ADT book

read()
{
Read title
Read publication
Read price
Read pages
}

Redefinition of virtual function show() in ADT book

show()
{
Display title
Display publication
Display price
Display pages

}

Creating a template ADT with name tape of type T

Step 1: Creating a template ADT tape of type T
Step 2: Declaring private data members
time //T type variable to store play time
Step 3: Declaring public member functions
 read()
 show()

Redefinition of virtual function read() in ADT tape

read()
{
Read title
Read publication
Read price
Read time
}

Redefinition of virtual function show() in ADT tape

show()
{
Display title
Display publication
Display price
Display time
}

Main procedure
main()
{
media<double>*m[9999] //template class of type double
int n=0
do
{
Read choice in ch(‘b’ for book ‘t’ for tape)
if(ch='b')
m[n]=new book<int> //create an object of type book and store in m
else
m[n]=new tape<int> //create an object of type tape and store in m

m[n++]->read() /*call the respective overridden function read() using base class
pointer*/
Read user’s choice to enter another record in ch (Y/y for yes)
}while(ch='Y OR ch='y')
for(int i=1 to n)
m[i]->show() //call show function to display the records
n=0
do
{
Read choice in ch(‘b’ for book ‘t’ for tape)
if(ch='b')
m[n]=new book<double> //create an object of type book and store in m
else
m[n]=new tape<double> //create an object of type tape and store in m

m[n++]->read() /*call the respective overridden function read() using base class
pointer*/

Read user’s choice to enter another record in ch (Y/y for yes)
}while(ch='Y' OR ch='y')
for(int i=1 to n)
m[i]->show() //call show function to display the records

}

Program Listing :
//C++ program to create a book shop hierarchy
#include<iostream>
#include<cstring>

using namespace std;

//Creating a template class with name media of type T
template<class T>
class media
{
protected:
char title[50];
int publication;
T price;
public:
virtual void read()=0;
virtual void show()=0;
};
//Creating a template class with name book of type T
template<class T>
class book:public media<double>
{
private:
int pages;
public:
void read();
void show();
};
//Redefinition of virtual function read() in class book
template<class T>
void book<T>::read()
{
cout<<"Enter book title:";
cin.ignore();
cin.getline(title,50);
cout<<"Enter no. of publications:";
cin>>publication;
cout<<"Enter price of the book(in rupees):";
cin>>price;
cout<<"Enter no. of pages:";
cin>>pages;
}
//Redefinition of virtual function show() in class book
template<class T>
void book<T>::show()
{
cout<<"Book information"<<endl<<"------------------------------"<<endl;
cout<<"Book title:"<<title<<endl;
cout<<"No. of publications:"<<publication<<endl;
cout<<"Price:"<<price<<endl;
cout<<"No. of pages:"<<pages<<endl<<endl;
}
//Creating a template class with name tape of type T
template<class T>
class tape:public media<double>
{
private:
T time;
public:
void read();
void show();
};
//Redefinition of virtual function read() in class tape
template<class T>
void tape<T>::read()
{
cout<<"Enter title:";
cin.ignore();
cin.getline(title,50);
cout<<"Enter no. of publications:";
cin>>publication;
cout<<"Enter price(in rupees):";
cin>>price;
cout<<"Enter playing time(mins.):";
cin>>time;
}
//Redefinition of virtual function show() in class tape
template<class T>
void tape<T>::show()
{
cout<<"Tape information"<<endl<<"------------------------------"<<endl;
cout<<"Title:"<<title<<endl;
cout<<"No. of publications:"<<publication<<endl;
cout<<"Price:"<<price<<endl;
cout<<"Playing time:"<<time<<endl<<endl;
}
int main()
{
media<double>*m[9999];
int n=0;
char ch;
cout<<"Check for int data\n-------------------------------------------\n";
do
{
cout<<"Enter book or tape(b/t):";
cin>>ch;
if(ch=='b')
m[n]=new book<int>;
else
m[n]=new tape<int>;
m[n++]->read();
cout<<"Do you want to enter another record(Y/N)?:";
cin>>ch;
}while(ch=='Y'||ch=='y');
cout<<endl<<endl;
for(int i=0;i<n;i++)
m[i]->show();
cout<<"Check for double data\n-------------------------------------------\n";
n=0;
do
{
cout<<"Enter book or tape(b/t):";
cin>>ch;
if(ch=='b')
m[n]=new book<double>;
else
m[n]=new tape<double>;
m[n++]->read();
cout<<"Do you want to enter another record(Y/N)?:";
cin>>ch;
}while(ch=='Y'||ch=='y');
cout<<endl<<endl;
for(int i=0;i<n;i++)
m[i]->show();
return 0;
}





Output :
Check for int data
-------------------------------------------
Enter book or tape(b/t):b
Enter book title:Heidi
Enter no. of publications:50000
Enter price of the book(in rupees):340
Enter no. of pages:238
Do you want to enter another record(Y/N)?:y
Enter book or tape(b/t):t
Enter title:Computing Concepts
Enter no. of publications:200
Enter price(in rupees):190
Enter playing time(mins.):78
Do you want to enter another record(Y/N)?:n


Book information
------------------------------
Book title:Heidi
No. of publications:50000
Price:340
No. of pages:238

Tape information
------------------------------
Title:Computing Concepts
No. of publications:200
Price:190
Playing time:78

Check for double data
-------------------------------------------
Enter book or tape(b/t):t
Enter title:Learn English
Enter no. of publications:1000
Enter price(in rupees):189.75
Enter playing time(mins.):118.50
Do you want to enter another record(Y/N)?:n


Tape information
------------------------------
Title:Learn English
No. of publications:1000
Price:189.75
Playing time:118.5


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; The class template definition is very similar to an ordinary class definition except the prefix template<class T> and the use of type T. This prefix tells the compiler that we are going to declare a template and use T as a type name in the declaration. T may be substituted by any data type including the user defined types.

&#61548; A class created from a class template is called a template class. The syntax for defining an object of a template class is:
classname<type>objectname(argumentlist);




Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments