Solved

create a class called "book" using c++ that has a private data of :

Posted on 2006-10-31
7
427 Views
Last Modified: 2010-04-01
Hi guys,

I need to create a class called "book" that has a private data of :

class book
{
char author[10];
char title[10];
float price;
char publisher[15];
int stock_position;
public :
book()
{
cout<<"enter name:- "cin>>author;
cout<<"enter title:- ";cin>>title;
.
.
.
..
.

 
author
  title
  price
  publisher,and
stock position,and the public fuction of "constructor" using file(in c++) that uses the new operator.and when the user need to search he will enter the title and author name then the system searchs and display if avialable if not it will display an appropriate message.


Please and  Thank you!
0
Comment
Question by:zackexpert
7 Comments
 
LVL 4

Expert Comment

by:hclgroup
Comment Utility
This sounds like home-work!

Please attempt assignment and ask questions pertaining to where you are having difficulty...
0
 
LVL 8

Expert Comment

by:adg080898
Comment Utility
What's the question? We can help with your work, but we can't DO your work! What aspect are you having a problem with?
0
 
LVL 4

Accepted Solution

by:
ct.smith earned 125 total points
Comment Utility
I have one little critique of your posted code.  It's almost always preferable to use fixed point, not floating point, for money values.  With floating point, you end up with rounding errors that shouldn't be there.  For
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 total points
Comment Utility
Some more issues:

>>>> char author[10];

use std::string if possible or at least increase the number of chars to reasonable value, say 32

>>>> char title[10];

Here you should have 128 chars cause some titles might get lengthy.

>>>> book()
>>>> {
>>>> cout<<"enter name:- "cin>>author;

Don't use user I/O in the constructor of a class. I/O should be made in the operative part of a program, i. e. in the main() function or in some operative class, e. g.

class BookShop()
{
      book currentBook;
      std::vector<book> allBooks;
public:
     
      bool addBook();
      // ... more to run a bookshop
};

Put the dialog to the addBook shop and add the new book to the collection of books already stored.

>>>> cout<<"enter name:- "cin>>author;

That is wrong syntax. Put any statement to a new line (for readability reasons)  and close it by a semicolon ;

   cout << "enter name: ";
   cin >> author;

Note, if 'author' isn't a (std) string type but a char[10] array your program writes beyond the array bounds if the user entered more than 9 chars ( + terminating zero char). That's why you should use a suitable big *buffer* if you have to use char arrays (and not std::string what is highly recommended).

   char input[256];   // don't be stingy
   char author[10];  // use the same size as in class book
   ...

   cout << "enter name: ";
   cin >> input;

   int inputlen = strlen(input);
   if (inputlen >= sizeof(author))
         inputlen = sizeof(author)-1;   // cut the input length (or give error message)
   strncpy(author, input, inputlen);   // now is safe
   author[inputlen] = '\0';  // set the terminating 0

   
After getting all attributes you should use a constructor of class book which takes all attributes in the argument list.

class book
{
     ...

public:
     ...
     book(const char a[], const char t[], /* and for all other data members */ );
 
};

Note, you can't simply assign the char array arguments if not using std::string for the members. You have to use strcpy instead.

Regards, Alex
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now