0

Hi guys so I'm trying to draw a tower by implementing shapes using structures and classes in C++. Below is what I'm trying to draw using 6 different shapes: point, shape, rectangle, circle, triangle, square. I have to create .h and .cpp file for each shapes and I never really understood what to put on each .h and .cpp files for each shapes. I will include what I have so far on my main.cpp and I just would like to know if I'm going in the right direction as well as what kind of information/code would I have to write on each .h and .cpp file for each shapes.

Picture of what I'm trying to draw

main.cpp
#include <iostream>
#include "point.h"
#include "shape.h"
#include "rectangle.h"
#include "square.h"
#include "triangle.h"
#include "circle.h"
using namespace std;

int main(){
//create objects of the shapes
Rectangle r, r1;
Square s;
Triangle t;
Circle c, c1;

//first rectangle
r.setLineType('*');
r.moveBy(5, 5); //x and y coordinates
r.setHeight(5);
r.setWidth(20);
r.computeArea();
r.draw(); // draw a rectangle in 2D array

auto firstRectangleArea = r.computeArea();
auto firstRectangleCircumference = r.computerCircumference();

//second rectangle
r1.setLineType('*');
r1.moveBy(10, 0);
r1.setHeight(20);
r1.setWidth(5);
r1.computeArea();
r1.draw(); // draw a rectangle in 2D array

auto secondRectangleArea = r1.computeArea();
auto secondRectangleCircumference = r1.computeCircumference();

//triangle
t.setLineType('*');
t.moveBy(15, 0);
t.setHeight(5);
t.setBase(5);



//first circle
c.moveBy(15, 0);
c.setRadius(2);

auto firstCircleArea = c.computeArea();
auto firstCircleCircumference = c.computeCircumference();

//second circle
c1.setLineType('*');
c1.moveBy(6, 0);
c1.setRadius(4);

auto secondCircleArea = c1.computeArea();
auto secondCircleCircumference = c1.computeCircumference();
Evg
  • 25,259
  • 5
  • 41
  • 83
  • I'm still very new at coding and this is my first time posting for help on stackoverflow so please don't hesitate to give me suggestions on anything. – nalgeneboi Nov 28 '20 at 06:23
  • 1
    This might help: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm https://www.geeksforgeeks.org/bresenhams-circle-drawing-algorithm/ Seems like you will be able to draw what you are trying to but you may want to have some sort of working buffer to make it simple. For example a 2d array of `char`. Then you can just set "pixels" as per the outputs of the above algorithms. – David Ledger Nov 28 '20 at 06:28
  • There's no polymorphism in posted code. Please see e.g. [here](https://stackoverflow.com/questions/15188894/why-doesnt-polymorphism-work-without-pointers-references). In general you want (1) a *container* of smart pointers to your shapes and (2) *constructors* for each of your classes (avoid `set` functions). – n. m. could be an AI Nov 28 '20 at 07:57

0 Answers0