Add to the test function a base class array of pointers of the same dimension as
ID: 3645441 • Letter: A
Question
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.Circle objects should display radius, circumference, and area.
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) binding using the assigned Shape class pointers to call the nonclass, member-display-information function.
I dont know how to do this. Here is the code i have so far.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
class Circle
{
float r;
Circle()
{
r=0;
}
Circle(float x)
{
r=x;
}
float Area()
{
return 4.3*r*r;
}
}
class Rect
{
float l,b;
Rect()
{
l=0;
b=0;
}
Rect(float x, float y)
{
l=x;
b=y;
}
float Area()
{
return l*b;
}
}
void main()
{
clrscr();
Circle c(4);
cout << "Area of circle is " << c.Area();
Rect r(3,6)
cout << "Area of Rectangle is" << r.Area();
getch();
}
Explanation / Answer
try this out - //Shape.c #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 #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 #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 #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 > r; cir.setRadius(r); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.