I have the following problem. I have an abstract class with a virtual function.
< class A{
public:
virtual double GetTotalPrice() = 0;
}; >
A derived class:
<class B : public A {
A (string st, int number, double num) : x(st), y(number), z(num) { }
double GetTotalPrice() //returns a quantity
{
...
} }; >
Another derived class:
<class C : public B {
C (double d, int i) : dd(d), ii(i) {}
double GetTotalPrice() //returns a quantity
{
...
} }; >
My task is to create a vector with pointers to class A instances and add 3 elements of class B and 3 elements of class C to it.
Here is how I created the vector:
vector<A*> it;
But I am not sure how to add items to it, especially of class C. Can somebody help me with that? I also need to call the virtual function GetTotalPrice() to all of those elements. How do I do that?
Thank you. But class B and C have constructors with elements that need to be used in the function that is virtual.
Can I use this somehow ? A.push_back(new B("DD-54", 3, , 7.50); or something similar.
We are creating pointers alright, as anonymous prvalues.
When we apply the address of operator & to an object of type B, it yields a pointer.
For instance, with, say, B b1( "DD-54", 5, 7.50 ) ;&b1 yields a pointer of type B*
There is an implicit conversion from this pointer to a pointer of type A*, which is what the vector requires.