
Abe K.
asked 04/15/21I need the implementations for all the classes in this project in C++
This is the link to all the instructions to the project, there should be a Shape class as the abstract parent class and circle, triangle, rectangle children classes which inherit from shape. Should be written in C++.
https://maryash.github.io/235/projects/project-4/project-4.html
1 Expert Answer

Patrick B. answered 04/16/21
Math and computer tutor/teacher
#ifndef _SHAPE
#define _SHAPE
class Shape
{
protected:
int width;
int height;
int edges;
char ** displayChars;
public:
// Parameterized Constructor; there is no default constructor
Shape(const int &width, const int &height)
{
this->width = width; this->height = height;
displayChars=(char**)0;
}
// Getters
int getEdges() { return(edges); }
int getWidth() { return(width); }
int getHeight() { return(height); }
char **getDisplayChars();
// Setters
void setEdges(const int& edges) { this->edges = edges; }
void setWidth(const int& new_width) { this->width = new_width; }
void setHeight(const int &new_height) { this->height = new_height; }
void setDisplayChars(char **display);
// Mutators
void rotateRight(); //rotate by 90 degrees
void rotateLeft(); //rotate by 90 degrees
void reflect(char axis); //reflect over x or y axis
// Pure Virtual Methods (no implementation)
virtual double getSurfaceArea() = 0;
virtual double get3DVolume(const double& depth) = 0;
// Display - //iterate through 2D array and print chars
void display();
~Shape();
};
#endif
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <string.h>
#ifndef _SHAPE
#include "shape.h"
#endif
//MEMORY LEAK: CALLER MUST FREE the 2-D array that is returned!!!
// NEVER give the ORIGINAL address away...
char **Shape::getDisplayChars()
{
char ** a = (char**)malloc(height*sizeof(char*));
for (int iLoop=0; iLoop<height; iLoop++)
{
a[iLoop]= (char*)malloc(width);
memcpy(a[iLoop],displayChars[iLoop],width);
}
return(a);
}
Shape::~Shape()
{
for (int iLoop=0; iLoop<height; iLoop++)
{
if (displayChars[iLoop]!=NULL)
{
free(displayChars[iLoop]);
}
}
free(displayChars);
}
void Shape::display()
{
for (int iLoop=0; iLoop<this->height; iLoop++)
{
cout << displayChars[iLoop] << endl;
}
}
/*********************************
THESE ARE A BAD IDEAS
--------------------------
void Shape::setDisplayChars(char **display)
{
}
void Shape::rotateRight() //rotate by 90 degrees
{
}
void Shape::rotateLeft() //rotate by 90 degrees
{
}
void Shape::reflect(char axis)
{
}
*********************************/
#ifndef _CIRCLE
#define _CIRCLE
#ifndef _SHAPE
#include "shape.h"
#endif
class Circle : public Shape
{
protected:
double diameter;
double radius;
public:
Circle(const int&);
double GetDiameter() { return(diameter); }
double GetRadius() { return(radius); }
double getSurfaceArea();
double get3DVolume(const double& depth);
};
#endif
#include <math.h>
#ifndef _CIRCLE
#include "circle.h"
#endif
Circle::Circle(const int &diameter) : Shape(diameter,diameter)
{
setEdges(0);
this->diameter = diameter;
this->radius = diameter/2;
// Populate 2D array with empty chars
this->displayChars = new char *[this->height];
for (int row = 0; row < this->height; row++)
{
displayChars[row] = new char[this->width];
for (int col = 0; col < this->width; col++)
{
displayChars[row][col] = ' ';
}
}
// Populate the proper positions with *'s
int x_radius = diameter / 2;
int y_radius = (diameter / 2) - 1;
float dist = 0;
char ascii_counter = 48;
for (int col = 0; col <= getWidth() + 1; col++)
{
for (int row = 0; row <= getHeight() + 5; row++)
{
dist = sqrt((row - y_radius) * (row - y_radius) +
(col - x_radius) * (col - x_radius));
// dist in range: (radius - 0.5) to (radius + 0.5)
if (dist > y_radius - 0.5 && dist < y_radius + 0.5)
{
displayChars[row][col] = ascii_counter;
// fix ascii_counter to wrap around after
ascii_counter++;
if (ascii_counter > 126)
{
ascii_counter = 48;
}
}
}
}
}
double Circle::getSurfaceArea()
{
return(M_PI*this->radius*this->radius);
}
double Circle::get3DVolume(const double& depth)
{
return(4*M_PI/3*this->radius*this->radius*this->radius);
}
#include <iostream>
#ifndef _CIRCLE
#include "circle.h"
#endif
int main(int argc, char** argv)
{
int diameter=25;
Circle circle(diameter);
circle.display();
return 0;
}
Abe K.
would you be able to do the other shapes04/17/21
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
You did not answer my question. What are the EDGES? What are they for and what do they do? I see in the circle it sets the edges to ZERO.. How do we know which direction to populate the characters. In the Circle example they are populated down the column but in the rectangle/square they are along the edges. INCOMPREHENSIBLE!04/15/21