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

bookChallenge: Function Overloading Practice

Taak

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)

Oplossing

solution.cpp

solution.cpp

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 2
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain this in simpler terms?

What are some examples related to this topic?

How does this information apply to real-world situations?

close

Awesome!

Completion rate improved to 5

bookChallenge: Function Overloading Practice

Veeg om het menu te tonen

Taak

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)

Oplossing

solution.cpp

solution.cpp

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 2
single

single

some-alt