144k views
4 votes
For this lab, you will create several "sum classes" (you can re-use parts of some of your homework assignments), then a main class which will inherit all of the others (using multiple inheritance) and an instance of the main class will be used in your main source file to execute all of the functions. Create a header file (ex. sums.h), and a main source file (main.cpp).

1. Create a class called "sum1". Within this class create a public function called "sum" that takes a positive integer input n and sums up the numbers from 1 to n and stores the result as a protected variable.
2. Create a class called "sum2". Within this class create a function called "sumsq" that takes a positive integer input n and sums up the squares of the numbers from 1 to n and stores the result as a protected variable.
3. Create a class called "sum3". Within this class create a function called "sumcube" that takes a positive integer input n and sums up the cubes of the numbers from 1 to n and stores the result as a protected variable.
4. Create a class called "sumsum". Within this class create 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 (and stores the result as a protected variable). ex. for n=4 : sumsums (4)=1+(1+2)+(1+2+3)+(1+2+3+4). You could potentially inherit the sum1 class and use that function.
5. Create a class called "sumfull" that inherits all of the other classes (sum1, sum2, sum3, sumsum). Within this class create a function called "allsums" that takes a positive integer input then uses the functions from all of the inherited classes to execute each sum and then prints all of the results.
6. In your main source file, create an instance of the class "sumfull" and execute the function "allsums" to carry out the calculations and print the results. Make sure to ask the user for a positive integer first.

User Caspar
by
7.4k points

1 Answer

1 vote

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

User Aly
by
8.0k points