Constructor with array parameter

Hello!

Firstly I'll state that I'm trying to solve a school exercise - so please only provide hints :).

I'm trying to create two classes: vertex and polygon. The polygon's constructor use an array of vertices to create a figure.

I haven't worked with multiple .cpp-files, or headers before - yet I think I got them right. My problem is that I don't understand how to create a constructor for the Polygon that uses an array as a parameter.

I want:
1/ the Polygon-class to have an instance variable array, i.e. m_vertices
2/ the Polygon-constructor is contacted with an array parameter
3/ the constructor creates a new array with the same size as the parameter array, and points the instance variable to it
4/ the constructor then assigns each of the parameter elements to the new array, i.e. for-loop that assigns constructorArray[i] = parameterArray[i] for all elements.

Yet I'm unable to do it - so I've done some research. If I understand it correctly the array I pass to the constructor doesn't have any information about the arrays size, or what each of its indexes contain. And instead of specifying the instance variable as an array, it seems I have to specify that it's an array pointer (?).

I've recently started studying programming - so if it seems I'm missing something obvious - I most likely am.

Although I think my error is in the Polygon.cpp-code I'll provide both .cpp-files and the header-file.

Header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #pragma once
#define HEADER_H

// content of .h file
class Vertex
{
	double m_positionX;
	double m_positionY;

	public:
		Vertex();
		Vertex(double positionX, double positionY);
		void setPositionX(double posX);
		void setPositionY(double posY);
		double getPositionX(void);
		double getPositionY(void);
};


Vertex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include "Header.h"
using namespace std;

// default Vertex constructor
Vertex::Vertex() {};

// Vertex constructor
Vertex::Vertex(double positionX, double positionY)
{
	setPositionX(positionX);
	setPositionY(positionY);
}

void Vertex::setPositionX(double positionX)
{
	m_positionX = positionX;
}

void Vertex::setPositionY(double positionY)
{
	m_positionY = positionY;
}

double Vertex::getPositionX(void)
{
	return m_positionX;
}

double Vertex::getPositionY(void)
{
	return m_positionY;
}


Polygon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include "Header.h"
using namespace std;

class Polygon
{
	private:
		Vertex* m_vertices;

	public:
		Polygon();
		Polygon(Vertex vertices);
};

// default Polygon constructor
Polygon::Polygon() {};

// Polygon constructor
Polygon::Polygon(Vertex vertices[], int length)
{
	m_vertices = Vertex[length];
	for (int i = 0; i < length; ++i)
	{
		m_vertices[i] = vertices[i];
	}
}



Your help is highly appreciated :).
Last edited on
1
2
3
4
5
6
7
8
9
// Polygon constructor
Polygon::Polygon(Vertex* vertices, int length)
{
	m_vertices = new Vertex[length];
	for (int i = 0; i < length; ++i)
	{
		m_vertices[i] = vertices[i];
	}
}
Can I ask you two follow up questions?
Does:
"Polygon::Polygon(Vertex* vertices, int length)"
mean that the Polygon constructor now asks for a pointer to an array?

Do you specify that something is a pointer by writing "*" after its datatype?

Thank you, Moschops, for taking time out of your weekend to help me : ).
mean that the Polygon constructor now asks for a pointer to an array?

No. Let's look at the parameter.
Vertex* vertices
The parameter is an object of type Vertex*, which is a pointer-to-vertex.

Do you specify that something is a pointer by writing "*" after its datatype?

Yes. If you're trying to use arrays, you need to stop now and learn about pointers. Come back to it when you know about pointers. http://www.cplusplus.com/articles/EN3hAqkS/
Last edited on
Yes. If you're trying to use arrays, you need to stop now and learn about pointers. Come back to it when you know about pointers. http://www.cplusplus.com/articles/EN3hAqkS/


Ok, will do! - thanks for the help and the link : ).
Topic archived. No new replies allowed.