0

The task is to save Circle and Rectangle in one array.

We should make a switch first and then decice if it should run in testMode or not. I have coded the testMode until now.

In testArea() I notice that the sArray doesn't go to the Circle and Rectangle .area(). What did I do wrong?

main.cpp

#include "circle.h"
#include "rectancle.h"
#include <string>

const int MAXARRAY = 2;
int countArray=0;


void testNewCircle(Shape2D &sArray,const Circle & exp);
void NewCircle(Shape2D &sArray);

void testNewRectangle(Shape2D &sArray,const Rectangle & exp);
void NewRectangle(Shape2D &sArray);

void testScale(float scale, Shape2D &sArray,const Shape2D &exp);

void testMove(Shape2D&sArray, Point2D p,const Rectangle &exp);

void testTotalArea(Shape2D sArray[], const int exp);

void endResult(bool result);

void showAll();

int decision;

//to debug
void hoi(){
    cout<<"hoi"<<endl;
}

int main()
{
    Shape2D shapeArray[MAXARRAY];

    cout<< "Do you want to go to the test mode? Yes: 1    No: 0"<<endl;
    cin>>decision;

    switch(decision){
    case 1:
        cout<<"------ Circle Create --------"<<endl;
        testNewCircle(shapeArray[countArray],Circle(1,1,1,Color(1,1,1),1,1));

        cout<<"------ Rectangle Create --------"<<endl;
        testNewRectangle(shapeArray[countArray],Rectangle(1,1,1,1,Color(1,1,1),1,1));

        cout<<"------ Scaling --------"<<endl;
        testScale(2,shapeArray[1],Rectangle(1,1,2,2,Color(1,1,1),1,1));

        cout<<"------ MoveTo --------"<<endl;
        testMove(shapeArray[1],Point2D(3,3),Rectangle(3,3,2,2,Color(1,1,1),1,1));

        cout<<"------ Area --------"<<endl;
        testTotalArea(shapeArray,7);
        break;

    case 0:

        break;
    }


    return 0;
}

void testNewCircle(Shape2D &sArray,const Circle & exp){
    NewCircle(sArray);

    endResult(sArray==exp);

}
void NewCircle(Shape2D &sArray){
    if(countArray<MAXARRAY){
        switch(decision){
    case 1:
        sArray = Circle(1,1,1,Color(1,1,1),1,1);
        countArray++;
        break;

    case 0:
        sArray = Circle(1,1,1,Color(1,1,1),1,1);
        countArray++;
        break;
        }
    }else{
        cout<<"You hit the Limit of Array Elements! "<<endl;
    }
}

void testNewRectangle(Shape2D &sArray,const Rectangle & exp){

    NewRectangle(sArray);
    endResult(sArray==exp);
}
void NewRectangle(Shape2D &sArray){
if(countArray<MAXARRAY){
        switch(decision){
    case 1:
        sArray = Rectangle(1,1,1,1,Color(1,1,1),1,1);
        countArray++;
        break;

    case 0:
        sArray = Rectangle(1,1,1,1,Color(1,1,1),1,1);
        countArray++;
        break;
        }
    }else{
        cout<<"You hit the Limit of Array Elements! "<<endl;
    }
}

void testScale(float scale, Shape2D &sArray,const Shape2D &exp){
    sArray.scaling(scale);
    endResult(sArray==exp);
}

void testMove(Shape2D &sArray, Point2D p,const Rectangle &exp){
    sArray.moveTo(p);
    endResult(sArray==exp);
}

void testTotalArea(Shape2D sArray[], const int exp){
    int area=0;
    for(int i=0; i<countArray;i++){
        cout<<sArray[i];
        area +=sArray[i].area();
    }
}

void endResult(bool result){
    if(result){
            cout<<"--> Huge Success"<<endl<<endl;
    }else{
        cout<<"--> Huge Fail..."<<endl<<endl;
    }
}

circle.h

#include "point.h"
#include "shape2dPoly.h"

class Circle: public Shape2D{
public:
    Point2D center;
    float radius;
public:
    Circle(float x=0,float y=0, float rad=0,const Color &c=Color(0,0,0), int line=1, bool filled=false);
    Circle(float x,float y, float rad, Shape2D &s);

    virtual printOut()const;
    virtual int operator==(const Circle& c);

    virtual void moveTo(Point2D p);
    virtual float area();

    virtual void scaling(float scale);
    friend ostream& operator<<(ostream &o, const Circle &c);
};

rectangle.h

#include "point.h"
#include "shape2dPoly.h"

class Rectangle: public Shape2D{
public:
   Point2D lowerLeftPnt;
   int width;
   float height;

public:
    Rectangle(float x=0,float y=0, int lenght=0,float up=0,const Color &c=Color(0,0,0), int line=1, bool filled=false);
    Rectangle(float x,float y,  int lenght,float up,Shape2D &s);

    virtual printOut()const;
        virtual void moveTo(Point2D p);
        virtual float area();

    virtual int operator==(const Rectangle& c);
    virtual void scaling(float scale);
    friend ostream& operator<<(ostream &o, const Rectangle &c);
};

shape.h

#include "point.h"

#ifndef Shape2D_H
#define  Shape2D_H

class Shape2D{
public:
    Color color;
    int lineart;
    bool isFilled;

    static const int  SOLID = 1;
    static const int  DASHED = 2;
    static const int  DOTTED = 3;

public:
    Shape2D(const Color &c=Color(0,0,0), int line=0,bool filled=0);
    Shape2D(Shape2D &s);

    //Actually they are Pure virtual but I do it this way.
    virtual void scaling(float scale);
    virtual void moveTo(Point2D p);
    virtual float area();

    virtual printOut()const;
    virtual int operator==(const Shape2D& c);
    friend ostream& operator<<(ostream &o, const Shape2D &c);
};

#endif // Shape2D_H
Jusauria
  • 13
  • 2
  • 1
    You cannot have `Shape2D shapeArray[MAXARRAY];`. https://stackoverflow.com/questions/274626/what-is-object-slicing – Jeffrey Jan 11 '22 at 14:10
  • `Shape2D shapeArray[MAXARRAY];` is an array of `Shape2D`s. It cannot hold elements of other types. Possible duplicates: https://stackoverflow.com/questions/274626/what-is-object-slicing and https://stackoverflow.com/questions/52222665/array-of-polymorphic-objects – 463035818_is_not_an_ai Jan 11 '22 at 14:11
  • An array of `Shape2D` can only hold `Shape2D` objects and nothing else, not even derived objects (it can, but the object will be sliced). If you are coming from a Java or other similar language, this is the difference with C++ and polymorphism and those languages. – PaulMcKenzie Jan 11 '22 at 14:11
  • 1
    In C++, polymorphism **requires** the use of pointers (raw or smart) or references. Otherwise the object will get sliced. I recommend using `std::vector> shapeArray;` – Eljay Jan 11 '22 at 14:11

0 Answers0