level 2
天空飞鱼84
楼主
问一个麻烦点的问题 我把类的声放在一个。HPP的文件里,把类的定义放在一个。CPP的文件里 编译时,对书把能改的错误都改了,可是还是报错, 通不驼编译。是为什么? 作者: 天空飞鱼84 2007-3-7 07:47 回复此发言 -------------------------------------------------------------------------------- 2 回复:问一个麻烦点的问题 附程序,麻烦你看看了 这是类的声明 #include
class Point { public: void SetX(int x) {itsX = x; } void SetY(int y) {itsY = y; } int GetX()const (return itsX;} int GetY()const {return itsY;} private: int itsX; int itsY; }; class Rectangle { public: Rectangle (int top , int left , int bottom , int right); ~Rectangle () {} int GetTop() const {return itsTop; } int GetLeft() const {return itsLeft; } int GetBottom() const {return itsBottom; } int GetRight() const {return itsRight; } Point GetUpperLeft() const {return itsUpperLeft; } Point GetLowerLeft() const {return itsLowerLeft; } Point GetUpperRight() const {return itsUpperRight; } Point GetLowerRight() const {return itsLowerRight; } void SetUpperLeft(Point Location) {itsUpperLeft = Location;} void SetLowerLeft(Point Location) {itsLowerLeft = location;} void SetUpperRight(Point Location) {itsUpperRight = location;} void SetLowerRight(Point location) {itsLowerRight = location;} void SetTop(int top) { itsTop = top; } void SetLeft(int left) { itsLeft = left; } void SetBottom(int bottom) { istBottom = bottom;} void SetRight(int right) { itsRight = right; } int GetArea() const; private: Point itsUpperLeft; Point itsUpperRight; Point itslowerLeft; Point itslowerRight; int itsTop; int itsLeft; int itsBottom; int itsRight; }; 作者: 天空飞鱼84 2007-3-7 07:48 回复此发言 -------------------------------------------------------------------------------- 3 回复:问一个麻烦点的问题 类的定义 #include "Rect.hpp" Rectangle::Rectangle(int top, int left,int bottom,int right) { itsTop = top; itsLeft = left; itsBottom = bottom; itsRight = right; itsUpperLeft.SetX(left); itsUpperLeft.SetY(top); itsUpperRight.SetX(right); itsUpperRight.SetY(top); itsLowerLeft.SetX(left); itsLowerLeft.SetY(bottom); itsLowerRight.SetX(right); itsLowerRight.SetY(bottom); } int Recttangle::GetArea() const { int Width = itsRight - itsLeft; int Height = itsTop - itsBottom; return (Width * Height); } int main() { Rectangle MyRectangle (100, 20 ,50 , 80); int Area = MyRectangle.GetArea(); std::cout << "Area: " << Area << "\n"; std::cout << "Upper Left X Coordinate: "; std::cout << MyRectangle.GetUpperLeft().GetX(); return 0; }
2007年03月06日 23点03分
1
class Point { public: void SetX(int x) {itsX = x; } void SetY(int y) {itsY = y; } int GetX()const (return itsX;} int GetY()const {return itsY;} private: int itsX; int itsY; }; class Rectangle { public: Rectangle (int top , int left , int bottom , int right); ~Rectangle () {} int GetTop() const {return itsTop; } int GetLeft() const {return itsLeft; } int GetBottom() const {return itsBottom; } int GetRight() const {return itsRight; } Point GetUpperLeft() const {return itsUpperLeft; } Point GetLowerLeft() const {return itsLowerLeft; } Point GetUpperRight() const {return itsUpperRight; } Point GetLowerRight() const {return itsLowerRight; } void SetUpperLeft(Point Location) {itsUpperLeft = Location;} void SetLowerLeft(Point Location) {itsLowerLeft = location;} void SetUpperRight(Point Location) {itsUpperRight = location;} void SetLowerRight(Point location) {itsLowerRight = location;} void SetTop(int top) { itsTop = top; } void SetLeft(int left) { itsLeft = left; } void SetBottom(int bottom) { istBottom = bottom;} void SetRight(int right) { itsRight = right; } int GetArea() const; private: Point itsUpperLeft; Point itsUpperRight; Point itslowerLeft; Point itslowerRight; int itsTop; int itsLeft; int itsBottom; int itsRight; }; 作者: 天空飞鱼84 2007-3-7 07:48 回复此发言 -------------------------------------------------------------------------------- 3 回复:问一个麻烦点的问题 类的定义 #include "Rect.hpp" Rectangle::Rectangle(int top, int left,int bottom,int right) { itsTop = top; itsLeft = left; itsBottom = bottom; itsRight = right; itsUpperLeft.SetX(left); itsUpperLeft.SetY(top); itsUpperRight.SetX(right); itsUpperRight.SetY(top); itsLowerLeft.SetX(left); itsLowerLeft.SetY(bottom); itsLowerRight.SetX(right); itsLowerRight.SetY(bottom); } int Recttangle::GetArea() const { int Width = itsRight - itsLeft; int Height = itsTop - itsBottom; return (Width * Height); } int main() { Rectangle MyRectangle (100, 20 ,50 , 80); int Area = MyRectangle.GetArea(); std::cout << "Area: " << Area << "\n"; std::cout << "Upper Left X Coordinate: "; std::cout << MyRectangle.GetUpperLeft().GetX(); return 0; }