Question

C++ problem 11-3 Chapter 10 defined the class circleType to implement the basic properties of a...

C++ problem 11-3

Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add the function print to this class to output the radius, area, and circumference of a circle.) Now every cylinder has a base and height, where the base is a circle. Design a class cylinderType that can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleType designed in Chapter 10. Some of the operations that can be performed on a cylinder are as follows: calculate and print the volume, calculate and print the surface area, set the height, set the radius of the base, and set the center of the base. Also, write a program to test various operations on a cylinder. Assume the value of \piπ to be 3.14159.

main.cpp , cylinderType.h and cylinderTypeImp.cpp are blank

-------------------------------------------------------------------------------------------

For circleType.h

//circleType.h

#ifndef circleType_H

#define circleType_H

class circleType

{

public:

    void print();

void setRadius(double r);

      double getRadius();

     double area();

      double circumference();

     circleType(double r = 0);

      private:

    double radius;

};

#endif

----------------------------------------------------

For circleTypeImp.cpp

//circleTypeImp.cpp

//Implementation File for the class circleType

#include <iostream>

#include "circleType.h"

using namespace std;

void circleType::print()

{

    cout << "Radius = " << radius

         << ", area = " << area()

         << ", circumference = " << circumference();

}

void circleType::setRadius(double r)

{

    if (r >= 0)

        radius = r;

    else

        radius = 0;

}

double circleType::getRadius()

{

    return radius;

}

double circleType::area()

{

    return 3.1416 * radius * radius;

}

double circleType::circumference()

{

    return 2 * 3.1416 * radius;

}

circleType::circleType(double r)

{

    setRadius(r);

}

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

Setup:

1) Make sure to place all the five files(circleType.h, circleTypeImp.cpp, cylinderType.h, cylinderTypeImp.cpp, main.cpp) in the same folder to avoid any linking errors.

2) When the files are all set-up, first compile the files together, to link them without errors, and then execute the executable file that is generated:

Compile and Link: g++ main.cpp cylinderType.cpp circleTypeImp.cpp
Execute: ./a.out

-----------------------------------------------------------------------------------------------------------------------------
Here is the code (with comments to explaining the logic ):

1. cylinderType.h

2. CylinderTypeImp.cpp

3. main.cpp


-----------------------------------------------------------------------------------------------------------------------------
Here is the output:

-----------------------------------------------------------------------------------------------------------------------------
Here is the same code (Without comments, for better readability):

1. cylinderType.h

2. CylinderTypeImp.cpp

3. main.cpp

-----------------------------------------------------------------------------------------------------------------------------
Here is the same code (available to copy-paste onto your compiler/text-editor for fast testing):

1. cylinderType.h

 #ifndef cylinderType_H #define cylinderType_H #include "circleType.h" class cylinderType : public circleType { private: double height, radius, x, y; public: void print(); void setRadius(double r); double getRadius(); void setHeight(double h); double getHeight(); void setCenter(double a, double b); double surfaceArea(); double volume(); cylinderType(double x = 0, double y = 0, double r = 0, double h = 0); }; #endif 

-----------------------------------------------------------------------------------------------------------------------------

2. CylinderTypeImp.cpp

 #include <iostream> #include "cylinderType.h" using namespace std; void cylinderType::print() { cout << "Radius = " << radius << ", Height = " << height << ", Center = (" << x << ", " << y << ") " << ", Surface Area = " << surfaceArea() << ", Volume = " << volume() << "\n\n"; } void cylinderType::setRadius(double r) { if (r >= 0) radius = r; else radius = 0; } double cylinderType::getRadius() { return radius; } void cylinderType::setHeight(double h) { if (h >= 0) height = h; else height = 0; } double cylinderType::getHeight() { return height; } void cylinderType::setCenter(double a, double b) { x = a; y = b; } double cylinderType::surfaceArea() { circleType base(radius); double base_area = base.area(); double curved_surface_area = base.circumference() * height; return 2 * base_area + curved_surface_area; } double cylinderType::volume() { circleType base(radius); double volume = base.area() * height; return volume; } cylinderType::cylinderType(double r, double h, double x, double y) { setRadius(r); setHeight(h); setCenter(x, y); }

-----------------------------------------------------------------------------------------------------------------------------

3. main.cpp

 #include <iostream> #include "cylinderType.h" using namespace std; int main() { cylinderType cylinder1(10, 10, 0, 0); cylinder1.print(); cylinder1.setCenter(1, 1); cylinder1.print(); cylinder1.setHeight(20); cylinder1.print(); cylinder1.setRadius(20); cylinder1.print(); double surfaceArea = cylinder1.surfaceArea(); double volume = cylinder1.volume(); cout << "surface Area = " << surfaceArea << ", Volume = " << volume << endl; } 


-----------------------------------------------------------------------------------------------------------------------------

If you have any doubts, feel free to ask in the comments.
If you found this answer helpful, please consider giving a like :)

Add a comment
Know the answer?
Add Answer to:
C++ problem 11-3 Chapter 10 defined the class circleType to implement the basic properties of a...
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
  • C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add t...

    C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add the function print to this class to output the radius, area, and circumference of a circle.) Now every cylinder has a base and height, where the base is a circle. Design a class cylinderType that can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleType designed in chapter 10.   Some of the...

  • Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add the...

    Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add the function print to this class to output the radius, area, and circumference of a circle.) Now every cylinder has a base and height, where the base is a circle. Design a class cylinderTypethat can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleTypedesigned in Chapter 10. Some of the operations that can...

  • In C++ Amanda and Tyler opened a business that specializes in shipping liquids, such as milk,...

    In C++ Amanda and Tyler opened a business that specializes in shipping liquids, such as milk, juice, and water, in cylindrical containers. The shipping charges depend on the amount of the liquid in the container. (For simplicity, you may assume that the container is filled to the top.) They also provide the option to paint the outside of the container for a reasonable amount. Write a program that does the following: Prompts the user to input the dimensions (in feet)...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius;...

    #include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • Please add //comments to the header file below. #ifndef CIRCLE_H_INCLUDED #define CIRCLE_H_INCLUDED #include <iostream> using namespace...

    Please add //comments to the header file below. #ifndef CIRCLE_H_INCLUDED #define CIRCLE_H_INCLUDED #include <iostream> using namespace std; class Circle { private: double radius; // declaration of public methods in header file public: Circle() { // default value radius = 1; } void input() { cout << "Enter radius: "; cin >> radius; } void print() { double PI = 3.14159; double area = PI * radius * radius; double circumference = 2 * PI * radius; cout << "Circle with...

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