I have to create three shapes, Square, Cross, Rectangle. This makes up three classes.

There is one super class, Shape.

So basically when I did not try any of the polymorphism but I did declare:

class Shape{
private:
    int x,y;
public:
    ...
}

class Square: public Shape{

}

If I have x,y in Shape, I do not need x,y to exist in Square right?

I have this all working, like other methods including setX, and setY.

Is there any good methods to store my x and y datas?
For now, it is working fine when I did not use any virtual functions.
Previously I did use virtual functions.

I had two arrays.
Shape *arrayShapes[100];
Shape *dataCoords[100][12];

I am using two arrays because I felt that this way I can store the data of the coords better, when I am trying to extract data out.

i.e
int count=0;
If a user enters the name of the shape, it will cin into "shapeName", then into arrayShapes[count]->setName(shapeName);
The first would be a square, hence when the user is trying to input the coords, I will send them into
dataCoords[count][sides];
So the user will enter (0,0), (0,2), (2,2), (2,0). This 4 points makes up the square.

The problem arises here. When I am using virtual function to do this, my program hangs and closes. But when I remove all virtual function and continue my work normally, it is fine. Everything prints out accordingly.

Is there any better way to store my coords and how do I work around this to include polymorphism and virtual function? I need it to be dynamic.

Any read-ups is also good for me. Need a reference/tutorial.
Please help!

Recommended Answers

All 5 Replies

I assume here that the x and y coordinates refer to a particular point in the shape, such as the centerpoint, or the top left.

If I have x,y in Shape, I do not need x,y to exist in Square right?
Correct, they will be inherited by the child classes. You might consider declaring them protected rather than private, however, as it would make them visible within the child classes; you wouldn't need to use the setter/getter methods as much.

You might also want to make Shape and abstract class; you wouldn't want anyone trying to declare a Shape object directly, only as a member of one of the sub-classes.

Is there any good methods to store my x and y datas?
Well, you could collect them into a Point structure or class, I suppose. Right now, you are storing them in the class just fine, I think, though a Point class would make it a bit more explicit and regular.

For now, it is working fine when I did not use any virtual functions.
Previously I did use virtual functions.

Odd. What nethods did you have as virtual, and how did they fail? Could you post more of your code, please?

I had two arrays.

Shape *arrayShapes[100];
Shape *dataCoords[100][12];

I am using two arrays because I felt that this way I can store the data of the coords better, when I am trying to extract data out.

Za? OK, I'm confused, and more to the point, I suspect you are confused about how classes usually work.

Right now, you are declaring an array or Shape pointers. OK, all well and good, though I'm at a lost as to why you would need one hundred of them when the problem statement only calls for three. However, you don't show how you are initializing these pointers; you need something for them to point to, such as

arrayShapes[0] = new Square(4);  // 4 is the size of the square

As for dataCoords, here is where I think you're having a serious disconnect between your code and your objects. You have declared an array of 100 arrays of 12 Shape pointers each. You seem to want to have the individual points stored in separate Shape objects, which makes no sense.

Let's go over what the class declarations normally would be like. You would usually have the parent class Shape:

// shape.h
#ifndef SHAPE_H
#define SHAPE_H 1

class Shape
{
protected:
    int x, y;

public:
    // a simple inline constructor
    Shape(int new_x, int new_y): x(new_x), y(new_y)
    {
        return;
    };

    virtual ~Shape()
    {
        return;
    };

    // inline getter functions
    int getX() { return x; };
    int getY() { return y; };

    virtual void move(int new_x, int new_y);
    virtual void erase() = 0;    // abstract method for erasing the object
    virtual void display() = 0;  // abstract method for displaying the object
};

#endif

and a code file:

// shape.cpp
#include "shape.h"

void Shape::move(int new_x, int new_y)
{
    if (new_x < 0 || new_y < 0)
    {
        return;  // don't try to move off of screen
    }
    else
    {
        x = new_x;
        y = new_y;
        erase();
        display();
    }
}

Then you would have the sub-class:

// square.h
#ifndef SQUARE_H
#define SQUARE_H 1

#include "shape.h"

class Square : public Shape
{
protected:
    int size;

public:
    // a c'tor that calls the parent class's c'tor
    Square(int new_x, int new_y, int new_size): Shape(new_x, new_y), size(new_size)
    {
        return;
    };

    virtual void erase();
    virtual void display();
};

#endif

(I won't show the implementation file for Square, as I don't know how you intend to display the shapes. For now I've just left the bodies of erase() and display() as return;.)

And finally, the main file might be something like:

#include <iostream>
#include "shape.h"
#include "square.h"

using namespace std;

int main()
{
    Shape* s;

    s = new Square(5, 6, 4);

    s->display();

    delete s;
}

The point I'm making is, you wouldn't have a separate data structure to hold the points of the Shape objects, as those objects already have their points as part of the object.

#include <iostream>
#include <string>

using namespace std;

class Shape
{
    private:
        int x, y;
        string name;
    public:
        Shape(int = 0, int = 0, string = " ");
        string getName();
        void setName(string);
        void setX(int);
        void setY(int);
        int getX();
        int getY();
        void toString();

        double computeArea();
        bool isPointInShape(int, int);
        bool isPointOnShape();
};
Shape::Shape(int x2, int y2, string name2)
{
    x=x2;
    y=y2;
    name=name2;
}

void Shape::setName(string shapeName)
{
    name = shapeName;
}

string Shape::getName()
{
    return name;
}

double Shape::computeArea()
{
    return 0.0;
}

void Shape::setX(int newX)
{
    x = newX;
}

void Shape::setY(int newY)
{
    y = newY;
}

int Shape::getX()
{
    return x;
}

int Shape::getY()
{
    return y;
}

class Square: public Shape
{

};

class Assn2
{
    public:
        void mainMenu();
        char menuChoice;
        void stringToUpper(string &s);
};

void stringToUpper(string &s)
{
   for(unsigned int l = 0; l < s.length(); l++)
  {
    s[l] = toupper(s[l]);
  }
}

void Test::mainMenu()
{
    cout<<"Welcome to Test program!"<<endl<<endl;
    cout<<"1) Input data"<<endl;
    cout<<"2) Compute area"<<endl;
    cout<<"3) Print shapes"<<endl;
    cout<<"4) Sort shapes"<<endl;
    cout<<"Q) Enter 'Q' to quit"<<endl<<endl;
}

int main()
{
    char menuChoice;

    bool quit=false;
    Test test;
    int count=0;

    string shape, special;
    Shape arrayOfShapes[100];
    Shape dataOfCoords[100][12];

    while ( !quit )
    {
        test.mainMenu();
        cout<<"Please enter your choice : ";
        cin>>menuChoice;
        menuChoice = toupper(menuChoice);

        switch(menuChoice)
        {
                case '1':
                    cout<<endl<<"[ Input data ]"<<endl;

                    cout<<"Please enter name of shape : "<<endl;
                    cin>>shape;
                    stringToUpper(shape);
                    arrayOfShapes[count].setName(shape);
                    if(arrayOfShapes[count].getName() == "SQUARE")
                    {       
                        int tempX, tempY;
                        for(int a=0; a<4; a++)
                        {
                            cout<<"Please enter x-ordinate of pt. "<<a+1<<" : "<<endl;
                            cin>>tempX;
                            dataOfCoords[count][a].setX(tempX);
                            cout<<"Please enter y-ordinate of pt. "<<a+1<<" : "<<endl;
                            cin>>tempY;
                            dataOfCoords[count][a].setY(tempY);
                        }
                    }
                    break;
                case '2':
                    cout<<"Print"<<endl<<endl;
                    cout<<dataOfCoords[0][0].getX();
                    cout<<dataOfCoords[0][0].getY();
                    cout<<dataOfCoords[0][1].getX();
                    cout<<dataOfCoords[0][1].getY();
                    cout<<dataOfCoords[0][2].getX();
                    cout<<dataOfCoords[0][2].getY();
                    cout<<dataOfCoords[0][3].getX();
                    cout<<dataOfCoords[0][4].getY();
                    break;           
                case '3':
                    cout<<"You choosen 3"<<endl<<endl;
                    break;                   
                case '4':
                    cout<<"You choosen 4"<<endl<<endl;
                    break;
                case 'Q':
                    cout<<"You have chosen to quit!"<<endl<<endl;
                    quit=true;
                    exit(0);                 
                default:
                    cout<<"Invalid entry!"<<endl<<endl;
                    break;
        }
    }
}

Sorry for the truck load of codes, this is my coding without the virtual functions i had. To be honest, I do not know where am I heading at.

Could you please take a look at my codes?

Am I able to work along the lines of what you have given me? I need something to start with because I am totally confused about where should I go or start from.

Also, thanks alot Schol-R-LEA for taking your time to read my post. I have been waiting for something to help me out on this :/. Thanks alot!

I would be showing the x,y coordinates by the way. If I use a point, how do I extract them out?

If I have a square, the x,y coordinates will be at the four points, 0,0 , 0,2 , 2,2 , 2,0 (x,y) respectively.

I am thinking how do I retrieve the values when I need them.

I need to retrieve the values also, because I need to find out the PointsInShape and also PointsOnShape. Using the x,y coordinates above. Points on the shape 0,1 , 1,2 , 2,1 , 1,0 and the points in the shape is 1,1.

I kept reading up about virtual function but simply could not get a better idea of them. It was until when I try to implement them, I am lost.

Anyone can help??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.