课程: Complete Guide to C++ Programming Foundations
免费学习该课程!
今天就开通帐号,24,100 门业界名师课程任您挑!
Solution: Calculate resource cost - C++教程
课程: Complete Guide to C++ Programming Foundations
Solution: Calculate resource cost
(upbeat intro music) (music ends) - [Instructor] For this challenge, your task was to write the code for the CalculateTotalCost function to return a double with the calculated cost of a set of resources which specify their type, which could be basic, luxury or essential. You were given a vector of resources as an argument. Let me show you my solution. Starting at line 20, we have a range-based for loop to traverse the resources vector. The current element per iteration is in the resource variable. Then, in line 21, I declared a double named costWithTax to calculate the cost of the current resource in this iteration. It is initialized at the baseCost. Then, in the sequence of if/else statements, I add its 5% or 15%, depending on the resource type. For this, I used additions in lines 24 and 26, but I could also have multiplied costWithTax by 1.05 or 1.15 respectively. Lastly, we accumulate the costs in the result variable, which is the one we ultimately return. Let's see it working. And…