Adding on to 5-8b with the rectangle - cube inheritance, we add on another subcl
ID: 3582400 • Letter: A
Question
Adding on to 5-8b with the rectangle - cube inheritance, we add on another subclass of rectangle for a colored rectangle that adds a new field for the color, the constructors should either set an argument to the color or initialize the color to a default blue, and the print should call the rectangle print and then add on a statement to print the color.
The new version of the super class and two sub classes are attached.
Create a main that declares an array of three rectangle pointers. Each element should be a different kind of object that “is a” rectangle (rectangle, cube, colored rectangle), then separately run the three different constructors (you can send it any values that match up with the argument constructors) and assign to each element of the array (ex. spot 0 rectangle, spot 1 cube, spot 2 colored rectangle). Then in a for loop, print the address of each object that "is a" rectangle (the address in the pointer) and then use dynamic binding to call the print function to print each of the objects in the array.
-------------------------------------------------------------------------------------------------------------------------------------------------
//rectangle5-12.h
#ifndef rectangle512_h
#define rectangle512_h
#include<iostream>
using namespace std;
class rectangle512
{
protected:
float length;
float width;
float area;
float perimeter;
public:
rectangle512()
{
length = 1;
width = 1;
area = 1;
perimeter = 4;
}
rectangle512(float l, float w)
{
length = l;
width = w;
area = length * width;
perimeter = 2 * (length + width);
}
void virtual print()
{
cout << "Length is " << length << endl;
cout << "Width is " << width << endl;
cout << "Area is " << area << endl;
cout << "Perimeter is " << perimeter << endl;
}
};
#endif
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//colorrectangle5-12.h
#include"rectangle5-12.h"
#include<iostream>
#include<cstring>
using namespace std;
class colorrectangle512 : public rectangle512
{
private:
char color[20];
public:
colorrectangle512() : rectangle512()
{
strcpy(color, "blue");
}
colorrectangle512(float l, float w, char c[]) : rectangle512(l, w)
{
strcpy(color, c);
}
void print()
{
rectangle512::print();
cout << "Color is " << color << endl;
}
};
---------------------------------------------------------------------------------------------------------------------------------------
//cube5-12.h
#include"rectangle5-12.h"
#include<iostream>
using namespace std;
class cube512 : public rectangle512
{
private:
float depth;
public:
cube512() : rectangle512()
{
depth = 1;
area = 2 * length * width + 2 * length * depth +
2 * width * depth;
perimeter = 2 * (length + width) + 2 * (length + depth) +
2 * (width + depth);
}
cube512(float l, float w, float d) : rectangle512(l, w)
{
depth = d;
area = 2 * length * width + 2 * length * depth +
2 * width * depth;
perimeter = 2 * (length + width) + 2 * (length + depth) +
2 * (width + depth);
}
void print()
{
rectangle512::print();
cout << "Depth is " << depth << endl;
}
};
Explanation / Answer
#include"rectangle5-12.h"
#include"colorrectangle5-12.h"
#include"cube5-12.h"
#include<iostream>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
rectangle512 ** rec = new rectangle512*[3];
rectangle512[0]=new rectangle512;
rectangle512[1]=new colorrectangle512;
rectangle512[2]=new cube512;
rectangle512 *spot0;
colorrectangle512 *spot1;
cube512 *spot2;
spot0 = new rectangle512(8,3);
spot1 = new colorrectangle512(8,3,"yellow");
spot2 = new cube(8,3,5);
rectangle512[0]=spot0;
rectangle512[1]=spot1;
rectangle512[2]=spot2;
for(int i=0;i<3;i++)
{
cout<<rectangle512[i]->print;<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.