Your
store
class adds no value. In fact, it makes things confusing. The statement
sml+load_t;
looks like an error. Just use
vector<Bclass>
instead and replace the odd add with
sml.push_back(load_t);
Also, what's the purpose of
Bclass
? All it does it make the members of class
cars
accessible. I'll assume that you have some other reason for this, but if not, get rid of Bclass and put those methods directly in class cars.
Bclass::get_info() won't work. Consider this code:
1 2 3 4
|
int i;
Bclass bc;
cin >> i;
cout << bc.get_info(i); // what is the type of bc.get_info()?
| |
The compiler needs to know the type of every expression at compile time. In this code, it won't know until run time.
To fix this, replace get_info() with get_marke(), get_modelis(), get_var_turis() and get_maks_grietis() methods.
To read
Bclass
, create a
read()
method (or
>>
operator if you know how to do that). This separates the reading part from the "adding to a vector" part, which may be useful. In fact, why not put that method in class
cars
instead?
Putting it all together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::istream;
using std::ifstream;
using std::string;
using std::vector;
class cars
{
protected:
string a_marke, a_modelis;
int var_turis, maks_grietis;
public:
cars(string am = "", string amm = "", int vt = 0, int mg = 0) {
a_marke = am;
a_modelis = amm;
var_turis = vt;
maks_grietis = mg;
}
istream &
read(istream &);
};
class Bclass: public cars
{
public:
string get_mark()
{
return a_marke;
}
string get_modelis()
{
return a_modelis;
}
int get_ark()
{
return var_turis;
}
int get_mks_g()
{
return maks_grietis;
}
void set_mark(string a = "") {
a_marke = a;
}
void set_modelis(string a = "") {
a_modelis = a;
}
void set_ark(int s = 0) {
var_turis = s;
}
void set_mks_g(int s = 0) {
maks_grietis = s;
}
};
istream & cars::read(istream & strm)
{
getline(strm, a_marke);
getline(strm, a_modelis);
strm >> var_turis >> maks_grietis;
return strm;
}
void
startup(vector < Bclass > &sml)
{
cout << "loading" << endl;
ifstream filein;
filein.open("db.txt");
Bclass load_t;
sml.clear(); // you'd be amazed how often people for to do this
while (load_t.read(filein)) {
sml.push_back(load_t);
}
for (unsigned y = 0; y != sml.size(); y++) {
cout << "+";
}
cout << endl;
cout << "u=krauta: " << sml.size() << endl;
cout << endl;
filein.close();
}
| |