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

bookChallenge: Function Overloading Practice

Opgave

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)

Løsning

solution.cpp

solution.cpp

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

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

Stryg for at vise menuen

Opgave

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)

Løsning

solution.cpp

solution.cpp

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2
single

single

some-alt