| | |
Drawing Program
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Here's a shorter version of the code, without the Line.
Point.h
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
Point.cpp
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
PointCollection.h
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
PointCollection.cpp
Page.h
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
Page.cpp
Main.cpp
This has nothing to do with Graphics.
I only want p1.draw() to be printed on the location ( 5, 5 ) ( x, y ) in the page.
Point.h
C++ Syntax (Toggle Plain Text)
#pragma once #include <iostream> using namespace std; class Point { private: int x; int y; public: Point(void); Point( int x, int y ); Point( const Point &xPoint ); ~Point(void); void setX( int x ); int getX() const; void setY( int y ); int getY() const; Point & operator = ( const Point &xPoint ); bool operator == ( const Point &xPoint ) const; bool includes( int cord_X, int cord_Y ) const; bool includes( const Point &xPoint ) const; void draw() const; };
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
Point.cpp
C++ Syntax (Toggle Plain Text)
#include "Point.h" Point::Point( void ) { this->x = 0; this->y = 0; } Point::Point( int x, int y ) { this->x = x; this->y = y; } Point::Point( const Point &xPoint ) { this->x = xPoint.x; this->y = xPoint.y; } Point::~Point( void ) {} ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// void Point::setX( int x ) // set X { this->x = x; } void Point::setY( int y ) // set Y { this->y = y; } int Point::getX() const // get X { return ( this->x ); } int Point::getY() const // get Y { return ( this->y ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// Point &Point::operator = ( const Point &xPoint ) { if( this != &xPoint ) { this->x = xPoint.x; this->y = xPoint.y; } return ( *this ); } bool Point::includes( int cord_X, int cord_Y ) const { return ( this->x == cord_X && this->y == cord_Y ); } void Point::draw() const { cout << '.'; } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool Point::operator == ( const Point &xPoint ) const { bool isEquality = false; if( this->x == xPoint.x && this->y == xPoint.y ) { isEquality = true; } return isEquality; }
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
PointCollection.h
C++ Syntax (Toggle Plain Text)
#pragma once #include "Point.h" const int MAXPoint = 100; class PointCollection { private: Point pts[ MAXPoint ]; int quantity; public: PointCollection( void ); PointCollection( const PointCollection &xPointCollection ); ~PointCollection( void ); // To know how many Points are // ..in the collection int size() const; PointCollection operator = ( const PointCollection &xPointCollection ); PointCollection &operator + ( const Point &xPoint ); Point &operator[]( int index ); const Point &operator[]( int index ) const; bool isFull() const; bool isEmpty() const; bool includes( const Point &xPoint ) const; int indexOf( const Point &xPoint ) const; };
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
PointCollection.cpp
C++ Syntax (Toggle Plain Text)
#include "PointCollection.h" PointCollection::PointCollection( void ) { this->quantity = 0; } PointCollection::PointCollection( const PointCollection &xPointCollection ) { this->quantity = xPointCollection.quantity; for ( int i = 0; i < this->quantity; i++ ) ( this->pts )[ i ] = ( xPointCollection.pts )[ i ]; } PointCollection::~PointCollection( void ) {} ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// int PointCollection::size() const { return ( this->quantity ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// Point &PointCollection::operator []( int index ) { return ( ( this->pts )[ index ] ); } const Point &PointCollection::operator []( int index ) const { return ( ( this->pts )[ index ] ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// PointCollection PointCollection::operator =( const PointCollection &xPointCollection ) { this->quantity = xPointCollection.quantity; for ( int i = 0; i < this->quantity; i++ ) ( this->pts )[ i ] = ( xPointCollection.pts )[ i ]; return ( *this ); } PointCollection &PointCollection::operator +( const Point &xPoint ) { (*this)[ this->quantity ] = xPoint; (this->quantity)++; return ( *this ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool PointCollection::isFull() const { return ( this->quantity == MAXPoint ); } bool PointCollection::isEmpty() const { return ( this->quantity == 0 ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool PointCollection::includes( const Point &xPoint ) const { bool thisOne = false; for ( int i = 0; i < this->quantity && ! thisOne; i++ ) { if( pts[ i ] == xPoint ) thisOne = true; } return ( thisOne ); } int PointCollection::indexOf( const Point &xPoint ) const { int index = -1; for ( int i = 0; i < this->quantity && index == -1; i++ ) { if( pts[ i ] == xPoint ) index = i; } return ( index ); }
Page.h
C++ Syntax (Toggle Plain Text)
#pragma once #include "PointCollection.h" const int MAXPAGE = 100; class Page { private: int heigh; int width; public: Page( void ); Page( int width, int heigh ); Page( const Page &xPage ); ~Page( void ); void setWidth( int width ); int getWidth() const; void setHeigh( int heigh ); int getHeigh() const; bool operator == ( const Page &xPage ) const; void draw() const; };
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
Page.cpp
C++ Syntax (Toggle Plain Text)
#include "Page.h" Page::Page( void ) { this->width = 0; this->heigh = 0; } Page::Page( int width, int heigh ) { this->width = width; this->heigh = heigh; } Page::Page( const Page &xPage ) { this->width = xPage.width; this->heigh = xPage.heigh; } Page::~Page( void ) {} ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// void Page::setHeigh( int heigh ) { this->heigh = heigh; } void Page::setWidth( int width ) { this->width = width; } int Page::getHeigh() const { return ( this->heigh ); } int Page::getWidth() const { return ( this->width ); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// void Page::draw() const { bool included; PointCollection pc; for( int y = 0; y < this->heigh; y++ ) { included = false; for ( int x = 0; x < this->width; x++ ) { for ( int p = 0; p < pc.size() && !included; p++ ) { if ( pc[ p ].includes( x, y ) ) { pc[ p ].draw(); included = true; } if ( included ) cout << ' '; } } cout << endl; } } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// bool Page::operator ==( const Page &xPage ) const { bool isEquality = false; if( this->heigh == xPage.heigh && this->width == xPage.width ) { isEquality = true; } return isEquality; }
Main.cpp
C++ Syntax (Toggle Plain Text)
#include "Page.h" int main () { Point p1( 5, 5 ), p2( 1, 1 ); Page pg( 15, 15 ); p1.draw(); pg.draw(); system("PAUSE"); return 0; }
This has nothing to do with Graphics.
I only want p1.draw() to be printed on the location ( 5, 5 ) ( x, y ) in the page.
You are not moving the cursor at right place before you do a "cout"
Ycan move cursor by including one more function in your point class
in point.h
add a member funtion in the point class
in point .cpp add following.
use it in your draw member function
Ycan move cursor by including one more function in your point class
in point.h
add a member funtion in the point class
C++ Syntax (Toggle Plain Text)
void gotoxy(int , int ) const ;
C++ Syntax (Toggle Plain Text)
void Point::gotoxy(int x, int y) const { HANDLE hConsoleOutput; COORD Cursor_Pos = {x, y}; hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos); }
use it in your draw member function
C++ Syntax (Toggle Plain Text)
void Point::draw() const { gotoxy(x,y); cout<<"."; }
Last edited by dubeyprateek; Dec 24th, 2007 at 8:51 am.
I know I am. Therefore I am.
![]() |
Similar Threads
- Help, Help with Traffic Simulation program (Java)
- Simple while loop in c++ (C++)
- Amazing Website Designs - How do they do this? (Site Layout and Usability)
- Gliven, I need an idea for a program... help anyone? (Pascal and Delphi)
- Mathematical formulas used in C++ ? (C++)
- I need a program (C++)
- Building an array for dice program (C#)
- Word 2002 hogs CPU with Visio drawing embedded. (Windows NT / 2000 / XP)
- How do you add animation to visual C++ program? (C++)
Other Threads in the C++ Forum
- Previous Thread: compilnig using Dev C++ (Bloodshed)
- Next Thread: Urgent: Plz Help me,Prime Number Program Problem
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





