currently i found myself stuck on step 3 i have made some attempts but keep runn
ID: 3635322 • Letter: C
Question
currently i found myself stuck on step 3 i have made some attempts but keep running into problems.STEP 3: Add a Base Class Pointer Array and an Additional Function
Add to the test function a base class array of pointers of the same dimension as the total number of Circle and
Rectangle objects that were created in the previous step. Use this pointer array to access the Circle and the
Rectangle objects to call a new, nonclass member function that will display all the information about each object.
1. Circle objects should display radius, circumference, and area.
2. Rectangle objects should display dimensions, perimeter. and area.
The information-display function should accept as its calling parameter a pointer of the class Shape.
Run the test function to demonstrate static (early) binding using the derived class objects calling their member
functions, and run the test function to demonstrate dynamic (late)
Here is what i currently have any help would be appreciated
//Shape.h
#ifndef ShapeH
#define ShapeH
class Shape
{
public:
virtual double Area() const = 0;
virtual double Parimeter() const = 0;
private:
};
#endif
//Circle.h
#include "Shape.h"
class Circle: public Shape
{
public:
Circle();
void setRadius(const double r);
double getRadius() const;
virtual double Area() const;
virtual double Parimeter() const;
~Circle();
protected:
double Radius;
};
#endif
//Circle.cpp
#include <iostream>
#include "Circle.h"
using namespace std;
const double PI = 3.14159;
Circle::Circle()
{
}
void Circle::setRadius(const double r)
{
Radius = r;
}
double Circle::getRadius() const
{
return(Radius);
}
double Circle::Area() const
{
return (PI * Radius * Radius);
}
double Circle::Parimeter() const
{
return(2 * PI * Radius);
}
Circle::~Circle()
{
}
//Rectangle.h
#ifndef RectangleH
#define RectangleH
#include "Shape.h"
class Rectangle: public Shape
{
public:
Rectangle();
void setDimensions(int H, int W);
double getHeight();
double getWidth();
double Area() const;
double Parimeter() const;
~Rectangle();
private:
double Height;
double Width;
double A;
double P;
};
#endif
//Rectangle.cpp
#include <iostream>
#include "Rectangle.h"
using namespace std;
Rectangle::Rectangle()
{
}
void Rectangle::setDimensions(int W, int H)
{
Width = W;
Height = H;
}
double Rectangle::getHeight()
{
return(Height);
}
double Rectangle::getWidth()
{
return(Width);
}
double Rectangle::Area() const
{
return(Width * Height);
}
double Rectangle::Parimeter() const
{
return((2 * Width) + (2 * Height));
}
Rectangle::~Rectangle()
{
}
//Main.cpp
#include <iostream>
#include "Circle.h"
#include "Rectangle.h"
using namespace std;
//Prototype
void displayMenu(void);
char getMenuSelection(void);
void Cir(void);
void Rec(void);
void main()
{
char action;
double r;
double h;
double w;
Rectangle rec;
Circle cir;
do
{
displayMenu(); //displays the intitial menu
action = getMenuSelection(); //gets the menu selection
if(action == 'c' || action == 'C')
{
cout << "Enter the radius of the circle: ";
cin >> r;
cir.setRadius(r);
cout << "Radius: " << r << endl;
cout << "Area: " << cir.Area() << endl;
cout << "Parimeter: " << cir.Parimeter() << endl;
}
if(action == 'r' || action == 'R')
{
cout << "Enter the height of the rectangle: ";
cin >> h;
cout << "Enter the length of the rectangle: ";
cin >> w;
rec.setDimensions(h, w);
cout << "Dimensions= " << "Hight: " << h << " Width: " << w << endl;
cout << "Area: " << rec.Area() << endl;
cout << "Parimeter: " << rec.Parimeter() << endl;
}
if(action == 'l' || action == 'L')
{
}
}
while(action != 'q' || action != 'Q'); //loop until program has been quit
cin.ignore(2);
}
void displayMenu() //Displays the initial starting menu options
{
cout << "==================================================================" << endl;
cout << "|*************** Enter C to create a circle object **************|" << endl;
cout << "==================================================================" << endl;
cout << "|************** Enter R to create a rectangle object ************|" << endl;
cout << "==================================================================" << endl;
cout << "|****************** Enter L to print array list *****************|" << endl;
cout << "==================================================================" << endl;
cout << "|*********************** Enter Q To quit ************************|" << endl;
cout << "==================================================================" << endl;
}
char getMenuSelection() //Gets the menu selection
{
cout << "Enter your choice and press enter:";
char action;
cin >> action;
switch(action)
{
case 'c':
case 'C':
return action;
break;
case 'r':
case 'R':
return action;
break;
case 'l':
case 'L':
return action;
break;
case 'q':
case 'Q':
exit(0);
break;
default: //if menu selection is not valid let the user know
cout << " Invalid selection: try again" << endl;
displayMenu();
getMenuSelection();
break;
}
}
Explanation / Answer
Please Rate:Thanks Hope this will help in your project Introduction to Polymorphism Polymorphism is by far the most important and widely used concept in object oriented programming. Some of the widely used technologies and libraries like COM, MFC etc. have polymorphism as their foundation. If you look at all the original design patterns, almost every pattern uses polymorphism in its structure. Polymorphism is a mechanism that allows you to implement a function in different ways. Pointers to base class We have seen that it is possible to derive a class from a base class and that we can add functionality to member functions. One of the features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism takes advantage of this feature. Let’s take a look at an example of a base class and derived classes: #include using namespace std; class CPolygon { protected: int width, height; public: void setup (int first, int second) { width= first; height= second; } }; class CRectangle: public CPolygon { public: int area() { return (width * height); } }; class CTriangle: public CPolygon { public: int area() { return (width * height / 2); } }; int main () { CRectangle rectangle; CTriangle triangle; CPolygon * ptr_polygon1 = &rectangle; CPolygon * ptr_polygon2 = ? ptr_polygon1->setup(2,2); ptr_polygon2->setup(2,2); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.