Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

C++ Classes and STL

Got an idea for a practice project / debug snippet / short-code contest / etc? Tell us about it here!

C++ Classes and STL

Postby codeseeker on Thu Feb 05, 2009 4:56 am

I just want to share this. This is a practice exercise that were given to us to gauge how we are doing so far in our class. The solution is to create a Class that has all the properties and methods to make the main code work. It requires the use of any STL container (map, vector, list etc). The DB should also have the capacity to be updated with new books with some user inputs (cin or reading a text file). It is not actually a DB (such as MySQL or Oracle) but a theoretical DB in memory. Tomorrow we will be dealing with file IO streams.

Code: Select all
int main()
{
   borrowerdb borrDB;
   bookdb bookDB;
   
   borrDB.add(333, "Bill", "Clinton" );
   borrDB.add(666, "George", "Bush" );
   borrDB.add(999, "Barack", "Obama");
   
   bookDB.add("AAA","Calculus","Leithold");
   bookDB.add("EEE","Algebra","Someone");
   bookDB.add("III","Java","Horstmann");
   bookDB.add("OOO","C++","Stroustrup");
   bookDB.add("UUU","Programming","Knuth");

   borrower *br;
   book *bk;

   br = borrDB.get(666);
   bk = bookDB.get("UUU");
   br->borrowbook( bk );
   br->printbooksborrowed();
}
codeseeker
 
Posts: 12
Joined: Mon Feb 02, 2009 9:30 am

Re: C++ Classes and STL

Postby C++ on Fri Feb 06, 2009 11:58 am

Here's an example of how it could be done. It's the first thing that popped in my head, so it might not be the best approach.

edit: Version 2
I forgot to implement book reservation.

Code: Select all
#include <map>
#include <vector>
#include <iostream>

using std::map;
using std::vector;
using std::ostream;
using std::cout;
using std::pair;

class borrower;

class book
{
    const char *isdn;
    const char *title;
    const char *author;
    borrower *isborrowed;
public:
    book(const char *_isdn, const char *_title, const char *_author) : isdn(_isdn), title(_title), author(_author), isborrowed(0) {}
    const char * getisdn() const {return isdn;}
    void setborrower(borrower *b) {isborrowed = b;}
    borrower * borrowed() {return isborrowed;}
    friend ostream & operator<<(ostream &o, book &b) {o << "Title: " << b.title << "\nAuthor: " << b.author << "\nisdn: " << b.isdn; return o;}
};

class borrower
{
    int id;
    const char *firstName;
    const char *lastName;
    vector<book *> books;
public:
    borrower(int _id, const char *_firstName, const char *_lastName) : id(_id), firstName(_firstName), lastName(_lastName) {}
    void borrowbook(book *b) {if (b) {if (!b->borrowed()) {books.push_back(b); b->setborrower(this);} else if (this == b->borrowed()) cout << "You already borrowed this book!\n"; else cout << "Someone else borrowed this book already.\n";}}
    void returnbook(book *b) {if (b) for (vector<book *>::iterator i = books.begin(); i < books.end(); ++i) if ((*i)->getisdn() == b->getisdn()) {books.erase(i); b->setborrower(0);}}
    void printbooksborrowed() {cout << "Books borrowed by: " << firstName << " " << lastName << "\n\n"; for (vector<book *>::iterator i = books.begin(); i != books.end(); ++i) cout << **i << "\n\n";}
};

class borrowerdb
{
    map<int, borrower *> borrowers;
public:
    ~borrowerdb() {for (map<int, borrower *>::iterator i = borrowers.begin(); i != borrowers.end(); ++i) delete i->second;}
    void add(int _id, const char *_firstName, const char *_lastName) {borrowers.insert(pair<int, borrower *>(_id, new borrower(_id, _firstName, _lastName)));}
    borrower * get(int _id) {map<int, borrower *>::iterator i = borrowers.find(_id); if (i != borrowers.end()) return borrowers[_id]; else return 0;}
};

class bookdb
{
    map<const char *, book *> books;
public:
    ~bookdb() {for (map<const char *, book *>::iterator i = books.begin(); i != books.end(); ++i) delete i->second;}
    void add(const char *_isdn, const char *_title, const char *_author) {books.insert(pair<const char *, book *>(_isdn, new book(_isdn, _title, _author)));}
    book * get(const char *_isdn) {map<const char *, book *>::iterator i = books.find(_isdn); if (i != books.end()) return books[_isdn]; else return 0;}
};


int main()
{
    borrowerdb borrDB;
    bookdb bookDB;

    borrDB.add(333, "Bill", "Clinton" );
    borrDB.add(666, "George", "Bush" );
    borrDB.add(999, "Barack", "Obama");

    bookDB.add("AAA","Calculus","Leithold");
    bookDB.add("EEE","Algebra","Someone");
    bookDB.add("III","Java","Horstmann");
    bookDB.add("OOO","C++","Stroustrup");
    bookDB.add("UUU","Programming","Knuth");

    borrower *br;
    book *bk;

    br = borrDB.get(666);
    bk = bookDB.get("UUU");
    br->borrowbook( bk );
    br->printbooksborrowed();
}
Last edited by C++ on Fri Feb 06, 2009 2:58 pm, edited 1 time in total.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: C++ Classes and STL

Postby codeseeker on Fri Feb 06, 2009 12:23 pm

Hey thanks! I'll post mine maybe sooner. I'm still halfway through my code.
With your code, the thing that's new to me is the ff.
friend ostream & operator<<(ostream &o, book &b) {o << "Title: " << b.title << "\nAuthor: " << b.author << "\nisdn: " << b.isdn; return o;}

I have used this & operator on our topic about copy constructor (deep copy) but never understood it fully.
codeseeker
 
Posts: 12
Joined: Mon Feb 02, 2009 9:30 am

Re: C++ Classes and STL

Postby C++ on Fri Feb 06, 2009 12:32 pm

codeseeker wrote:Hey thanks! I'll post mine maybe sooner. I'm still halfway through my code.
With your code, the thing that's new to me is the ff.
friend ostream & operator<<(ostream &o, book &b) {o << "Title: " << b.title << "\nAuthor: " << b.author << "\nisdn: " << b.isdn; return o;}

I have used this & operator on our topic about copy constructor (deep copy) but never understood it fully.


If a class declares a friend function it basically considers that function to be a friend of the class.
A friend can access any members of the class.

std::cout does not know of type book. You need to tell operator<< how to print a book to the screen.
Therefore you overload operator<<
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: C++ Classes and STL

Postby codeseeker on Fri Feb 06, 2009 1:12 pm

Here's my solution.. thanks to C++ (not the language) for answering my QA here.

Code: Select all
#include <iostream>
#include <string>
#include <map>

using namespace std;

class person;
class book;
class borrower;
class borrwerdb;
class bookdb;

class person
{
public:
   int id;
   string firstname;
   string lastname;
   person(int i, string  f, string  l):
      id(i), firstname(f), lastname(l) { }
};

class borrower:public person
{
public:
   int count;
   book *bk[5];
   borrower(int i, string f, string l): person(i,f,l), count(0) {}
   borrower( person& p ): person(p), count(0) {}
   void borrowbook( book *b );
   void printbooksborrowed();
};

class book
{
private:
    string catnum;
    string author;
    string title;
   borrower *current;
public:
   book(string c, string a, string t):
      catnum(c), author(a), title(t) {}
   void returnbook();
   void setborrower( borrower *b ) { current = b; }
   string getcatnum() { return catnum; }
   string getauthor() { return author; }
   string gettitle() { return title; }
};

class borrowerdb
{
public:
   map<int, borrower *> brwr;
   void add(int i, string f, string l);
   borrower * get(int i);
};

class bookdb
{
public:
   map<string, book *> bk;
   void add(string c, string a, string t);
   book * get(string c);
};
void borrowerdb::add(int i, string f, string l)
{
   brwr[i] = new borrower(i, f, l);
}
borrower * borrowerdb::get(int i)
{
   return brwr[i];
}
void bookdb::add(string c, string a, string t)
{
   bk[c] = new book(c, a, t);
}
book * bookdb::get(string c)
{
   return bk[c];
}
void borrower::borrowbook( book *b )
{
   bk[count++] = b;
   b->setborrower( this );
}

void borrower::printbooksborrowed()
{
   cout << "----" << endl;
   cout << "borrower: " << id << " " << lastname << endl;
   for( int i = 0; i < count; i++ )
   {
      cout << "  " << bk[i]->getcatnum()
         << " " << bk[i]->gettitle()
         << " " << bk[i]->getauthor() << endl;
   }
}

void book::returnbook(){
   book *bktemp[5];
   for(int i=0; i < current->count; i++){
      if(current->bk[i] == this) {
         current->bk[i] = 0;
      }
   }
   for(int i=0; i < current->count; i++){
      if(current->bk[i] != 0) {
         bktemp[i] = current->bk[i];
      }
   }
   current->count--;
   for(int i=0; i < current->count; i++){
      current->bk[i] = bktemp[i];
   }
   current = 0;
}

int main()
{

   borrowerdb borrDB;
   bookdb bookDB;
   
   borrDB.add(333, "Bill", "Clinton" );
   borrDB.add(666, "George", "Bush" );
   borrDB.add(999, "Barack", "Obama");

   bookDB.add("AAA","Calculus","Leithold");
   bookDB.add("EEE","Algebra","Someone");
   bookDB.add("III","Java","Horstmann");
   bookDB.add("OOO","C++","Stroustrup");
   bookDB.add("UUU","Programming","Knuth");

   borrower *br;
   book *bk;

   br = borrDB.get(666);
   bk = bookDB.get("UUU");
   br->borrowbook( bk );
   br->printbooksborrowed();

}



I need some suggestions, or comments on this one especially the best approach I should have done. TIA.
codeseeker
 
Posts: 12
Joined: Mon Feb 02, 2009 9:30 am

Re: C++ Classes and STL

Postby C++ on Fri Feb 06, 2009 3:26 pm

Good job. It's not that different from mine. I'd recommend using a vector for books borrowed by someone. Now you're limiting yourself with a maximum of 5 books. I'd also check to see if a book is already borrowed by someone. You could also add an extra attribute to books, that holds how many copies of that specific book can be borrowed.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am


Return to -- Code Practice Submissions --

Who is online

Users browsing this forum: No registered users and 0 guests