1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// classes defined here
#include <iostream>
#include <string>
class book
{
public:
//constructor
book(std::string Name, int pNum);
std::string getName(); //take in mem name
int getPageNum(); //take in mem pg numbers
void setPageNum(int pNum); //set
std::string m_Name;
int m_pNum;
~book();
};
class shelf : public book
//shelf inherits from class book
{
public:
shelf(std::string Name, int pNum);
/*
NOTE: std::string getName() and getPageNum() do not need to be included
in this definition as they are inherited behaviours
*/
//these are the new behaviours unique to shelf
std::string addBooks(std::string Name, int currentHeld, int BooksAdded);
//added as a string since the only output will be "book added to shelf"
//name of book added to shelf, number of books currently in shelf, total books to be added to shelf
~shelf();
std::string m_Name;
int m_Num;
};
//class functions defined here
book::book(std::string Name, int pNum)
: m_Name(Name),m_pNum(pNum)
{
}
book::~book()
{
//burn the books
}
void book::setPageNum(int NewpNum)
{
this->m_pNum = NewpNum;
}
int book::getPageNum()
{
return m_pNum;
}
std::string book::getName()
{
return m_Name;
}
shelf::shelf(std::string Name, int pNum)
: book(m_Name, m_Num)
{
}
shelf::~shelf()
{
//destructor
}
std::string shelf::addBooks(std::string Name, int currentHeld, int BooksAdded)
{
for(int i = 0; i < BooksAdded; i++)
{
currentHeld = currentHeld + 1;
if (currentHeld <= 10)
{
std::cout << book::getName() << " has been added to the self" << std::endl;
}
else
{
std::cout << "Well done. The shelf is too small and the books all fell!" << std::endl;
}
}
}
//main body
int main()
{
//declare all books in an array
book books[11] =
{
book("1984", 84),
book("Animal Farm", 190),
book("Harry Potter", 8),
book("LOTR", 300),
book("Flypaper", 40),
book("Great Expectations", 1000),
book("Wuthering Heights", 562),
book("Slaughterhouse 5", 5),
book("Mrs Dalloway",5),
book("The Mister Men", 5),
book("C++ for Dummies", 180)
};
int i = 0; //loop counter
int addNum = 11; //number of books to add to shelf
for (i = 0; i < 11; i++)
{
shelf::addBooks(books[i].getName, i, addNum);
}
return 0;
}
|