how to instantiate control statement inside virtual function

Pages: 12
closed account (48T7M4Gy)
There are lots of ways of doing this and maybe this explains what I am getting at better than a thousand words. You might be able to adapt a few of the ideas, but obviously there's no obligation and there is a lot of tidying up to do anyway.
Last edited on
closed account (48T7M4Gy)
No, @elsa, I don't mind. (I have to go soon though for a few hours)

However, to answer your question.
1. The variable cost is a protected member in the base class
2. So it is accessible by any of the inherited objects as I have done.
3. That means the value of cost used in the discount function is simply cost
4. Since the discount offered is a general thing there only needs to be one place to do the calculation and that is in the base class - try it in each subclass but the formula is the same if I read your assignment correctly.

As a suggestion on variable names, final_cost = cost - discount. This means a simpler way might be to work out the discount and then subtract it at the end using just one return statement. eg return cost - discount;

Also your cascade of if's is not right. You can't do cost == 500 to 1000
cost <=1000 && cost >= 500 is one way but there are a few refinements possible on that if you surf around.

Kemort. Thank you for staying with me on this problem. Much appreciated for your work, you've gone above and beyond, truly.

I've been studying what you've done, and it's very clean and compact. You really keep the main air-tight and your passing of the bools altogether with the rest of the information for the sub-class is interesting. There truly are many ways to approach this problem. And I'm starting to see how this program, can scale beautifully if given an exponential amount of data points and do very complex things. So in a sense I'm starting to understand the importance of classes more broadly and how this is a vast improvement over C, which I truly hated.

My next battle from here will involve 1.) calculating total cost after discount, which you alluded to with your comment at the beginning, and am still stuck on because I'm not sure how to return the calculated cost into the discount calculation function...would it be declared at the top (inside Package) as virtual double calculate_discount()const{ cost; return discount; } wherein /*cost*/ would be passing the returned cost into the discount function for manipulation and then pass out the discount calculation? ...sorry for my confusion! and 2.) creating a control statement for determining the time, which, again would involve passing the specific bool information into the appropriate if-statement for determining ETA.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//float Package::discount() 
//{
//	if (cost < 200)
//		discount = cost - (cost * .019);
//		return discount;
//	else if (cost == 200.01 to 500)
//		discount = cost - (cost * 0.024);
//		return discount;
//	else if (cost == 500.01 to 1000)
//		discount = cost - (cost * 0.028);
//		return discount;
//	else 
//		discount = cost - (cost * 0.033);
//		return discount;
//} 
I see you responded. Thanks again Kemort. Have a great President's day.
closed account (48T7M4Gy)
Maybe calculate the cost and discount in the constructor. Like everything there are lots of ways to do things. The way I would cascade through the discounts is a little different. (Don't just copy what I've done, especially because I have only done this roughly and haven't checked any calculations, and there could well be bloopers.)
Last edited on
Just wanted to mention that there are 2 sides to having protected data in classes. Note, I am not disagreeing with kemort, the FAQ mentions both sides :+)

https://isocpp.org/wiki/faq/basics-of-inheritance#protected-data-not-evil

In this case having protected functions would make the code longer and more complex.

About why I mentioned using a std::vector with 1 item in it: that was a response to avoid using new ; std::vector puts it's data on the heap - which is often the motivation for someone wanting to use new.
closed account (48T7M4Gy)
@TIM

To be perfectly honest I didn't even think to make the calculate functions protected because public access was a quick way to demonstrate to OP how you can work out costs (and display them). I've just changed my current version to be protected because the 'invoice/mailing label' obviates any need to have external or separate access to the various calculation(s). Given the way I have approached it, except for adding a couple of protected:'s, the code is exactly the same my latest. Given the choices I go for the higher protection in the absence of other considerations

I might have jumped the gun on vectors. Luckily(maybe) this OP assignment doesn't require lists of items to be recorded so pointers aren't needed as per the recent thread where pointers (smart or otherwise) come into it - even there new & delete wasn't needed http://www.cplusplus.com/forum/beginner/208682/#msg983699
Hi kemort,

I was was meaning the protected data:

5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Package
{
protected:
    std::string name_and_address = "?";
    
    int no_items = 0;
    
    double cost = 0.0;
    double discount = 0.0;
    
    bool express_delivery;
    bool insured;
    
    std::string package_contents = "?";


The FAQ talks about having protected functions to access private data. As I mentioned earlier, for this it is probably fine as it is. Cheers :+)
closed account (48T7M4Gy)
OK now I see why you refer to more code being necessary and it's relevance to the management of large projects.

I find that getters and setters are nice to avoid on the grounds of laziness on my part. :)
@kemort I have a question one of the techniques you've used, particularly this:

<< (insured ? "- INSURED\n" : "- NOT INSURED\n")

How would you be able to fit something like a variable such as time inside this split output? Depending on whether express_delivery is up or down, I want to input two different time calculations. But I'm not sure how to stick a variable on either side of the colon. I've been getting a "expected a ':'" error, and various other errors when I try to insert time. My time looks like this:

1
2
3
4
5
6
7
chrono::system_clock::time_point now = chrono::system_clock::now();
time_t now_c = chrono::system_clock::to_time_t(now + chrono::hours(24));
cout << "Overnight Shipping Expected Arrival Date: " << put_time(localtime(&now_c), "%F") << '\n';

chrono::system_clock::time_point now2 = chrono::system_clock::now();
time_t now2_c = chrono::system_clock::to_time_t(now2 + chrono::hours(72));
cout << "Standard Shipping Expected Arrival Date: " << put_time(localtime(&now2_c), "%F") << '\n';
<< (express_delivery ? (put_time(localtime(&now_c), "Expected Arrival Date: %F")) : put_time(localtime(&now2_c), "Expected Arrival Date: %F"));

Solution ^^^
closed account (48T7M4Gy)
Looks good. Another way would be to have a function in the base class that returns the arrival date.

You'll probably need to now build on this because I noticed a couple of things in the assignment specification. You'll probably need to develop a specific invoice (virtual) for each sub class because the assignment says you have to itemise the bill.

Another one for the delivery address label in the base class.

These would appear via a menu in main I guess.

:)


Topic archived. No new replies allowed.
Pages: 12