Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Initialization List Practice | Constructors and Destructors
C++ OOP

bookChallenge: Initialization List Practice

Task

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.

  1. Implement a constructor using initializer list syntax:

    • It should take amount and rate as parameters.
    • Initialize amount and rate with the passed values.
    • Automatically calculate total as amount * rate.
  2. Implement a single method processTransaction that takes two parameters:

    • Take the current value of total, which was calculated in the constructor as amount * rate.
    • Calculate the fee: divide feePercent by 100 to get the fraction and multiply it by total.
    • Subtract the fee from total to get the amount after the fee.
    • Convert the remaining amount to another currency by multiplying total by targetRate.
    • Return the updated value of total.

Example

Transaction(100, 1.2).processTransaction(5, 0.8) β†’ 91.2
Transaction(50, 0.9).processTransaction(10, 1.1) β†’ 44.55

Solution

solution.cpp

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain this in simpler terms?

What are the main takeaways from this?

Can you give me an example?

close

Awesome!

Completion rate improved to 3.13

bookChallenge: Initialization List Practice

Swipe to show menu

Task

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.

  1. Implement a constructor using initializer list syntax:

    • It should take amount and rate as parameters.
    • Initialize amount and rate with the passed values.
    • Automatically calculate total as amount * rate.
  2. Implement a single method processTransaction that takes two parameters:

    • Take the current value of total, which was calculated in the constructor as amount * rate.
    • Calculate the fee: divide feePercent by 100 to get the fraction and multiply it by total.
    • Subtract the fee from total to get the amount after the fee.
    • Convert the remaining amount to another currency by multiplying total by targetRate.
    • Return the updated value of total.

Example

Transaction(100, 1.2).processTransaction(5, 0.8) β†’ 91.2
Transaction(50, 0.9).processTransaction(10, 1.1) β†’ 44.55

Solution

solution.cpp

solution.cpp

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

some-alt