need help on my book coding here

This is a discussion on need help on my book coding here within the C++ Programming forums, part of the General Programming Boards category; help my coding is nearly finish it just i don't know to make the getCount() function to return the number ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    need help on my book coding here

    help my coding is nearly finish it just i don't know to make the getCount() function to return the number of book objects that have been created..
    and dont understand this line>>the count data member should be initialized to zero and must keep track of number of book objects..
    my coding is like this


    Code:
    class Book
    {
    private:
    
    string author,title;
    const short bookType;
    int count;
    
    public:
    Book( const string& author,const string& title, int bookType = 1):count(0);
    ~Book();
    string getTitle();
    string getAuthor();
    Void setTitle(string title);
    void setAuthor(string author);
    int getCount(){return count;};
    
    }
    
    	
    Book::Book( const string& author,const string& title, int bookType = 1);
    	{    
    setAuthor(author);
                  setTitle(title);
    		++count; 
    }
    
    Book::~Book~(){
        --count;
        
    }
    
    int Book::count = 0;
    
    
    void Book::setTitle(string title)
    
    {
    title = title
    }
    
    string Book::getTitle() 
    
    {
    
    return title;
    
    }
    void Book::setAuthor(string author)
    
    {
    author = author;
    }
    
    string Book::getAuthor() 
    
    {
    
    return author;
    
    }
    Last edited by MyRedz; 11-02-2009 at 12:24 AM. Reason: more coding i put for checking what's wrong

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Hey dude what you can do you can have a static counter in the class

    and just increment that in constructor

    you can look at this code snippet for your help

    Code:
    #include <iostream>
    
    class Test {
      static int counter;
    public:
      Test() {++counter; }
      void show() { std::cout << "counter == " << counter << std::endl; }
    };
    
    int Test::counter = 0;
    
    int main() {
      Test t1, t2, t3, t4, t5, t6, t7, t8 ,t9;
      t1.show();
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,047
    Don't forget to implement or disable a copy constructor, so that the count goes up when you copy a book.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a chain-structure
    By MalickT in forum C Programming
    Replies: 12
    Last Post: 03-27-2008, 05:51 AM
  2. Looking for a c++ book, didn't were to post this...
    By Rune Hunter in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 06:32 PM
  3. Replies: 5
    Last Post: 09-19-2004, 06:52 AM
  4. Must read book!
    By RealityFusion in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-15-2004, 09:05 PM
  5. Should i get a new reference book?
    By Raison in forum Windows Programming
    Replies: 2
    Last Post: 06-15-2004, 10:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21