Question

In: Computer Science

Programming Language C++ Task 1: Write a program to calculate the volume of various containers. A...

Programming Language C++

Task 1: Write a program to calculate the volume of various containers. A base class, Cylinder, will be created, with its derived classes, also called child classes or sub-classes.

First, create a parent class, Cylinder. Create a constant for pi since you will need this for any non-square containers. Use protected for the members. Finally, create a public function that sets the volume.

// The formula is: V = pi * (r^2) * h

Task 2: Create a derived, or child class for Cylinder, that is, a Cone class. The same function, with the same parameters, is used. However, the formula is different for a cone.

// The formula is: V = (1/3) * pi * (r^2) * h

Task 3: Test your classes in the main function by creating an instance of Cone and an instance of Cylinder. In each case, call the set_volume function, passing the same parameters.

Task 4: Create a derived class for Cone called PartialCone. Add a second radius variable with scope specific to this class (because the top and bottom radii of a partial cone are different). Redefine the set_volume function.

The formula for the volume of a partial/truncated cone is:

  • V = h * (PI / 3) * (R2 + (R * r) + r2 )
  • V = volume
  • R = base radius
  • r = top surface radius

Task 5:

  1. What happens if you change the access modifiers for Cylinder's variables to private?
  2. Consider that you declare a private variable in the Cone class. Can you reference it in Cylinder?

Solutions

Expert Solution

Code:

#include <iostream>

using namespace std;

// Cylinder class

class Cylinder{

protected:

const float pi=3.14;

float volume;

public:

void set_volume(int r,int h){

volume=pi*(r*r)*h;

}

float get_volume(){

return volume;

}

};

// derived class cone from cylinder

class Cone : public Cylinder{

public:

void set_volume(int r,int h){

volume=pi*(r*r)*h*0.34;

}

};

int main() {

Cone c1;

c1.set_volume(30, 40);

cout<<"Volume of Cone c1 with r=3, h=4 is "<<c1.get_volume()<<endl;

Cylinder c2;

c2.set_volume(30, 40);

cout<<"Volume of Cylinder c2 with r=3, h=4 is "<<c2.get_volume()<<endl;

}

Output:

Volume of Cone c1 with r=30, h=40 is 38433.6
Volume of Cylinder c2 with r=30, h=40 is 113040

Please post the other parts as another question. Thank you.


Related Solutions

Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Write a program in C++ that solves this problem Calculate the area and volume of a...
Write a program in C++ that solves this problem Calculate the area and volume of a sphere problem. Inside a for loop, vary the radius from 10 to 40  with a step or increment of 5 and calculate the area and volume Your radius will be equal to your loop counter. All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma. Print the radius, area,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT