Hi again and thanks in advance ! Last question !
I have to create a class called polygon (not normal) that has in private the number of sides and the length of each side and in public
that has 2 constructors (the one has as variables the number and each length (how is that possible), getlength (perimeter) , setpolygon , getedge (of a k side) .
Also then i have to make a subclass called rectangle that has constructor with length,height
getlength again ,getarea ...

What does rectangle inherits from polygon ?
Here's the code of the polygon class
but i have no idea what i'm supposed to create in that way dynamically

here's the code !

#ifndef POLYGON_H    
#define POLYGON_H

class Polygon
 {
 private : 
     int size;
     int *len;
 public:
     Polygon();
     Polygon(int k,int *l);
     ~Polygon();
     int getLength();
     void setPolygon(int k,int *l);
     int getEdge(int k);
 };

#endif

polygon.cpp

#include "polygon.h" 

Polygon::Polygon()
 {
 size=0;
 }     

Polygon::~Polygon()
 {
 delete [] len;
 }

Polygon::Polygon(int k,int *l)
 {
 size=k;
 len=new int[size];
 for(int i=0;i<size;i++)len[i]=l[i];
 }

int Polygon::getLength()
 {
 int sum=0;
 for(int i=0;i<size;i++) sum+=len[i];
 }

void Polygon::setPolygon(int k,int *l)
 {
 size=k;
 len=new int[size];
 for(int i=0;i<size;i++) len[i]=l[i];
 }

int Polygon::getEdge(int k)
 {
 return len[k];
 }

main.cpp

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
 {
 system("PAUSE");
 return EXIT_SUCCESS;
 }

Edited 4 Years Ago by pyTony: Code format mess clean up try, be careful next time!

A rectangle is a polygon with four sides where the opposite sides are equal. I imagine it would inherit everything that polygon has, plus add a constructor taking a length and a width and some getters and setters. Who wrote this code?

but i have no idea what i'm supposed to create in that way dynamically

I have no idea what you mean by this.

The task you have seems quite unusual. with regards to

2 constructors (the one has as variables the number and each length (how is that possible),

There a few options for this as the constructor will need a different number of arguments for each polygon type.

One option is to take from printf or iostream and use either va_args or istream as your argument. this allows you the felxibility to have any length of arguments. or you can use an object like vector<T> or a special object with a similar feature set say something like

    typedef SideType_t float;
    typedef std::vector<SideType_t> SideData_t;
    class ClPolygonSideDescriptor
    {
    public:
        void AddSide( SideType_t _length );

        size_t GetNumSides( void );
        SideType_t GetSide( size_t _side );

    private:
        SideData_t m_sideData;
    }

I think any of these approaches would be ok. The method keeps the interface class generic and expandable (im assuming thats what polygon will be )

Maybe its a push to far but i dont like seeing variable arguments passed with int size, int* data in C++ as its not really OOP design and its not exactly the safest code and makes it easy to make mistakes. using something like STL if it is available would be better in my opinion but thats just my take on it.

Edited 4 Years Ago by Kanoisa

This article has been dead for over six months. Start a new discussion instead.