Final answer:
To solve this lab, create several sum classes and a main class that inherits all of them. Ask the user for a positive integer and execute the allsums function.
Step-by-step explanation:
The Solution to the Lab
To solve this lab, you need to create several sum classes and a main class that inherits all of them. Here are the steps:
- Create a class called sum1 with a public function called sum that takes a positive integer input n and sums up the numbers from 1 to n, storing the result as a protected variable.
- Create a class called sum2 with a function called sumsq that takes a positive integer input n and sums up the squares of the numbers from 1 to n, storing the result as a protected variable.
- Create a class called sum3 with a function called sumcube that takes a positive integer input n and sums up the cubes of the numbers from 1 to n, storing the result as a protected variable.
- Create a class called sumsum with a function called sumsums that takes a positive integer input n and sums up the sums of numbers from 1 to 1,1 to 2,1 to 3,1 to 4,…,1 to n, storing the result as a protected variable.
- Create a class called sumfull that inherits from all the other classes (sum1, sum2, sum3, sumsum). Within this class, create a function called allsums that takes a positive integer input and uses the functions from the inherited classes to execute each sum and prints all of the results.
- In your main source file, create an instance of the class sumfull and execute the allsums function to carry out the calculations and print the results.
Remember to ask the user for a positive integer before executing the allsums function.