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

bookChallenge: Managing Data with Constructors and Destructors

Task

Swipe to start coding

Imagine you are building a student grading system. You need to create a GradesManager class that manages a list of student grades.

Your task is to implement a constructor that initializes a dynamic array of grades and a single method that calculates the average grade. The destructor should release the allocated memory automatically.

  1. Implement a constructor using initializer list syntax:

    • It should take size as a parameter.
    • Allocate a dynamic array of integers called grades with the given size.
    • Use a for loop with index variable i from 0 to size to initialize each grade:
      • Assign grades[i] = i + 1 as example values.
  2. Implement a single method calculateAverage:

    • Create a variable sum initialized to 0.
    • Use a for loop with index variable i from 0 to size to iterate over the grades array:
      • Add grades[i] to sum in each iteration.
    • Calculate the average as sum * 1.0 / size to ensure a double result.
    • Return the average.
  3. Implement a destructor:

    • Use delete[] grades to release the memory allocated for the array.
    • Print "Grades memory released." to indicate that the memory has been freed.

Example

GradesManager(5).calculateAverage() β†’ 3.0

Solution

solution.cpp

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 8
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 3.13

bookChallenge: Managing Data with Constructors and Destructors

Swipe to show menu

Task

Swipe to start coding

Imagine you are building a student grading system. You need to create a GradesManager class that manages a list of student grades.

Your task is to implement a constructor that initializes a dynamic array of grades and a single method that calculates the average grade. The destructor should release the allocated memory automatically.

  1. Implement a constructor using initializer list syntax:

    • It should take size as a parameter.
    • Allocate a dynamic array of integers called grades with the given size.
    • Use a for loop with index variable i from 0 to size to initialize each grade:
      • Assign grades[i] = i + 1 as example values.
  2. Implement a single method calculateAverage:

    • Create a variable sum initialized to 0.
    • Use a for loop with index variable i from 0 to size to iterate over the grades array:
      • Add grades[i] to sum in each iteration.
    • Calculate the average as sum * 1.0 / size to ensure a double result.
    • Return the average.
  3. Implement a destructor:

    • Use delete[] grades to release the memory allocated for the array.
    • Print "Grades memory released." to indicate that the memory has been freed.

Example

GradesManager(5).calculateAverage() β†’ 3.0

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Β 8
single

single

some-alt