2014-09-27 68 views
-1

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

我有任务的指令.h(点类),.cpp(Point类),.h(矩形类),.cpp(Rectange类)和主。我没有做太多的事情,但指定了应该做的事情。他想要一个用yy字符组成的轮廓绘制的矩形。

我觉得一切都在.h(点类)好,.cpp(Point类),.h(矩形类),但我有矩形.cpp的绘制方法和印刷方法的问题,教师可以说只是使用一个临时变量的绘制方法或类似的东西,

也不知道在.cpp矩形文件的打印方法,将不胜感激这里的帮助。试图编译,但所有地狱都打破了,任何解释/例子都会有所帮助。

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 

矩形.h文件

#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; 
}//; 

主要

#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 
+0

这里有两个提示。 1.从编译器中获取错误消息的第一行并解决该问题。 2.不要输入全部内容,并希望获得最佳效果。通过几条线进行编译并编译,当你更有信心时,你可以在并发症之间输入更多行。 – 2014-09-27 10:17:09

回答

1

我能看到一个C++具体的问题:你没有重载"<<"操作上点。首先请做那个。

其次,你正在使用哪种编译器?

clrscr(); gotoxy(); 

不是标准C+++的一部分。
如果你的编译器支持它们,那就很好。 否则,您需要寻找替代品。

好习惯:使用命名空间std避免

;

相反,写

std::cout 
    std::endl; 

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

0

感谢您的建议,我会研究这一点,但我也特别有问题的矩形.cpp方法draw()和print()。我是C++新手,我为Rectangle .cpp文件提供了draw()和print()方法的代码,但是我的逻辑错误。

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

也是我的print()在矩形的.cpp方法是给我的问题,我把东西在那里,不知道这是正确的...

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