Thread: More homework

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    More homework


    When in doubt I turn to you guys so thanks in advance.

    Instructions: Create a C++ program that uses a class named Triangle (shown below) in order to produce a subclass of Triangle called Prism. Use the program to create a Prism object that calculates the volume and surface area of the Prism when supplied values for a width, height, and length of the Prism. As you can see, the width and height are defined in the Triangle class. The volume of the Prism should be calculated by multiplying the area of the triangle by the length of the Prism.

    Prompt the user for the values of width, height, and length. Validate these values to make sure they are not negative. If a negative value is entered, force the user to make another entry. If all three values are zero, use a default constructor for Prism that takes the default value of width and height from Triangle and assigns a value of 10 to length in the default constructor of Prism. Within the subclass Prism include an overriding function named findArea() that computes the surface area of the prism (the sum of the areas of all five sides). Obtain the area of the end of the prism from the findArea() function in the Triangle class. Use this value and the area of each side to compute the total surface area of a Prism. (ie., surface area = 2 times the area of an end plus the three sides. You may assume that the ends are isosceles triangles ie., 2 sides are equal length.)

    You will need to compute the slanting side (ss) of the triangular end of the prism when computing the surface area. ss = square root of ( width * width + height * height)

    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    #include<math.h>
    using namespace std;
    
    class Triangle
    {
    protected: 
    		double width, height;
    public:
    		Triangle();
    		Triangle(double, double);
    		double getWidth();
    		double getHeight();
    		double findArea();
    };
    
    Triangle::Triangle()
    {
    	width =7;
    	height = 8;
    }
    
    Triangle::Triangle(double w, double h)
    {
    	width = w;
    	height = h;
    }
    
    double Triangle::findArea()
    {
    	return(width * height * 0.5);
    }
    
    double Triangle::getWidth()
    {
    	return width;
    }
    
    double Triangle::getHeight()
    {
    	return height;
    }
    
    
    class Prism : public Triangle
    {
    private:
    		double length;
    public:
    		Prism();
    		Prism(double);
    		double getLength();
    		double calcVolume();
    		double findArea();
    };
    
    Prism::Prism() : Triangle()
    {
    	length = 10;
    }
    
    Prism::Prism(double l) : Triangle(width,height)
    {
    	length = l;
    }
    
    double Prism::getLength()
    {
    	return length;
    }
    
    double Prism::calcVolume()
    {
    return length * Triangle::findArea();
    }
    
    double Prism::findArea()
    {
    	double ss, surfArea;
    	ss = sqrt(((.5 * width)*(.5 * width))+(height*height));
    	surfArea = (2*Triangle::findArea())+ 3*ss;
    	return surfArea;
    }
    
    int main()
    {
    	char loopValue = 'y';
    	double width, height, length;
    
            cout<<"===Lab 12 has successfully started for Drew Faber==="<<endl;
    
    	do{
    
    		cout<<endl;
    		cout<<"Enter the value for width   ";
    		cin>>width;
    
    		while(width < 0)
    		{
    			cout<<endl;
    			cout<<"The width may not be Negative! Please re-enter! ";
    			cin>>width;
    		}
    	
    		cout<<endl;
    		cout<<"Enter the value for height  ";
    		cin>>height;
    
    		while(height < 0)
    		{
    			cout<<endl;
    			cout<<"The height may not be Negative! Please re-enter! ";
    			cin>>height;
    		}
    
    		cout<<endl;
    		cout<<"Enter the value for length  ";
    		cin>>length;
    
    		while(length < 0)
    		{
    			cout<<endl;
    			cout<<"The length may not be Negative! Please re-enter! ";
    			cin>>length;
    		}
    
    
    
    		if(width == 0 && height == 0 && length == 0)
    		{
    			Prism myPrism;
    			cout<<"The width of the Prism is "<<myPrism.getWidth()<<endl;
    			cout<<"The height of the Prism is "<<myPrism.getHeight()<<endl;
    			cout<<"The length of the Prism is "<<myPrism.getLength()<<endl;
    			cout<<"The Volume is "<<myPrism.calcVolume()<<" cubic units"<<endl;
    			cout<<"The Surface Area is"<<myPrism.findArea()<<" square units"<<endl;
    		}
    		else
    		{
    			Prism myPrism(length);
    			cout<<"The width of the Prism is "<<myPrism.getWidth()<<endl;
    			cout<<"The height of the Prism is "<<myPrism.getHeight()<<endl;
    			cout<<"The length of the Prism is "<<myPrism.getLength()<<endl;
    			cout<<"The Volume is "<<myPrism.calcVolume()<<" cubic units"<<endl;
    			cout<<"The Surface Area is"<<myPrism.findArea()<<" square units"<<endl;
    		}
    
    		cout<<endl;
    		cout<<"Would you like to enter more data(Y/N) "<<endl;
    		cin>>loopValue;
    
    	}while(loopValue == 'y' || loopValue == 'Y');
    
    	cout<<endl;
    	cout<<"===Lab 12 has successfully terminated for Drew Faber===";
    	cout<<endl;
    	system("pause");
    }
    Havn't formated the output yet want to figure everything else out first. I'm a little lost on subclasses. I'm getting some funky numbers back.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Let's start by looking at your Prism constructor that takes one argument.

    Code:
    Prism::Prism(double l) : Triangle(width,height)
    Where are you getting the values for the width and height that you are using in the Triangle constructor? Your Prism constructor probably needs to be passed all three variables, the height, width and length.

    I'm getting some funky numbers back.
    What do you mean? Please provide a sample of your input, and what you expect the program to produce with this input and what your program is actually producing.

    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Yes, Prism needs to have all 3 width, height, length. I figure it just got them from triangle.

    sample of funy numbers

    http://imgur.com/iefmVW1


    so Prism should look like.

    Code:
    Prism::Prism(double l, double w, double h) : Triangle(width,height)
    {
    	length = l;
    	width = w;
    	height = h;
    }
    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    #include<math.h>
    using namespace std;
    
    class Triangle
    {
    protected: 
    		double width, height;
    public:
    		Triangle();
    		Triangle(double, double);
    		double getWidth();
    		double getHeight();
    		double findArea();
    };
    
    Triangle::Triangle()
    {
    	width =7;
    	height = 8;
    }
    
    Triangle::Triangle(double w, double h)
    {
    	width = w;
    	height = h;
    }
    
    double Triangle::findArea()
    {
    	return(width * height * 0.5);
    }
    
    double Triangle::getWidth()
    {
    	return width;
    }
    
    double Triangle::getHeight()
    {
    	return height;
    }
    
    
    class Prism : public Triangle
    {
    private:
    		double length;
    public:
    		Prism();
    		Prism(double, double, double);
    		double getLength();
    		double calcVolume();
    		double findArea();
    };
    
    Prism::Prism() : Triangle()
    {
    	length = 10;
    }
    
    Prism::Prism(double l, double w, double h) : Triangle(width,height)
    {
    	length = l;
    	width = w;
    	height = h;
    }
    
    double Prism::getLength()
    {
    	return length;
    }
    
    double Prism::calcVolume()
    {
    return length * Triangle::findArea();
    }
    
    double Prism::findArea()
    {
    	double ss, surfArea;
    	ss = sqrt(((.5 * width)*(.5 * width))+(height*height));
    	surfArea = (2*Triangle::findArea())+ 3*ss;
    	return surfArea;
    }
    
    int main()
    {
    	char loopValue = 'y';
    	double width, height, length;
    
    	cout<<"===Lab 12 has successfully started for Drew Faber==="<<endl;
    
    	do{
    		cout<<endl;
    		cout<<"Enter the value for width   ";
    		cin>>width;
    
    		while(width < 0)
    		{
    			cout<<endl;
    			cout<<"The width may not be Negative! Please re-enter! ";
    			cin>>width;
    		}
    	
    		cout<<endl;
    		cout<<"Enter the value for height  ";
    		cin>>height;
    
    		while(height < 0)
    		{
    			cout<<endl;
    			cout<<"The height may not be Negative! Please re-enter! ";
    			cin>>height;
    		}
    
    		cout<<endl;
    		cout<<"Enter the value for length  ";
    		cin>>length;
    
    		while(length < 0)
    		{
    			cout<<endl;
    			cout<<"The length may not be Negative! Please re-enter! ";
    			cin>>length;
    		}
    
    
    
    		if(width == 0 && height == 0 && length == 0)
    		{
    			Prism myPrism;
    			cout<<"The width of the Prism is "<<myPrism.getWidth()<<endl;
    			cout<<"The height of the Prism is "<<myPrism.getHeight()<<endl;
    			cout<<"The length of the Prism is "<<myPrism.getLength()<<endl;
    			cout<<"The Volume is "<<myPrism.calcVolume()<<" cubic units"<<endl;
    			cout<<"The Surface Area is"<<myPrism.findArea()<<" square units"<<endl;
    		}
    		else
    		{
    			Prism myPrism(length, width, height);
    			cout<<"The width of the Prism is "<<myPrism.getWidth()<<endl;
    			cout<<"The height of the Prism is "<<myPrism.getHeight()<<endl;
    			cout<<"The length of the Prism is "<<myPrism.getLength()<<endl;
    			cout<<"The Volume is "<<myPrism.calcVolume()<<" cubic units"<<endl;
    			cout<<"The Surface Area is"<<myPrism.findArea()<<" square units"<<endl;
    		}
    
    		cout<<endl;
    		cout<<"Would you like to enter more data(Y/N) "<<endl;
    		cin>>loopValue;
    
    	}while(loopValue == 'y' || loopValue == 'Y');
    
    	cout<<endl;
    	cout<<"===Lab 12 has successfully terminated for Drew Faber===";
    	cout<<endl;
    	system("pause");
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Close but Prism doesn't use anything but the length and I also suggest you use the initialization list to initialize the member variables.

    Code:
    Prism::Prism(double l, double w, double h) : Triangle(width,height), length(l)
    {
    }
    
    Or:
    
    Prism::Prism(double l, double w, double h) : Triangle(width,height)
    {
    	length = l;
    }
    Jim
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    getting there


    Okay, think I'm getting pretty close when I use values to invoke the default constructors everything works fine but if I invoke the defualt values myself I get a completely different awnser heres a sample of my output

    http://imgur.com/g9Lz7Br
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Please post your current code.

    When I changed your Prism constructor to the following it seems to work properly.

    Code:
    Prism::Prism(double l, double w, double h) : Triangle(w,h), length(l)
    {
       // Blank body.
    }
    Jim
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    #include<math.h>
    using namespace std;
    
    class Triangle
    {
    protected: 
    		double width, height;
    public:
    		Triangle();
    		Triangle(double, double);
    		double getWidth();
    		double getHeight();
    		double findArea();
    };
    
    Triangle::Triangle()
    {
    	width =7;
    	height = 8;
    }
    
    Triangle::Triangle(double w, double h)
    {
    	width = w;
    	height = h;
    }
    
    double Triangle::findArea()
    {
    	return(width * height * 0.5);
    }
    
    double Triangle::getWidth()
    {
    	return width;
    }
    
    double Triangle::getHeight()
    {
    	return height;
    }
    
    
    class Prism : public Triangle
    {
    private:
    		double length;
    public:
    		Prism();
    		Prism(double, double, double);
    		double getLength();
    		double calcVolume();
    		double findArea();
    };
    
    Prism::Prism() : Triangle()
    {
    	length = 10;
    }
    
    Prism::Prism(double l, double w, double h) : Triangle(width,height), length(l)
    {
    }
    
    double Prism::getLength()
    {
    	return length;
    }
    
    double Prism::calcVolume()
    {
    return length * Triangle::findArea();
    }
    
    double Prism::findArea()
    {
    	double ss, surfArea;
    	ss = sqrt(((.5 * width)*(.5 * width))+(height*height));
    	surfArea = (2*Triangle::findArea())+ 3*ss;
    	return surfArea;
    }
    
    int main()
    {
    	char loopValue = 'y';
    	double width, height, length;
    
    	cout<<"===Lab 12 has successfully started for Drew Faber==="<<endl;
    
    	do{
    		cout<<endl;
    		cout<<"Enter the value for width   ";
    		cin>>width;
    
    		while(width < 0)
    		{
    			cout<<endl;
    			cout<<"The width may not be Negative! Please re-enter! ";
    			cin>>width;
    		}
    	
    		cout<<endl;
    		cout<<"Enter the value for height  ";
    		cin>>height;
    
    		while(height < 0)
    		{
    			cout<<endl;
    			cout<<"The height may not be Negative! Please re-enter! ";
    			cin>>height;
    		}
    
    		cout<<endl;
    		cout<<"Enter the value for length  ";
    		cin>>length;
    
    		while(length < 0)
    		{
    			cout<<endl;
    			cout<<"The length may not be Negative! Please re-enter! ";
    			cin>>length;
    		}
    
    
    
    		if(width == 0 && height == 0 && length == 0)
    		{
    			Prism myPrism;
    			cout<<"The width of the Prism is "<<myPrism.getWidth()<<endl;
    			cout<<"The height of the Prism is "<<myPrism.getHeight()<<endl;
    			cout<<"The length of the Prism is "<<myPrism.getLength()<<endl;
    			cout<<"The Volume is "<<myPrism.calcVolume()<<" cubic units"<<endl;
    			cout<<"The Surface Area is"<<myPrism.findArea()<<" square units"<<endl;
    		}
    		else
    		{
    			Prism myPrism(length, width, height);
    			cout<<"The width of the Prism is "<<myPrism.getWidth()<<endl;
    			cout<<"The height of the Prism is "<<myPrism.getHeight()<<endl;
    			cout<<"The length of the Prism is "<<myPrism.getLength()<<endl;
    			cout<<"The Volume is "<<myPrism.calcVolume()<<" cubic units"<<endl;
    			cout<<"The Surface Area is "<<myPrism.findArea()<<" square units"<<endl;
    		}
    
    		
    		cout<<"Would you like to enter more data(Y/N) "<<endl;
    		cin>>loopValue;
    
    	}while(loopValue == 'y' || loopValue == 'Y');
    
    	cout<<endl;
    	cout<<"===Lab 12 has successfully terminated for Drew Faber===";
    	cout<<endl;
    	system("pause");
    }

    when I change my non default constuctor to
    Code:
    Prism::Prism(double l, double w, double h) : Triangle(width,height)
    {
       width = w;
       length = l;
       height= h;
    
    }
    it works fine.....
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    The problem with the following snippet is:
    Code:
    Prism::Prism(double l, double w, double h) : Triangle(width,height), length(l)
    {
    }
    The arguments you are using in the call to the Triangle() constructor don't match the parameters in your Prism() constructor. In the Prism constructor you use the variable names of l, w, and h. You must use these names when you call the Triangle() constructor.

    Code:
    Prism::Prism(double l, double w, double h) : Triangle(w, h), length(l)
    {
    }
    Jim
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo