我有一個任務,我應該繪製一個矩形形狀,方法是在平面中指定兩個點,繪製兩條水平線和兩條垂直線。我們應該使用Rectangle類中的Point類。矩形繪製和打印方法
我有任務的指令.h
(點類),.cpp
(Point類),.h
(矩形類),.cpp
(Rectange類)和主。我沒有做太多的事情,但指定了應該做的事情。他想要一個用y
或y
字符組成的輪廓繪製的矩形。
我覺得一切都在.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
這裏有兩個提示。 1.從編譯器中獲取錯誤消息的第一行並解決該問題。 2.不要輸入全部內容,並希望獲得最佳效果。通過幾條線進行編譯並編譯,當你更有信心時,你可以在併發症之間輸入更多行。 – 2014-09-27 10:17:09