Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Function Overloading Practice | Some Advanced Topics
C++ Functions

bookChallenge: Function Overloading Practice

Task

Swipe to start coding

You are building a geometry calculation tool that can compute the area of different shapes. You will implement function overloading so that the same function name calculateArea can handle rectangles, circles, and triangles.

  1. Rectangle Area Function

    • Declare a function calculateArea that takes two double parameters: length and width.
    • Inside the function, calculate the area by multiplying length by width.
    • Return the calculated area.
  2. Circle Area Function

    • Overload the calculateArea function to take one double parameter: radius.
    • Calculate the area using the formula PI multiplied by radius squared, using the pow function to raise radius to the power of 2.
    • Return the calculated area.
  3. Triangle Area Function

    • Overload the calculateArea function to take three double parameters: a, b, and c.
    • Compute the semi-perimeter s = (a + b + c) / 2.
    • Use Heron's formula: sqrt(s * (s - a) * (s - b) * (s - c)) to calculate the area.
    • Return the calculated area.

Do not modify the value of the PI variable.

Example

calculateArea(4, 6) β†’ 24 (rectangle)
calculateArea(3) β†’ 28.27431 (circle)
calculateArea(5, 4, 6) β†’ 9.92157 (triangle)

Solution

solution.cpp

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

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

close

Awesome!

Completion rate improved to 5

bookChallenge: Function Overloading Practice

Swipe to show menu

Task

Swipe to start coding

You are building a geometry calculation tool that can compute the area of different shapes. You will implement function overloading so that the same function name calculateArea can handle rectangles, circles, and triangles.

  1. Rectangle Area Function

    • Declare a function calculateArea that takes two double parameters: length and width.
    • Inside the function, calculate the area by multiplying length by width.
    • Return the calculated area.
  2. Circle Area Function

    • Overload the calculateArea function to take one double parameter: radius.
    • Calculate the area using the formula PI multiplied by radius squared, using the pow function to raise radius to the power of 2.
    • Return the calculated area.
  3. Triangle Area Function

    • Overload the calculateArea function to take three double parameters: a, b, and c.
    • Compute the semi-perimeter s = (a + b + c) / 2.
    • Use Heron's formula: sqrt(s * (s - a) * (s - b) * (s - c)) to calculate the area.
    • Return the calculated area.

Do not modify the value of the PI variable.

Example

calculateArea(4, 6) β†’ 24 (rectangle)
calculateArea(3) β†’ 28.27431 (circle)
calculateArea(5, 4, 6) β†’ 9.92157 (triangle)

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Β 4. ChapterΒ 2
single

single

some-alt