0

I've a class of Shape to compute area of a shape among other attributes and functions, and my class Square inherits from it.

In main, I have a vector of shape pointers. After the user inputs their shape and coordinates, I have to store their coordinates into the shape object,then store the object into the vector itself.

I'm not sure how do I store an array into a object,or if it's even possible. Here's what I have tried.

//Global variables vector<Shape> *Shape; void CalculateShapeData() { //Variables declaration string shape; //Store x,y coordinates in array int tempx[100],tempy[100]; cout << "Please enter name of shape : " << endl; cin >> shape; cout << "Please enter special type: " << endl; if (shape == "Rectangle") { } else if (shape == "Square") { for (int i = 0; i < 4;i ++) { cout << "Enter x-coordinate of pt " << i << ":" << endl; //cin >> tempx[i]; cout << "Enter y-coordinate of pt " << i << ":" << endl; //cin >> tempy[i]; //Store coordinates into square object } } 

I've read an alternative here, but it uses structs. storing input into Arrays C++

I'm not sure if I can do it using arrays instead?

5
  • 1
    add a coordinate vector to the class instead of using global arrays? Commented Nov 15, 2014 at 3:52
  • But if I want to do it using arrays,is it still possible? @isim Commented Nov 15, 2014 at 3:53
  • yes, but what's the point? A shape can have an indefinite amount of points, so you should use a container that can hold an indefinite amount of items. Commented Nov 15, 2014 at 3:57
  • vector<Shape> *Shape; is a bad idea, use different name for the variable Commented Nov 15, 2014 at 4:01
  • I only need about 100 shapes though,and the container will hold the shapes. Commented Nov 15, 2014 at 4:03

1 Answer 1

0

A class certainly can contain an array, or members of any other valid type:

class Square: public Shape { private: Point vertices_ [4]; }; 

Question is: is that what you want? Or maybe you want one Point, plus a width and height.

...and how do you get info into the Square? Best way is through the ctor:

Square::Square (const Point& upperLeft, int width, int height); 

I see you've posted a comment saying: can't I use a global array? Well, sure, you can do anything you want. But it will be confusing and thus harder to write, debug, and maintain. If you MUST use a global array, Square can store not a Point but an index to that global array. But please don't. There's no need, and it's another level of complexity.

Sign up to request clarification or add additional context in comments.

3 Comments

Well,I just want to store coordinates only,no need for width and height. Just an array to store those. But I'm not sure on how to go about that, then storing it into the vector itself.
So it's using const Point for coordinates,and a vertices_ as Point as a array?
Yes: a Point is x and y, and vertices_ is an array of Points.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.