Need exercises

Hello, i want to test my skills in classes, so maybe you can give me some exercises? I read tutorials about classes and polymorphism but i can't found where to use them...
Excercise:

I want to be able to save data to a text file. The data to save is a list of stuff I own, meaning the text file will be my inventory. For each asset, I want to save certain data, but it really varies from one type of asset to the other.

Right now, my types of assets are cars, motorcycles, computers, printers, and buildings.

For cars and motorcycles, I want to record the make, model, year, license plate number, and VIN.

For computers, I want to save the model, the processor description, the installed operating system, the language of the operating system, and a 6-digit asset number that uniquely identifies it. This asset number may contain letters.

For printers, I want to save the model, the type of printer (inkjet, dot-matrix, laser), if it prints in color, the number of paper trays, and if they can handle duplex printing. These also have a 6-digit asset number like the computers above. I want it saved too.

Finally, for buildings I want to save the address, a meaningful name that uniquely identifies it, the number of stories, the area of the terrain in square meters, and the number of parking spots.

So, make an application that can accept any of these types of assets in any order, and then save them all into a text file, grouped by type. A plus: Within each group, sort them. Based on what? Surprise me. :-)


This can be achieved using polymorphism for sure. Enjoy!
Polymorphism? Not in this case...none of those objects are related...
There are related in the sense that they can all be saved to a file / loaded from a file. They could probably all be derived from a 'Serializer' abstract base class or something.
They are related. They are all assets.

In this case, all assets can be saved to a file. So there. If I say one more word, I'll spoil the fun.
@kevinchkin: maybe i'll try the last exercice later.
@webJose: i'll start coding right now! And a few questions:
1. Should i keep assets grouped together? (i must enter data of all assets or i can enter data only for one asset?)
2. Can i do this with classes?
1
2
3
4
class myclass
{/*...*/}pmyclass;
pmyclass=new pmyclass[100]; /db of 100 classes
The requirement is that you save the assets to the text file grouped by asset type. Also a requirement: I can enter assets in any order, meaning the asset 1 may be a car, asset 2 may be a building, and asset 3 another car. I want the resulting file showing asset 1, asset 3, and asset 2 because cars must be grouped together. Therefore, you cannot start writing the assets to the file without having 100% of the assets to record. Meaning: You must keep storing in RAM all assets until instructed to generate the text file.

For question 2. I think your question is: How can I create an array of classes? The answer is:
 
ClassType ** myArray = new ClassType*[total];


But that looks weird. Use some typedefs
1
2
3
4
5
6
7
8
9
10
11
12
13
class MyClass
{
...
}

typedef MyClass *PMyClass;
typedef PMyClass *PPMyClass;
typedef PPMyClass ArrMyClass;

...

//Now you can define the array as:
ArrMyClass myArray = new PMyClass[total];

Doh! I forgot: To populate an item of the class array:

 
myArray[someIndex] = new MyClass();

I just wrote 200th line of source code - nothing works for now but i'll finish this in 2-3 days. By the way, i understood that i don't need an array of classes - here's pseudocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Cmain
{
public:
void LoadData(); //open file and read data to structs
void SaveData();

class Cobject{ //car, pc, printer...
void GetData(); // function to ask user to enter information about object and put data to struct  
struct ObjectData
{
bool ok; // if false, struct won't be written to file
 char name[50];
  //.....
}*pObjectData;

//other objects...
}Cobject;
}pCmain,main;

also i'll keep number of objects of each type at the begining of file and wih this data i'll dynamical allocate memory for structs. I don't know if it will work but i'll see :-)
I just wrote 200th line of source code - nothing works for now but i'll finish this in 2-3 days. By the way, i understood that i don't need an array of classes - here's pseudocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Cmain
{
public:
void LoadData(); //open file and read data to structs
void SaveData();

class Cobject{ //car, pc, printer...
void GetData(); // function to ask user to enter information about object and put data to struct  
struct ObjectData
{
bool ok; // if false, struct won't be written to file
 char name[50];
  //.....
}*pObjectData;

//other objects...
}Cobject;
}pCmain,main;

also i'll keep number of objects of each type at the begining of file and wih this data i'll dynamical allocate memory for structs. I don't know if it will work but i'll see :-)
EDIT: i don't need array of classes, only array of array of structs which will be created at runtime
class Cobject{ //car, pc, printer...
void GetData(); // function to ask user to enter information about object and put data to struct


I have not seen the remaining code but I don't think you can ask user to enter information about object using void GetData(); as it is a private function.
Topic archived. No new replies allowed.