矩形绘制和打印方法

时间:2014-09-27 06:46:47

标签: c++

我有一个任务,我应该绘制一个矩形形状,方法是在一个平面中指定两个点,绘制两条水平线和两条垂直线。我们应该在Rectangle类中使用Point类。

我有作业指示.h(点类),.cpp(点类),.h(矩形类),.cpp(Rectange类)和主要指令。我没有在主要做太多,但指定了应该做什么。他希望绘制一个矩形,其轮廓由y或字符y组成。

我认为.h(点类),.cpp(点类),.h(矩形类)中的一切都很好,但是我遇到了draw方法和Rectangle .cpp的print方法,教师说只是在draw方法中使用temp变量作为原点,或者类似的东西,

同样不确定.cpp矩形文件中的print方法,请点击此处获得帮助。试图编译,但所有地狱都破裂了,任何解释/例子都会有很多帮助。

Point.h

//Point.h
#include <iostream.h>
/* The Point class Header file (Point.h) */
#ifndef POINT_H
#define POINT_H

class Point {

private:
    double x,y;//x and y are private variables

public:    
    Point(int x, int y):x(x),y(y){}//use initialization list    
    double getX() const; //Getters    
    double getY() const;    
    void setX(double x);  //Setters    
    void setY(double y);    //Setters

    void print()const;
    //Overload '+' operator
    const Point operator +(const Point & rt)const;
    //Overload '-' operator
    const Point operator - (const Point &rt)const;

    Point operator +=(Point & rt);
    Point operator -=(Point & rt);
    //Overload '==' operator comparing two points

    int operator ==(Point &rt);
    int operator <(Point &rt);
    int operator >(Point &rt);
    int operator <=(Point &rt);
    int operator >=(Point &rt); 

};
/* POINT_H */
#endif

点.cpp

//the Point.cpp file
#include "Point.h"
#include<iostream>    
using namespace std;    
//Getters
double Point::getX()const {return x;}
double Point::getY()const {return y;}

//setters    
void Point::setX(double x) {this->x=x;}
void Point::setY(double y) {this->y=y;}

//Public functions
void Point::print()const{
    cout << "(" << x << "," << y << ")" << endl;
}

//overloading '+' operator
const Point Point::operator+(const Point & rt) const{
return Point(x + rt.x, y + rt.y);
}

const Point Point::operator-(const Point & rt) const{
return Point(x - rt.x, y - rt.y);
}

Point Point::operator+=(Point & rt){
return Point(x+=rt.x, y+=rt.y);
}

int Point::operator ==(Point & rt){
return (x == rt.x && y==rt.y);
}

int Point::operator <(Point & rt){
return (x < rt.x && y<rt.y);
}

int Point::operator >(Point & rt){
return (x > rt.x && y>rt.y);
}

int Point::operator <=(Point & rt){
return (x <= rt.x && y<=rt.y);
}

int Point::operator >=(Point & rt){
return (x >= rt.x && y>=rt.y);
}
//;   
//;    
//END POINT.CPP

矩形文件

#ifndef RECTANGLE_H    
#define RECTANGLE_H
#include <iostream.h>   
#include "Point.h"    
class Rectangle {

private:    
    Point origin;
    Point corner;

public:
    Rectangle (const Point & or, const Point & cr):origin(or),corner(cr) {}
    // void move(int dx, int dy);    
    void draw();
    void print()const;
};      
#endif  /* RECTANGLE_H */

矩形.cpp文件

/* The Rectangle.cpp file) */
#include "Point.h"
#include "Rectangle.h"
#include <iostream.h>
#include <string>
#include <conio.h>
using namespace std;    
// Public Functions
 void Rectangle::print() const
{
     cout<<"(" <<origin <<"," <<corner << ")" <<endl;
}  

void Rectangle::draw()    
 {    
      Point temp=origin;      //store origin in temp object
      while (temp.getX() < corner.getX())  {
      putch('y');    
     }
     /* int temp=origin;    
       for (int x = temp.getX(); x < center.getX(); x++) {
        Point pt1 (x, temp.getY());
        Point pt2 (x, center.getY());
       pt1(6,4);
      //    move to p1 // not sure how to do this
        putCH ('y');
       pt2(30,15);
       //   move to p2  //not sure how to do this
        putCH ('y');
  }

   for (int y = lowerRight.getY(); y < upperLeft.getY(); y++) {
          Point pt1 (origin.getX(),y);
          Point pt2 (corner.getX(),y);
           pt1(6,4);
          //move to p1  //not sure how to do this
          putCH('y');
          pt2(30,15);
         // move to p2  //not sure how to do this
          putCH ('y');
   }*/   
//return 0;   
}//;

MAIN

 #include "Point.h"
 #include "Rectangle.h"
 #include <iostream>
 #include <conio.h>
 using namespace std;

 int _tmain(int argc, _TCHAR* argv[])
 {
      //char y;        
      Point p1(6, 4), p2(30, 15);

      //cout<<"\n the origin of Rectangle is at:  ";
      //p1.print();
      //cout<<"\n the opposite corner of rect is at:";
      //p2.print();

      Rectangle r1(p1,p2);        
      r1.draw();        
      clrscr();        
      gotoxy(1,20);        
      //r1.print();        
      getch();
      return 0;       
    } //;
    //END OF MAIN

2 个答案:

答案 0 :(得分:1)

我可以看到一个特定于C ++的问题:你没有在Point上重载"<<"运算符。首先请这样做。

其次,您使用的是哪个编译器?

clrscr(); gotoxy();

不是标准C +++的一部分 如果你的编译器支持它们,那就好了。 否则,你需要寻找其他选择。

好习惯:避免

  

使用namespace std;

相反,写一下

   std::cout 
   std::endl;

我没有通过你的实际逻辑。    这实际上是你的任务 - 不是吗?

答案 1 :(得分:0)

感谢您的建议,我将调查一下,但我也特别遇到了Rectangle .cpp方法draw()和print()的问题。我是C ++的新手,我有Rectangle .cpp文件的draw()和print()方法的代码,但我的逻辑错了。

我知道我需要在Rectangle.cpp文件的Rectangle draw方法中声明一个临时变量,我需要一个循环来增加临时而不是永久原点。我开始为draw()方法编写一些代码,但我不确定我是否在正确的轨道上。

另外我在Rectangle .cpp中的print()方法给了我一些问题,我把东西放在那里,不确定它是对的......

顺便说一句Point.h,Point .cpp很好,我只是在Rectangle .cpp中使用了一点Point类。我不关心Point.h文件中的运算符方法或Point.cpp文件(例如int Point :: operator ==(Point&amp; rt)等)。我不认为我需要但是因为过去需要它们所以把它们放下来。