C++ Email solution to rolphmunz@yahoo.com Will pay $50 with paypal. 1. Define th
ID: 3859678 • Letter: C
Question
C++ Email solution to rolphmunz@yahoo.com Will pay $50 with paypal.
1. Define the class Shape with two private data members width and height (both double). The Shape class should have a default constructor as well as constructor with parameters. The class should have get and set methods for its private data members. The Shape class must also have a method called area() that returns an integer. For the implementation of the area() method simply use “cout” to print the “base class” and return 0.
A. Include the exception handling routine to set methods for width and height. The set methods should throw runtime_error exception with the message “ bad input”.
B. Define the class Rectangle derived from shape. Rectangle has no additional private data members. But it overrides the method area() in Shape class by computing and returning the area of the rectangle.
C. Define the class Triangle derived from shape. Triangle has no additional private data members. But it overrides the method area() in Shape class by computing and returning the area of the Triangle. The Triangle object interprets the width as the base of the triangle “b” and height as the height “h” of the triangle. The area is given by b*h/2.
D. Call the set method of the shape class to set its value to a negative number. Catch the exception. No need to allow user to reenter value.
E. Demonstrate polymorphism for the classes shape, rectangle, and Triangle. Note that the class has no print method, just call and print the value returned by the area() method when demonstrating polymorphism.
2. Write a function template displayList() that receives a list container as a parameter and prints its elements. Write a main program to test displayList() by passing it the list created in part 1.
3. Write a function template displayVector() that receives a vector container as a parameter and prints its elements. Write a main program to test displayVector() by passing it the vector created in part 1.
4. In problem 2 and 3 we had to write two function templates that really did the same thing, which was printing containers. Use the for_each() method to implement a strategy that can print both a vector and a list.
Explanation / Answer
/*list test*/
#include <list>
/* vector test*/
#include <string>
#include <vector>
/* input output general include*/
#include <iostream>
using namespace std;
class MyContainer {
protected:
// list of double
list<double> numList;
// list of strings
vector<double> my_double;
public:
// creates a predefined Vector
void setVector(){
my_double.push_back( 12.7);
my_double.push_back( 9.65);
my_double.push_back( 8.72);
my_double.push_back( 4.69);
}
//creates a predefined List
void setList(){
//add the values 12.7, 9.65, 8.72 and 4.69 to numList
numList.push_back(12.7);
numList.push_back(9.65);
numList.push_back(8.72);
numList.push_back(4.69);
}
//create a default List
MyContainer(){
setList();
setVector();
}
//display List
void displayList(){
cout << " "<< endl;
cout << " "<< endl;
cout << "---------------"<< endl;
cout << "List contains: "<< endl;
cout << "---------------"<< endl;
cout << endl;
//display the list using an iterator
list<double>::iterator itt = numList.begin();
itt++;
itt++;
for(itt = numList.begin(); itt != numList.end(); itt++)
cout<<*itt<<" ";
}
//display Vector
void displayVector(){
cout << " "<< endl;
cout << " "<< endl;
cout << "---------------"<< endl;
cout << "Vector contains: "<< endl;
cout << "---------------"<< endl;
cout << endl;
for (size_t n = 0; n < my_double.size(); n++)
cout << my_double[ n ] << " ";
cout << endl;
}
// display List and Vector using for each()
// function_tbd()
};
class Shape {
private:
double _width, _height;
/*get set methods for private members*/
public:
void setWidth( double w ) {
try{
_width = w;
/* catch the exception for negative number */
if (w < 0 ){
throw "width cannot be negative";}
}
catch (const char* msg) {
cerr << msg << endl;
throw msg; //* throw the error further
}
}
void setHeight( double h) {
try{
_height = h;
/* catch the exception for negative number */
if (h < 0 ) {
throw "height cannot be negative";}
}
catch (const char* msg) {
cerr << msg << endl;
throw msg; //* throw the error further
}
}
double width() const { return _width; }
double height() const { return _height; }
public:
/* default constructor for Shape*/
Shape(){
setWidth(0);
setHeight(0);
}
/* constructor with parameters*/
Shape( int a = 0, int b = 0) {
setWidth(a);
setHeight(b);
}
/* print area*/
virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Rectangle class area :" ;
return (width() * height());
}
};
class Triangle: public Shape{
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Triangle class area :" ;
return (width() * height() / 2);
}
};
// Main function for the program
int main( ) {
Shape *shape;
Rectangle rec;
Triangle tri;
//Rectangle rec(10,7);
//Triangle tri(10,5);
cout << "TEST POLYMORPHISM USING SHAPE CLASS EXAMPLE" << endl;
cout << "-------------------------------------------" << endl;
cout << endl;
try
{
rec.setWidth(10);
rec.setHeight(7);
tri.setWidth(10);
tri.setHeight(5);
}
catch(const char * msg){
cerr << "There was an error. Stop program" << endl;
//in case of an error do not continue ahead with the program.
return -1;
}
// store the address of Rectangle
shape = &rec;
// call rectangle area.
cout << shape->area() << endl;
// store the address of Triangle
shape = &tri;
// call triangle area.
cout << shape->area() << endl;
// QUESTION 2
cout << endl;
cout << "-----------------------------------------------" << endl;
cout << "TEST CONTAINER EXAMPLE USING LIST AND VECTOR" << endl;
cout << "-----------------------------------------------" << endl;
cout << endl;
MyContainer container;
container.displayList();
container.displayVector();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.