Creating a Poligon class

Hi all!
I created a Poligon class ( http://www.math.com/tables/geometry/polygons.htm ) and the code is the following:
Poligono.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef POLIGONO_H
#define POLIGONO_H
#include <string>
using namespace std;
class Poligono{
public:
	Poligono();
	Poligono(int);
	void setNome(string novo_nome);
	string getNome();
private:
	string nome;
	int numLados;
};
#endif	//POLIGONO_H 

Poligono.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Poligono.h"

Poligono::Poligono(){
	numLados = 0;
	nome="Não definido!";
}

Poligono::Poligono(int lados){
	numLados = lados;
	switch(lados){
		case 3: nome = "triângulo"; break;
		case 4: nome = "quadrado"; break;
		case 5: nome = "pentágono"; break;
		default:nome = "Não definido!"; numLados = 0;
	}
}

void Poligono::setNome(string novo_nome){nome = novo_nome;}

string Poligono::getNome(){return nome;}

a main.cpp test file
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Poligono.h"
using namespace std;
int main(){
	Poligono figura1;
	Poligono figura2(3);
	cout << "Nome : " << figura1.getNome() << endl;
	cout << "Nome : " << figura2.getNome() << endl;
}


The members are names in Portuguese but i think that it's easy to understand.
The Poligono class allows me to define the name and the number of side of the poligon i am creating.
Now imagine i want to store the lengh of the sides in a data-member, according to the value of numLados defined by the constructor?!
Help apreciated.
Thanks
I think you need to add an argument to the constructor:
1
2
3
4
5
6
7
8
9
10
Poligono::Poligono(int lados, unsigned int length){
	numLados = lados;
	sideLength = length;//sideLength is the member storing the length           
	switch(lados){
		case 3: nome = "triângulo"; break;
		case 4: nome = "quadrado"; break;
		case 5: nome = "pentágono"; break;
		default:nome = "Não definido!"; numLados = 0;
	}
}

Why do you have the getNome and setNome functions? I think making the variable nome to be public would be better
Last edited on
There is a language problem here :)
"lado" already stores the number of sides of the poligon ... wish makes length redundant.
I was thinking of an array with the capacity to store "lado" x length(float).
In a square: lado = 4 ... so the array shold be float ladolength[4];
Notice that square is a specific figure of a poligon with four side (don't know if the word side is the most apropriate :).

About the getNome and setNome ... assuminng that only the class can handle them is the best aproch ... and after this i will create other classe derivating this one.

Imagine a Triangle class ... i can use the Poligon members and add new and specific ones.

What i don know is if the array will work ... as it depends from other data-member ... at least did't on my first try .

The question should be if it possible to create an array in the private section to store the lengths off all the sides of the poligon and set it's dimension acording to the value received by the constructor?
You should try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Poligono
{
    //...
    unsigned int *length;//Will be a dinamic array
    public:
        Poligono(int lados, unsigned int[] lengths)
        {
            //...
            length = new unsigned int[lados];
            for (i=0; i<lados; i++)
                length[i] = lengths[i];
        }
}
@Bazzy: name is private/protected and the set/get methods are there because it's best practice OOD. It's one of the 3 key guidelines of OOD (Encapsulation)
With the exception of a triangle, a polygon cannot be defined only by the lengths of the sides.
To define a polygon, you need to define either the position of each vertex or the length and angle of each side.
You could use a vector or linked list or something dynamic like that to collect the information.
Yeah ... that is correct.
With more then 3 sides i will have a lot of things to deal with.
Not a good example for the purpose i intend.
But i will try it out with a dynamic data structure just to know if it works.
Thanks all
Last edited on
Topic archived. No new replies allowed.