Need help with the following c++ lab project about Polymorphism Polymorphism Lab
ID: 3838280 • Letter: N
Question
Need help with the following c++ lab project about Polymorphism
Polymorphism Lab (Library Holdings) In this assignment you will learn how to use inheritance and virtual functions by designing a set of classes to represent library holdings. Our library has two kinds of items: books and recordings. Each item has a title and an integer call number In addition, books have authors, and recordings have performers and formats In addition to constructors and destructors, each holding WAV, FF). (MP3 function that displays its data to an ostream object should have a pr (such as cout) that the user specifies You should begin by designing a Holding class that contains all the data common to books and recordings. It should also have a pure virtual print function. Then design the Book and Recording classes to add the details necessary for those types of items and to implement the print function. Note that Holding will be an abstract base class, since it will have a pure virtual function. This makes sense, since there are no generic holdings, only books and recordings accept classes should have both copy constructors and constructors that to the individual data fields as arguments. Although it may seem natural have a zero-argument default constructor that the user and reads in the data fields, doing I/O from within a constructor is probably unwise. If the constructor did class implementation would be tied to a particular style of getting data. Applications that use this class may read input from a which the prompt messages would be inappropriate, or they may a window- based interface that does not support the cin and cout I/O stream objects. Since constructors are such a basic part of the operation of a class, it is best to let the user decide where their input data comes from Once you have written the code to implement the Holding, Book, and Recording classes, you should write a simple program to test them. This program should declare an array of five pointers to Holding objects and then repeatedly call a function that creates a new Holding. This function asks the user which kind of Holding is to be entered and then inputs the data needed to a Book or a Recording. A new object of the appropriate class i create either created with this data, and the resulting pointer is returned. a Book pointer or a Note that even though the function always returns either Recording pointer, the function can be declared to return a Holding pointer, and the pointer to the newly created object can be returned without conversion. This pointer is then stored in the array and the function is called again until all Page 504 Focus on Object-oriented Programming with C++Explanation / Answer
Solution:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Holding{
protected:
string tit;
int callNo;
public:
Holding(string title, int Num);
Holding();
Holding(const Holding &);
virtual ~Holding();
virtual void print(ostream &dout);
};
class Book : public Holding {
protected:
string auth;
public:
Book(string title, string author, int Num);
Book(const Book &);
void print(ostream &);
};
class Recording : public Holding {
protected:
string perf;
string form;
public:
Recording(string title, string performer,string format, int Num);
Recording(const Recording &);
~Recording();
void print(ostream &);
};
Holding::Holding(string title, int Num){
tit=title;
callNo=Num;
}
Holding::~Holding(){
}
void Holding::print(ostream& dout){
cout << tit << '"' << " " << callNo;
}
Book::Book(string title, string author, int Num):Holding(title,Num)
{
auth=author;
}
void Book::print(ostream& dout){
cout <<"Book: " << auth << endl;
}
Recording::Recording(string title, string performer, string
format,int Num) : Holding(title, Num) {
perf = performer;
form = format;
}
Recording::~Recording(){
}
void Recording::print(ostream &dout){
dout << "RECORDING: " << form << callNo << endl;
}
int main(){
ofstream o;
ostream &dout=std::cout;
o.open("inp.txt");
Holding* hPtr[100];
int Num;
char cho;
std::string t;
std::string au;
std::string f;
std::string p;
for (int i=0;i<5;i++){
cout << "Enter holdings to be stored in a list: " << endl;
cout << "Enter B for book, R for recordings: ";
cin >> cho;
std::cin.ignore();
if (ch == 'b' || ch == 'B') {
cout << "Enter book tit: ";
std::getline(std::cin, t);
cout << "Enter book auth: ";
std::getline(std::cin, a);
cout << "Enter call number: ";
cin >> Num;
cout<<Num;
Book *b= new Book(t,a,Num);
hPtr[i]=(Holding *)b;
} else if (ch == 'r' || ch == 'R') {
cout << "Enter recording tit: ";
std::getline(std::cin, t);
cout << "Enter perf: ";
std::getline(std::cin, p);
cout << "Enter form: (L)P, (C)assette, (R)eel_to_reel, (D)isk: ";
std::getline(std::cin, f);
if (f.compare("m") == 0 || f.compare("M") == 0)
f = "MP3";
if (f.compare("W") == 0 || f.compare("w") == 0)
f = "AV";
if (f.compare("A") == 0 || f.compare("a") == 0)
f = "IFF";
cout << "Enter recording number: ";
cin >> Num;
hPtr[i]= new Recording(t,p,f,Num);
} else
cout << "else called (testing)"<< endl;
}
for (int i=0;i<5;i++){
hPtr[i]->print(dout);
}
o.close();
system("pause");
return 0;
}
Output:
Enter holdings to be stored in a list:
Enter B for book, R for recordings: B
Enter book tit: sss
Enter book auth: www
Enter call number: 123
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.