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.
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?
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?