Thread: graphics in c++

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    graphics in c++

    hey guys ! i am new to graphics in c++ .. i have to develop an application called " paint " . i have to implement it using classes ( and inheritance in classes ) . For example i have to draw a line using mouse . All the graphic functions are implemented in the class Graphics.h file. my code is pasted and the exact problem lies below :
    Code:
    
    
    Code:
    • #include<iostream>
    • usingnamespace std;
    • classPoint
    • {
    • protected:
    • int x1 ;
    • int y1 ;
    • public:
    • Point(int _x ,int _y)
    • {
    • x1 = _x ;
    • y1 = _y ;
    • }
    • voidSetX(int _x)
    • {
    • x1 = _x ;
    • }
    • voidSetY(int _y)
    • {
    • y1 = _y ;
    • }
    • intGetX()const
    • {
    • return x1 ;
    • }
    • intGetY()const
    • {
    • return y1 ;
    • }
    • ~Point()
    • {}
    • };
    • #include<iostream>
    • usingnamespace std;
    • #include"Point.h"
    • classLine:publicPoint
    • {
    • public:
    • Line(int x2=0,int y2=0):Point(x1,y1)
    • {
    • }
    • ~Line()
    • {
    • }
    • };
    • #include<iostream>
    • usingnamespace std;
    • #include"circle.h"
    • #include"graphics.h"
    • #include"Point.h"
    • #include"Line.h"
    • void main ()
    • {
    • int maxx ;
    • int maxy ;
    • int x2 ;
    • int y2 ;
    • Line l ;
    • initwindow(450,300);
    • maxx = getmaxx();
    • maxy = getmaxy();
    • while(!ismouseclick(WM_LBUTTONDOWN))
    • {
    • Line(x2,y2);
    • }
    • getmouseclick(WM_LBUTTONDOWN,x2,y2);
    • system("pause");
    • }


    ok the exact problem is in the " while loop " . because i have to draw a line and this function is implemented in the graphics.h file so should i call the constructor of class line in the while loop or the Line function of the graphics.h file ?? Remember i have to implement this assignment using classes .







  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    class Line:public Point
    Ask yourself a question: is line a point?

    Your approach of treating line as a point is out of any logic. Line consists of two points, therefore, it should contain two Point member variables.

    Quote Originally Posted by waqas94 View Post
    should i call the constructor of class line in the while loop or the Line function of the graphics.h file ?? Remember i have to implement this assignment using classes .
    What you have to do is to call Line() from graphics.h. You can do this using your Line class (which should then be placed in a separate namespace), but you have to construct an instance first. Constructing Line is not enough, because the constructor of line does not call Line() from graphics.h.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by kmdv View Post
    Code:
    class Line:public Point
    Ask yourself a question: is line a point?

    Your approach of treating line as a point is out of any logic. Line consists of two points, therefore, it should contain two Point member variables.



    What you have to do is to call Line() from graphics.h. You can do this using your Line class (which should then be placed in a separate namespace), but you have to construct an instance first. Constructing Line is not enough, because the constructor of line does not call Line() from graphics.h.

    Brother can u please tell a bit of syntax of the solution u have told me ? thank you

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Create Line class (which has nothing to do with printing):

    Code:
    namespace MyProgram {
    
    class Line
    {
    public:
    
        Line(const Point& a, const Point& b):
            mA(a),
            mB(b)
        {
        }
        
        const Point& GetA() const
        {
            return mA;
        }
        
        const Point& GetB() const
        {
            return mB;
        }
        
        void SetA(const Point& a)
        {
            mA = a;
        }
        
        void SetB(const Point& b)
        {
            mB = b;
        }
        
    private:
    
        Point mA;
        
        Point mB;
    };
    
    }
    And then call the Line() function from graphics.h. You didn't provide the signature of Line(), so nobody can tell how to call it properly. It will be more or less like this:

    Code:
    Line line(Point(100, 150), Point(200, 250)); // construct line by calling its constructor; pass two points
    
    ...
    
    ::Line(line.GetA().GetX(), line.GetA().GetY(), line.GetB().GetX(), line.GetB().GetY()); // call Line() from graphics.h, passing coordinates of two line's points

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>line.GetA().GetX()
    I'd name them X1, X2, Y1 and Y2

    @OP: Also, please indent your code. It's unreadable as it is!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    I'd name them X1, X2, Y1 and Y2
    I'd implement them as non-member functions so as to keep the interface minimal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. graphics in c++
    By waqas94 in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2013, 02:18 PM
  2. graphics
    By Yarin in forum Windows Programming
    Replies: 5
    Last Post: 03-27-2008, 11:58 AM
  3. Help with Graphics
    By Yartch in forum C++ Programming
    Replies: 5
    Last Post: 06-22-2007, 05:09 PM
  4. Please help with graphics!
    By Unregistered in forum Game Programming
    Replies: 3
    Last Post: 07-23-2002, 09:46 PM
  5. graphics.h
    By Seti in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 04:11 PM