Hi, this is my first time on these boards, so please bare with me. Im in need of some help trying to get my program running correctly. Here is the assignment that i have to follow:
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 audio recordings. Each item has a title and an integer call number. In addition, books have authors, and recordings have performers and formats (LP, Cassette, Reel-to-Reel, or Compact Disk). In addition to constructors and destructors, each holding should have a print() function that displays its data to an ostream object (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.
All three classes should have both copy constructors and constructors that accept the individual data fields as arguments. Although it may seem natural to have a zero-argument default constructor that queries the user and reads in the data fields, doing I/O from within a constructor is probably unwise. If the constructor did I/O, the class implementation would be tied to a particular style of getting data. Applications that use this class may read input from a file, in which case the prompt messages would be inappropriate, or they may use 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 create either a Book or a Recording. A new object of the appropriate class is created with this data, and the resulting pointer is returned. Note that even though the function always returns either a Book pointer or a 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 five objects have been created. Now there is an array to five pointers to Holding objects, and each of these objects "knows" whether it is a Book or a Recording, although the program has no direct way of querying them to get this information. However, it can run through the array and call the virtual print() function for each object, which will then use the appropriate virtual function for printing itself out.
In short, i basically have to make 4 files that come together to form a library in which it will ask for holdings and then store it and then print out the holdings, whether its a book or record. Below is my code for my library, book, holding and record files. If anyone is interested in the errors im getting when im making the library, i can post it up. Sorry for such a big post for a first post. I dont know any other way to get my code out.
Code:
//library.cpp
#include <sstream>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "library.h"
#include <string>
Holding *getInfo();
using namespace std;
ofstream csis;
int main(){
csis.open("csis.dat");
Holding* holdPtr[5];
for (int i=0;i<5;i++){
holdPtr[i] = getInfo();
}
for (int i=0;i<5;i++){
holdPtr[i]->print();
}
csis.close();
}
Holding *getInfo(){
int callNumTemp;
char choice;
std::string titleTemp;
std::string authorTemp;
std::string formatTemp;
std::string performerTemp;
cout << "Enter holdings to be stored in a list: " << endl;
cout << "Enter B for book, R for recordings: ";
cin >> choice;
std::cin.ignore(); //need so NOT to skip title input...
if (choice == 'b' || choice == 'B') {
cout << "Enter book title: ";
std::getline(std::cin, titleTemp);
cout << "Enter book author: ";
std::getline(std::cin, authorTemp);
cout << "Enter call number: ";
cin >> callNumTemp;
return new Book(titleTemp,authorTemp,callNumTemp);
} else if (choice == 'r' || choice == 'R') {
cout << "Enter recording title: ";
std::getline(std::cin, titleTemp);
cout << "Enter performer: ";
std::getline(std::cin, performerTemp);
cout << "Enter format: (L)P, (C)assette, (R)eel_to_reel, (D)isk: ";
std::getline(std::cin, formatTemp);
if (formatTemp.compare("l") == 0 || formatTemp.compare("L") == 0)
formatTemp = "LP";
if (formatTemp.compare("c") == 0 || formatTemp.compare("C") == 0)
formatTemp = "Cassette";
if (formatTemp.compare("r") == 0 || formatTemp.compare("R") == 0)
formatTemp = "Reel_to_reel";
if (formatTemp.compare("d") == 0 || formatTemp.compare("D") == 0)
formatTemp = "Disk";
cout << "Enter recording number: ";
cin >> callNumTemp;
return new Recording(titleTemp,performerTemp,formatTemp,callNumTemp);
} else
cout << "else called (testing)"<< endl;
return NULL;
}
Code:
//library.h
#ifndef _LIBRARY_H
#define _LIBRARY_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "holding.h"
#include "book.h"
#include "record.h"
extern std::string titleTemp;
extern int callNumTemp;
extern char choice;
extern std::string authorTemp;
Holding* getInfo(int x);
#endif
Code:
//book.cpp
#include "book.h"
using namespace std;
Book::Book(std::string titleTemp, std::string authorTemp, int
callNumtemp) : Holding(title, callNumber) { //seperated for screen width
author = authorTemp;
}
Book::~Book(){
delete [] author;
}
void Book::print(/*ostream &out*/){
cout << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
}
Code:
//book.h
#ifndef _BOOK_H
#define _BOOK_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "holding.h"
class Book : public Holding {
protected:
public:
string author;
Book(string titleTemp, string authorTemp, int callNumTemp);
Book(const Book &);
~Book();
void print(ostream &);
#endif
Code:
//holding.cpp
#include "holding.h"
using namespace std;
extern ofstream csis;
Holding::Holding(std::string tempTitle, int callNumTemp){
title = tempTitle;
callNumber = callNumTemp;
}
Holding::~Holding(){
delete [] title;
}
Code:
//holding.h
#ifndef _HOLDING_H
#define _HOLDING_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
class Holding{
protected:
string title;
int callNumber;
public:
string title;
int callNumber;
Holding(string titleTemp, int callNumTemp);
Holding();
Holding(const Holding &);
virtual ~Holding();
virtual void print(ostream &) = 0;
~Holding();
};
#endif
Code:
//record.cpp
#include "record.h"
using namespace std;
Recording::Recording(std::string titleTemp, std::string performerTemp, std::string
formatTemp,int callNumtemp) : Holding(title, callNumber) { //seperated for screen width
performer = performerTemp;
format = formatTemp;
}
Recording::~Recording(){
delete [] author;
}
void Recording::print(ostream &out){
cout << "RECORDING: " << '\"' << title << '\"' << " " << performer << " " << format << callNumber << endl;
}
Code:
//record.h
#ifndef _RECORDING_H
#define _RECORDING_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "holding.h" //commented out above since holding.h has them.
class Recording : public Holding {
protected:
public:
std::string performer;
std::string format;
Recording(string titleTemp, string performerTemp,string formatTemp, int callNumTemp);
Book(const Book &);
~Recording();
void print(ostream &);
};
#endif