i am trying to write a programme which incorporates making a database using map containers. I have an issue with the fact that i am using a map to store the data from various derived classes.
I have an abstract base class and three derived classes from this abstract base class.
class A
class derived1:pubic A
class derived2:public A
class derived3: public A
I am trying to store information of the various types from each class in one map container, using an integer as my key. However, i cannot find a way of defining a map container so that it can change the type of each element as required to enable it to store all the data from each of the different classes.
I have used vector contaienrs to store data from derived classes before in conjunction with base class pointers, where i defined(using same class names as defined above):-
vector<*A> data;
data.push_back(new derived1(a, b, c ..etc));
this allowed me to store various types of data within a vector container.
I was hoping maybe someone could tell me a similiar method, or different (which ever works) to do the same with map containers?
Thank you so much, i was also wondering how i can insert the int into this map. is it possible to insert a pair into each element for example something like
m[1]= new pair<int, derived1>( int1, a etc);
i have tried writing this but it diddn't seem to work.
Or is there some other way i can insert the integer for each element into the map?
map contains std::pair. Note that it sort it's content. The key is what's within the [] it could be any object that's sortable (overloaded operator<()).
You can also use vector if your key is just an integer. You just have to make the size of the vector large enough like
1 2
std:vector<A*> v(20); // Key 0 - 19;
v[1] = new derived1(a, b, c ..etc); // Key = 1
Thank you, i was also wondering if You may have any ideas for this question. I have posted it but unfortunately stil havent solved the issue,
Basically i have 3 classes derived from and abstract base class. In my main i have a switch case statement with three case's one for an object created of each class type. I have a function which is pretty much identical for in each of the cases in my main and i need to declare it within the classes using a pure virtual function in my abstract base class instead of simply writing it within the main crux of my code.
ostringstream ofss;
ofss <<" "<<clec<<" "<<cgrade<<" %"<<endl;
string outputFilename(ofss.str());
(clec and cgrade are previously declared)
This is few lines of code i have in my main of which i wish to write as a function inside my classes, and the pure virtual function of it declared in abstract base class, which will simply when called return a string.
A fellow cplusplus member did reply to me and suggest the following, of which i had already tried something similiar to but couldn't get it to work.
string temp(ofss.str());
return temp;
}
in each of the 3 derived classes. "]
When i tried to implement this i jsut got a few errors within my classes, mentionin operands in reference to the "ofss<<course<" "<<grade<<"%"<<endl;" line.
Just in case it is important my classes are contained within a header file.