Question

Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses...

Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses them to initialize the data members. It should have a default constructor that initializes the height to 10 and the radius to 2. It should have a method named surfaceArea that returns the the surface area (in square inches) of a cylinder with that height and radius which is closed on one end but open on the other. For pi, use a value of 3.14159. Write a class named Vase that has two data members: a HalfOpenCylinder that gives the dimensions of the vase and a double that gives the cost per square inch for the vase. It should have a constructor that takes two parameters and uses them to initialized the data members. It should have a method named totalCost that returns the surface area of the vase multiplied by its cost per square inch. It should have a method named costsMoreThan that takes a Vase parameter and returns true if the Vase the method is called on costs more than the Vase that was passed as a parameter. Otherwise it should return false. Here's one simple example of how your classes could be used: HalfOpenCylinder hoc1; Vase vase1(hoc1, 0.12); cout << vase1.totalCost() << endl; HalfOpenCylinder hoc2(80.4, 13); Vase vase2(hoc2, 0.2); // std::boolalpha prints Boolean values as "true" or "false" instead of an integer value cout << std::boolalpha << vase1.costsMoreThan(vase2) << endl; Because Vase.hpp needs to know what a HalfOpenCylinder is, it will need to #include HalfOpenCylinder.hpp. In your compile command, you need to list all of the .cpp files that you are compiling together. You do not need to list any .hpp files - those are included by the #include statements. The files must be named: HalfOpenCylinder.hpp, HalfOpenCylinder.cpp, Vase.hpp, Vase.cpp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//HalfOpenCylinder.hpp file

#ifndef HalfOpenCylinder_H

#define HalfOpenCylinder_H

class HalfOpenCylinder{

                //attributes

                double height;

                double radius;

                public:

                //constructor taking values for height and radius

                HalfOpenCylinder(double h,double r);

                //default constructor

                HalfOpenCylinder();

                //returns the surface area

                double surfaceArea() const;

};

#endif

//end of HalfOpenCylinder.hpp file

//HalfOpenCylinder.cpp file

#include "HalfOpenCylinder.hpp"

//implementation of all methods

HalfOpenCylinder::HalfOpenCylinder(){

                height=10;

                radius=2;

}

HalfOpenCylinder::HalfOpenCylinder(double h, double r){

                height=h;

                radius=r;

}

double HalfOpenCylinder::surfaceArea() const{

                double pi=3.14159;

                //finding the surface area (in square inches) of a cylinder

                //which is closed on one end but open on the other.

                double area=(pi*radius*radius)+(2*pi*radius*height);

                return area;

}

//end of HalfOpenCylinder.cpp file

//Vase.hpp file

#ifndef Vase_H

#define Vase_H

#include "HalfOpenCylinder.hpp"

class Vase{

                //attributes

                HalfOpenCylinder cylinder;

                double cost_per_sq_inch;

                public:

                //public methods

                Vase(HalfOpenCylinder c, double cost);

                double totalCost() const;

                bool costsMoreThan(Vase other) const;

               

};

#endif

//end of Vase.hpp file

//Vase.cpp file with main method

#include "Vase.hpp"

#include <iostream>

using namespace std;

//implementing all methods

Vase::Vase(HalfOpenCylinder cyl,double c){

                cylinder=cyl;

                cost_per_sq_inch=c;

}

double Vase::totalCost() const{

                return cylinder.surfaceArea()*cost_per_sq_inch;

}

bool Vase::costsMoreThan(Vase other) const{

                return totalCost()>other.totalCost();

}

//main method for testing

int main(){

                HalfOpenCylinder hoc1;

                Vase vase1(hoc1, 0.12);

                cout << vase1.totalCost() << endl;

                HalfOpenCylinder hoc2(80.4, 13);

                Vase vase2(hoc2, 0.2);

                // std::boolalpha prints Boolean values as "true" or "false" instead of an integer value

                cout << std::boolalpha << vase1.costsMoreThan(vase2) << endl;

}

/*OUTPUT*/

16.5876

false

Add a comment
Know the answer?
Add Answer to:
Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • C++, entry level no pointers or vectors. Write a class called Person that has two data...

    C++, entry level no pointers or vectors. Write a class called Person that has two data members - a string variable called name and a double variable called age. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods. Write a separate function (not part of the Person class) called stdDev that takes two parameters...

  • Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and...

    Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and will add various functionality. We will store the following private variables specific to Warehouse: . a std::vector of float values which indicate the monetary values of each sale made by this salesman . a std::string which stores that salesman's position title . a float which stores their commission percentage, i.e., the percentage of sales they receive as a paycheck . a float which stores...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Write a class named Taxicab that has three private data members: one that holds the current...

    Write a class named Taxicab that has three private data members: one that holds the current x-coordinate, one that holds the current y-coordinate, and one that holds the odometer reading (the actual odometer distance driven by the Taxicab, not the Euclidean distance from its starting point). The class should have an init method that takes two parameters and uses them to initialize the coordinates, and also initializes the odometer to zero. The class should have get members for each data...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...

    Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double variable that holds the rooms length(in feet). Breadth: The double variable that holds the rooms breadth(in feet). Height: Double variable that holds rooms height in feet. Color: String object that holds the color you want the room to be painted with. The class should have a constructor to initialize the fields for a particular instance (or object) of the class as given: All double...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT