COP 3014 Lab 09 Use the classes LibMat, Book, and AudioBook in this assignment.
ID: 652740 • Letter: C
Question
COP 3014
Lab 09
Use the classes LibMat, Book, and AudioBook in this assignment.
Please create a new class, Magazine, derived from LibMat and place
it in a new header file called magazine.h
Magazine should have two private data members: _title and _editor,
and two public methods: title() and editor(), and a friend operator <<().
Please modify the all .h files by adding
#ifndef XXXX
#define XXXX
. . .
#endif XXXX
to each file as we discussed in class.
The XXXX is usually the name of the header file where we replace
a dot for an underscore. For example, in libmat.h, XXXX could be LIBMAT_H
#ifndef checks to make sure that XXXX has not been
defined, defines it, and then includes the rest of the .h
file until the #endif.
If we happen to include the same .h file again (directly or
indirectly) in the same .cpp file, the effects of the
second #include are ignored becase XXXX has already been defined.
Use lab09.cpp to test your code. Note that we included some .h files
more than once. The purpose is to test your correct usage of #ifndef
The output from your solution should be similar to lab09.out
Please submit a zip file containing ONLY the .h files.
This assignment is due on the BlackBoard one week after it is assigned.
No late assignments.
Please
- use braces around any block
- use blocks as we did in our class examples
- indent your code as we have used indentations in our examples
- comment your code; do not over comment your code
Penalties:
o -2 for each missing block
o -2 for each missing indentation
o no credit if your cpp file does not compile or does not run
audiobook.h
#include "book.h"
class AudioBook : public Book {
public:
AudioBook(const string &title,
const string &author,
const string &narrator);
virtual ~AudioBook();
virtual void print() const;
const string &narrator() const;
protected:
string _narrator;
};
inline AudioBook::AudioBook(const string &title,
const string &author,
const string &narrator)
: Book(title, author),
_narrator(narrator)
{
cout << "AudioBook::AudioBook(" << _title << ", "
<< _author << ", " << _narrator
<< ") constructor ";
}
inline AudioBook::~AudioBook()
{
cout << "AudioBook::~AudioBook() destructor ";
}
inline void AudioBook::print() const
{
cout << " AudioBook::print() -- AudioBook object "
<< " title: " << _title << ' '
<< " author: " << _author << ' '
<< " narrator: " << _narrator << ' ';
}
inline const string &AudioBook::narrator() const
{
return _narrator;
}
book.h
#include <string>
#include "libmat.h"
class Book : public LibMat {
public:
Book(const string &title,
const string &author);
virtual ~Book();
virtual void print() const;
const string &title() const;
const string &author() const;
protected:
string _title;
string _author;
};
inline Book::Book(const string &title,
const string &author)
: _title(title), _author(author)
{
cout << "Book::Book(" << _title << ", "
<< _author << ") constructor ";
}
inline Book::~Book()
{
cout << "Book::~Book() destructor ";
}
inline void Book::print() const
{
cout << "Book::print() -- Book object "
<< " title: " << _title << ' '
<< " author: " << _author << ' ';
}
inline const string &Book::title() const
{
return _title;
}
inline const string &Book::author() const
{
return _author;
}
lab09.cpp
#include "audiobook.h"
#include "book.h"
#include "libmat.h"
#include "audiobook.h"
#include "magazine.h"
#include "libmat.h"
#include "book.h"
void print( const LibMat & );
int main()
{
Book sc( "Effective C++", "Scott Meyers" );
print( sc );
Magazine ym( "Essential C++", "your-cpp-magazine" );
cout << ym;
AudioBook sl( "Essential C++", "Stan Lippman", "Tom Brokaw" );
print( sl );
Magazine sk( "Beginning Shorinji Kempo", "your-sport-magazine" );
cout << sk;
return 0;
}
void print( const LibMat &mat )
{
cout << "global print() ";
mat.print();
}
libmat.h
#ifndef LIBMAT_H
#define LIBMAT_H
#include <iostream>
using namespace std;
class LibMat {
public:
LibMat();
virtual ~LibMat();
virtual void print() const;
};
inline LibMat::LibMat()
{
cout << " LibMat::LibMat() default constructor ";
}
inline LibMat::~LibMat()
{
cout << "LibMat::~LibMat() destructor ";
}
inline void LibMat::print() const
{
cout << "LibMat::print() -- LibMat object ";
}
#endif
Explanation / Answer
magazine.h
#ifndef MAGAZINE_H
#define MAGAZINE_H
#include <string>
#include "libmat.h"
class Magazine : public LibMat {
public:
Magazine(const string &title,
const string &editor);
virtual ~magazine();
const string &title() const;
const string &editor() const;
friend ostream& operator<<(ostream& os, const Magazine &m);
protected:
string _title;
string _editor;
};
ostream& operator<<(ostream& os, const Magzine &m)
{
os << m._title << " " << m._editor ;
return os;
}
inline Magazine::Magazine(const string &title,
const string &editor)
: _title(title), _editor(editor)
{
cout << "Magazine::Magazine(" << _title << ", "
<< _editor << ") constructor ";
}
inline Magazine::~Magazine()
{
cout << "Magazine::~Magazine() destructor ";
}
inline const string &Magazine::title() const
{
return _title;
}
inline const string &Magazine::editor() const
{
return _editor;
}
#endif
libmat.h
#ifndef LIBMAT_H
#define LIBMAT_H
#include <iostream>
using namespace std;
class LibMat {
public:
LibMat();
virtual ~LibMat();
virtual void print() const;
};
inline LibMat::LibMat()
{
cout << " LibMat::LibMat() default constructor ";
}
inline LibMat::~LibMat()
{
cout << "LibMat::~LibMat() destructor ";
}
inline void LibMat::print() const
{
cout << "LibMat::print() -- LibMat object ";
}
#endif
book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
#include "libmat.h"
class Book : public LibMat {
public:
Book(const string &title,
const string &author);
virtual ~Book();
virtual void print() const;
const string &title() const;
const string &author() const;
protected:
string _title;
string _author;
};
inline Book::Book(const string &title,
const string &author)
: _title(title), _author(author)
{
cout << "Book::Book(" << _title << ", "
<< _author << ") constructor ";
}
inline Book::~Book()
{
cout << "Book::~Book() destructor ";
}
inline void Book::print() const
{
cout << "Book::print() -- Book object "
<< " title: " << _title << ' '
<< " author: " << _author << ' ';
}
inline const string &Book::title() const
{
return _title;
}
inline const string &Book::author() const
{
return _author;
}
#endif
audiobook.h
#ifndef AUDIOBOOK_H
#define AUDIOBOOK_H
#include "book.h"
class AudioBook : public Book {
public:
AudioBook(const string &title,
const string &author,
const string &narrator);
virtual ~AudioBook();
virtual void print() const;
const string &narrator() const;
protected:
string _narrator;
};
inline AudioBook::AudioBook(const string &title,
const string &author,
const string &narrator)
: Book(title, author),
_narrator(narrator)
{
cout << "AudioBook::AudioBook(" << _title << ", "
<< _author << ", " << _narrator
<< ") constructor ";
}
inline AudioBook::~AudioBook()
{
cout << "AudioBook::~AudioBook() destructor ";
}
inline void AudioBook::print() const
{
cout << " AudioBook::print() -- AudioBook object "
<< " title: " << _title << ' '
<< " author: " << _author << ' '
<< " narrator: " << _narrator << ' ';
}
inline const string &AudioBook::narrator() const
{
return _narrator;
}
#endif