So I am working on a faux graphics program for class. The current part that I am working on is to determine just from top left and bottom right corners what the coordinates should be. I believe my logic is good but it seems to be setting it as tl(3,1) tr 3,1 bl 5,1 br 5,1.
I don't see what I am doing wrong. If anyone can see, I would greatly appreciate it. It has to be written in this style because it needs to be expanded tremendously.

#include <iostream>
using namespace std;

class point{
    private:
        int x,y;
    public:
        point(){
            x = 0;
            y = 1;
        }
        void print(){
            cout<<x<<","<<y<<endl;
        }
        void set (int u,int v){
            x=u;
            y=v;
        }
        int getX(){
            return x;
        }
        int getY(){
            return y;
        }
        void setX(int n){
            x = n;
        }
        void setY(int n){
            x = n;
        }
};


class rectangle{
    private:
        point tl,tr,bl,br;
    public:
        rectangle();
        rectangle(point top_l, point bot_r);
        void print();
        void set(point top_l, point bot_r);
        point getTL();
        point getTR();
        point getBL();
        point getBR();
};

int main()
{
    point topleft;
    point bottomright;
    topleft.set(7,3);
    bottomright.set(2,5);

    rectangle first_rectangle(topleft,bottomright);


    first_rectangle.print();
}
rectangle::rectangle(){
    tl.setX(0);
    tl.setY(0);
    tr.setX(0);
    tr.setY(0);
    bl.setX(0);
    bl.setY(0);
    br.setX(0);
    br.setY(0);
}
rectangle::rectangle(point top_l, point bot_r){
    tl.setX(top_l.getX());
    tl.setY(top_l.getY());
    tr.setX(bot_r.getX());
    tr.setY(top_l.getY());
    bl.setX(top_l.getX());
    bl.setY(bot_r.getY());
    br.setX(bot_r.getX());
    br.setY(bot_r.getY());
}
void rectangle::print(){
    cout<<"The top left corner is: "<<tl.getX()<<","<<tl.getY()<<"."<<endl;
    cout<<"The top right corner is: "<<tr.getX()<<","<<tr.getY()<<"."<<endl;
    cout<<"The bottom left corner is: "<<bl.getX()<<","<<bl.getY()<<"."<<endl;
    cout<<"The bottom right corner is: "<<br.getX()<<","<<br.getY()<<"."<<endl;
}
void rectangle::set(point top_l, point bot_r){
    tl.setX(top_l.getX());
    tl.setY(top_l.getY());
    tr.setX(bot_r.getX());
    tr.setY(top_l.getY());
    bl.setX(top_l.getX());
    bl.setY(bot_r.getY());
    br.setX(bot_r.getX());
    br.setY(bot_r.getY());
}
point rectangle::getTL(){
    return tl;
}
point rectangle::getTR(){
    return tr;
}
point rectangle::getBL(){
    return bl;
}
point rectangle::getBR(){
    return br;
}
Comments
Nice eye-catching title :)

28-31

void setY(int n)
           {            
                    x = n;
           }

This should be

void setY(int n)
           {            
                    y = n;
           }

Please check for more errors.

Edited 6 Years Ago by thomas_naveen: n/a

So I am working on a faux graphics program for class. The current part that I am working on is to determine just from top left and bottom right corners what the coordinates should be. I believe my logic is good but it seems to be setting it as tl(3,1) tr 3,1 bl 5,1 br 5,1.
I don't see what I am doing wrong. If anyone can see, I would greatly appreciate it. It has to be written in this style because it needs to be expanded tremendously.

#include <iostream>
using namespace std;

class point{
    private:
        int x,y;
    public:
        point(){
            x = 0;
            y = 1;
        }
        void print(){
            cout<<x<<","<<y<<endl;
        }
        void set (int u,int v){
            x=u;
            y=v;
        }
        int getX(){
            return x;
        }
        int getY(){
            return y;
        }
        void setX(int n){
            x = n;
        }
        void setY(int n){
            x = n;
        }
};


class rectangle{
    private:
        point tl,tr,bl,br;
    public:
        rectangle();
        rectangle(point top_l, point bot_r);
        void print();
        void set(point top_l, point bot_r);
        point getTL();
        point getTR();
        point getBL();
        point getBR();
};

int main()
{
    point topleft;
    point bottomright;
    topleft.set(7,3);
    bottomright.set(2,5);

    rectangle first_rectangle(topleft,bottomright);


    first_rectangle.print();
}
rectangle::rectangle(){
    tl.setX(0);
    tl.setY(0);
    tr.setX(0);
    tr.setY(0);
    bl.setX(0);
    bl.setY(0);
    br.setX(0);
    br.setY(0);
}
rectangle::rectangle(point top_l, point bot_r){
    tl.setX(top_l.getX());
    tl.setY(top_l.getY());
    tr.setX(bot_r.getX());
    tr.setY(top_l.getY());
    bl.setX(top_l.getX());
    bl.setY(bot_r.getY());
    br.setX(bot_r.getX());
    br.setY(bot_r.getY());
}
void rectangle::print(){
    cout<<"The top left corner is: "<<tl.getX()<<","<<tl.getY()<<"."<<endl;
    cout<<"The top right corner is: "<<tr.getX()<<","<<tr.getY()<<"."<<endl;
    cout<<"The bottom left corner is: "<<bl.getX()<<","<<bl.getY()<<"."<<endl;
    cout<<"The bottom right corner is: "<<br.getX()<<","<<br.getY()<<"."<<endl;
}
void rectangle::set(point top_l, point bot_r){
    tl.setX(top_l.getX());
    tl.setY(top_l.getY());
    tr.setX(bot_r.getX());
    tr.setY(top_l.getY());
    bl.setX(top_l.getX());
    bl.setY(bot_r.getY());
    br.setX(bot_r.getX());
    br.setY(bot_r.getY());
}
point rectangle::getTL(){
    return tl;
}
point rectangle::getTR(){
    return tr;
}
point rectangle::getBL(){
    return bl;
}
point rectangle::getBR(){
    return br;
}

Take a close look at you point implementation. Might there be an error in this class anywhere?

28-31

void setY(int n)
           {            
                    x = n;
           }

This should be

void setY(int n)
           {            
                    y = n;
           }

Please check for more errors.

Thank you very much, that fixed the issue. I always make one careless error like this. Anyone have any advice on how to avoid careless errors?

Use in-code documentation.
Pay closer attention to what you are doing.
Think before you type.
Remember your program's structure.
Take notes.

Take your pick.

Edited 6 Years Ago by Fbody: n/a

There are a huge amount of resources on minimising errors. Designing the idea before you charge in for classes is a good idea.

For this case the use of the word const would cause the compiler to pick up the error

void set(const int x);

another common mistake that can be difficult to spot is:

if(x = 0)
{
//this will not run and x has been set to 0 
}

you can write in reverse

if(0 == x)
{
//this would have given a compile error with single =
}

In the long term having a test class to test every function you write can be beneficial. but normally this is too time consuming to be attractive until things have gone wrong

The first thing that you want to do is comment your code so that you know what you intended to do later

Choose meaningful function names.

There are many other pitfalls to watch out for and you will get better at spotting them if you keep your code short and in classes you will be able to reuse your code and so you can know where the error is.

The last thing I would recommend is always use braces {} for any if
- Spotting a wrongly matched else when they are not used can be very difficult

Thank you very much, that fixed the issue. I always make one careless error like this. Anyone have any advice on how to avoid careless errors?

Use in-code documentation.
Pay closer attention to what you are doing.
Think before you type.
Remember your program's structure.
Take notes.

Take your pick.

Actually, pick them all. And heavy on think. That's what the brain does best.

This question has already been answered. Start a new discussion instead.