Challenge: Initialization List Practice
Swipe to start coding
Imagine you are building a banking application. You need to create a Transaction class that represents a money transfer.
Your task is to implement a constructor that initializes the transaction amount and exchange rate, calculates the total in the target currency, and a single method that applies a fee and converts the amount to a different currency.
-
Implement a constructor using initializer list syntax:
- It should take
amountandrateas parameters. - Initialize
amountandratewith the passed values. - Automatically calculate
totalasamount * rate.
- It should take
-
Implement a single method
processTransactionthat takes two parameters:- Take the current value of
total, which was calculated in the constructor asamount * rate. - Calculate the fee: divide
feePercentby 100 to get the fraction and multiply it bytotal. - Subtract the fee from
totalto get the amount after the fee. - Convert the remaining amount to another currency by multiplying
totalbytargetRate. - Return the updated value of
total.
- Take the current value of
Example
Transaction(100, 1.2).processTransaction(5, 0.8) β 91.2
Transaction(50, 0.9).processTransaction(10, 1.1) β 44.55
Solution
solution.cpp
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain this in simpler terms?
What are the main takeaways from this?
Can you give me an example?
Awesome!
Completion rate improved to 3.13
Challenge: Initialization List Practice
Swipe to show menu
Swipe to start coding
Imagine you are building a banking application. You need to create a Transaction class that represents a money transfer.
Your task is to implement a constructor that initializes the transaction amount and exchange rate, calculates the total in the target currency, and a single method that applies a fee and converts the amount to a different currency.
-
Implement a constructor using initializer list syntax:
- It should take
amountandrateas parameters. - Initialize
amountandratewith the passed values. - Automatically calculate
totalasamount * rate.
- It should take
-
Implement a single method
processTransactionthat takes two parameters:- Take the current value of
total, which was calculated in the constructor asamount * rate. - Calculate the fee: divide
feePercentby 100 to get the fraction and multiply it bytotal. - Subtract the fee from
totalto get the amount after the fee. - Convert the remaining amount to another currency by multiplying
totalbytargetRate. - Return the updated value of
total.
- Take the current value of
Example
Transaction(100, 1.2).processTransaction(5, 0.8) β 91.2
Transaction(50, 0.9).processTransaction(10, 1.1) β 44.55
Solution
solution.cpp
Thanks for your feedback!
single