Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Function Overloading Practice | Some Advanced Topics
C++ Functions

bookChallenge: Function Overloading Practice

Завдання

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.cpp

solution.cpp

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Свайпніть щоб показати меню

Завдання

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.cpp

solution.cpp

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
single

single

some-alt