Solved

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

Posted on 2006-10-31
7
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:hclgroup
ID: 17845016
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
ID: 17845971
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
ID: 17856341
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
ID: 17856741
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

627 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