I have a program that creates an array of pointers to an object. The class the pointers are pointing to stores the net price of an item and has a function for calculating the gross price of the object.
Product *p[10];
Book *b;
Software *s;
double price;
cout << "Enter the price of the book: " << endl;
cin >> price;
b = new Book(price);
cout << "Enter the price of the software: " << endl;
cin >> price;
s = new Software(price);
cout << "Gross price of book: " << b->getGrossPrice() << endl;
cout << "Gross price of software: " << s->getGrossPrice() << endl;
p[0] = b;
p[1] = s;
Is there a way of sorting the array in ascending order of the gross price?
I know there is a sort algorithm in the STL but I don't think it will work for me or at least I'm not sure how to make it work.
There are solutions to similar problems on the site that use sort however none that I can find show it's used in this context.
getGrossPrice()virtual? then you can just call sort with a custom functor that calls that