Read a data file containing objects of 2 diff. classes

I'm writing a program for managing the stock details of a book store. It sells both books and their audio cassette versions. My defined classes are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  class stock
{
    char title[30],author[15];
    float price; int stockpos;
    char version[5];

protected:

    void getdata(); void putdata();
    void defvers(char a[])  {strcpy(version,a);}//used to identify book & tape
    void rettitle(char a[]) {strcpy(a,title);}
    void retauthor(char a[]){strcpy(a,author);}
    void retvers(char a[])  {strcpy(a,version);}/* all these used to obtain title, 
author, and whether its a book/tape to compare with the given details while searching*/
};


Here I have defined a class stock for the common data members and functions and derived 2 classes book and tape for defining the unique data members.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class book:public stock
{
    int pagecount;
public:
    book()   {  pagecount=0; defvers("book");    } //ctor
    void getdata();
    void putdata();
};

class tape:public stock
{
    float duration; char readby[15];
public:
    tape()    {  duration=0.00; defvers("tape");  }//ctor
    void getdata();
    void putdata();
};




I'm using a single data file stock.dat and storing the details of both books and tapes to it.

Now, what has me stumped is how to define a class function in stock that would display all details of either books or tapes, depending upon the user's choice, from that single data file stock.dat. Seeing as both book and tape classes would have different sizes, how can I manipulate the file.read()?
Or am I going the wrong way and should just create 2 seperate data files books.dat and tapes.dat, not caring that it would lengthen the source code?

Please help me out here.
~Athira
1. Virtual functions. That is the answer to "How does call to show() via pointer to Stock do the right thing?"

2. File access is not trivial. Given istream file you cannot do directly:
1
2
3
4
5
Book b;
file >> b;
// or
Tape t;
file >> t;

What you do need is a third party that reads an identifier and then invokes appropriate read().

This does mean that when you do write to file, you must write the identifier as the first bit of the entry.

3. I strongly recommend that you consider std::string instead of the char arrays.
First of all, thanks for replying.

1. I have never used a virtual function, my text and practicals never required it. So I'm not at all familiar with it. Can you help me out on how to use them here?

2. I'm using version[5] to specify whether it is a book or a tape and I'm assigning to it using ctors from classes book and tape. So I should make it the first data member in class stock, right?

I'm using in main the code
1
2
3
4
5
6
7
8
9
fstream fin("stock.dat", ios::in|ios::out|ios::binary);
book b; tape t; char ch[5];
cout<<"Book or tape"; gets(ch);

if(!strcmpi(ch, "book"))
 {b.getdata();  fin.write((char*)&b, sizeof(b));
else if(!strcmpi(ch, "tape"))
 {t.getdata();  fin.write((char*)&t, sizeof(t));

is this ok?

3. I don't understand why is that. Why?

Topic archived. No new replies allowed.