constructor & destructor

Please help me...

The question is...

Extend the above book class so that book class also contains publisher name, author name and book name, now create three more constructor in the new book class, so that we can create new object when:
a)we have all the information about a book.
b)we don't know the publisher's name and number of pages in the book.
c)we don't have any information about the book ( default constructor )

#include <iostream.h>

//using namespace std

class Book
{
int PageCount;
int CurrentPage;

public:
// Constructor
Book (int NumPages);

// Destructor
~Book ()
{
cout<<"I completed this book\n";
}
void bookMark(int PageNumber);
int getBookMark( void );
};

Book::Book(int NumPages)
{
PageCount=(NumPages);
cout<<"I have started reading a new book\n";
}

void Book::bookMark(int PageNumber)
{
CurrentPage=PageNumber;
}

int Book::getBookMark(void)
{
return CurrentPage;
}

int main ()
{
Book twilight(498) ;
twilight.bookMark(56);
return 0;
}
So the teacher apparently wants you to create 3 constructors. As of right now you only have one constructor and it is "overloaded"
1
2
3
4
5
Book::Book(int NumPages)
{
  PageCount=(NumPages);
  cout<<"I have started reading a new book\n";
}


another one you could include would be a default constructor:
1
2
3
Book::Book() : PageCount( 0 ), CurrentPage( 0 )
{
}


The part ": PageCount( 0 ), CurrentPage( 0 )" is called a initialization list and it is important that the variables listed there are in the order in which they are declared in the class:
1
2
3
4
class Book
{
int PageCount;
int CurrentPage;


edit: just to clarify, you can sort of think of the : PageCount( 0 ), CurrentPage( 0 ) as
1
2
PageCount = 0;
CurrentPage = 0;

if that helps...
Another constructor you could use would be a copy constructor. Google it! Also google default constructor and initialization list.

If you're too lazy to google that stuff or just don't have any interest you should reconsider your... uh... major
Last edited on
Topic archived. No new replies allowed.