Question

In: Computer Science

Adding on to 5-8b with the rectangle - cube inheritance, we add on another subclass of...

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;
}
};

Solutions

Expert Solution

I have implemented the Main.cpp to create the three Objects and call the print() method. Please find the Following Code Screenshot, Output, and Code(at the End of the Post)

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE:

Main.cpp

#include"rectangle5-12.h"
#include"colorrectangle5-12.h"
#include"cube5-12.h"
/*
1. Create a main that declares an array of three rectangle pointers.
2. 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).
3.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.*/
int main(){
   /* 1*/
   rectangle512* r[3];
   /*2*/
   r[0]=new rectangle512(2,3);
   r[1]=new cube512(2,3,4);
   char col[]={'R','e','d','\0'};
   r[2]=new colorrectangle512(2,3,col);
   /*3*/
   for(int i=0;i<3;i++){
       cout<<"\nThe address of the Object is : "<<r[i]<<endl;
       r[i]->print();
   }
}

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

//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

//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;
}
};


Related Solutions

(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
XYZ wants to expand its operations by adding another product, which will be a 5-years project...
XYZ wants to expand its operations by adding another product, which will be a 5-years project with an investment cost of $115,000. If XYZ borrowed $115,000 at a yearly interest rate of 12% for 6 years, generate the loan amortization table (starting balance, interest payment, principal payment, ending balance) if they want to pay off the loan at the end of year 5. Show your calculations.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT