Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 28th, 2004, 09:32 PM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default PLEASE PLEASE HELP ME

hello.....I have a problem with this question ppl....I tried so much to do it but i coulnt. here is the queston..and then my code:

CREATE A CLASS CALLED CARD THAT MAINTAINS A LIBRARY CARD CATALOG ENTRY. HAVE THE CLASS STORE A BOOK;S TITLE,AUTHOR AND NUMBER OF COPIES ON HAND. STORE THE TITLE AND AUTHOR AS STRINGS AND THE NUMBER ON HAND AS INTEGER. USE A PUBLIC MEMBER FUNCTION CALLED store() TO STORE A BOOK'S INFORMATION AND A PUBLIC MEMBER FUNCTION CALLED show()
TO DISPLAY THE INFORMATION. INCLUDE A SHORT main() FUNCTION TO DEMONSTRATE THE CLASS.

*please help me..


ans here is my code:

# include <iostream.h>
# include <conio.h>

class card
{
private:
char book;
int author;
int nocopies;

public:
void store();
void show();
};

void card::store()
{
cout<<"enter book title:";
cin>>book;
cout<<" nenter the author's name:";
cin>>author;
cout<<" nenter the number of copies:";
cin>>nocopies;
}

void card::show()
{
cout<<"\nthe book title is:"<<book;
cout<<"\nthe athor's name is:"<<author;
cout<<"\nthe number od copies are:"<<nocopies;
}





int main()
{

card book;
card author;
card nocopies;

book.show();
author.show();
nocopies.show();

getch();
}





Reply With Quote
  #2 (permalink)  
Old January 28th, 2004, 10:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This sounds suspiciously like homework, so I won't give you a working answer. I will tell you that you should #include iostream, NOT iostream.h. Also, the member variable, book, of the card class is declared to be of type char. That's just one character. You probably meant a char *. Better yet, use a std::string. You'll need to #include <string> to get access to that type.


Now then, in your main(), you declare three variables of type "card". You seem confused because these three variables are named "book", "author", and "nocopies". These names don't make sense. Think about it -- you're creating an object of type "card". So the variable name should have something to do with cards. "book", "author", and "nocopies" are *properties* of cards.

A better name would've been "my_card" or something like that.

In fact, the most widely accepted naming convention is to capitalize the names of your classes and data types, and to use lower-case names for your variables. In that sense, you'd have:
------------------------------------------------------------
// card.h
class Card
{
   // Class declarations go here...
};
------------------------------------------------------------
// card.cpp

#include "card.h"

// Class implementations go here...
------------------------------------------------------------


Finally, your main() simply calls show() on each of your three card variables, but you never store() anything in them. So the output of your program would be meaningless -- you'd be displaying three empty cards.

At the bare minimum, you don't need to mess with more than one card, but you DO need to exercise all of your public functions -- that is, store() and show().

That means your main should look something like this:

------------------------------------------------------------
// main.cpp

#include "card.h"

int main(int argc, char * argv[])
{
    Card card;

    card.store();
    card.show();

    return 0;
}
------------------------------------------------------------


One more thing -- don't forget to define constructor(s) and a destructor for your Card class.



And please, please please please -- DON'T TYPE IN ALL CAPS.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.