Oto kod:
Kod
#include <iostream>
class Point //klasa przechowująca współrzędne x i y
{
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;
}; //koniec klasy przechowujacej wspolrzedne
class Rectangle //klasa tworząca prostokąt
{
public:
Rectangle(int top, int bottom, int left, int right);
~Rectangle(){}
int GetTop() const {return itsTop;}
int GetBottom() const {return itsBottom;}
int GetRigth() const {return itsRight;}
int GetLeft() const {return itsLeft;}
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 SetBottom(int bottom) {itsBottom=bottom;}
void SetLeft(int left) {itsLeft=left;}
void SetRight(int right) {itsRight=right;}
int GetArea() const;
private:
int itsTop, itsBottom, itsLeft, itsRight;
Point itsUpperLeft;
Point itsLowerLeft;
Point itsUpperRight;
Point itsLowerRight;
};
//koniec naglowka
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop=top;
itsBottom=bottom;
itsLeft=left;
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);
}
//obliczanie pola prostokata
int Rectangle::GetArea() const
{
int Width=itsRight-itsLeft;
int Height=itsTop-itsBottom;
return(Width*Height);
}
using namespace std;
int main()
{
//lokalna zmienna typu Rectangle
Rectangle MyRectangle(100, 20, 50, 80);
int Area =MyRectangle.GetArea();
cout << "Pole prostokąta wynosi: " << Area << "n";
system("PAUSE");
return 0;
}
class Point //klasa przechowująca współrzędne x i y
{
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;
}; //koniec klasy przechowujacej wspolrzedne
class Rectangle //klasa tworząca prostokąt
{
public:
Rectangle(int top, int bottom, int left, int right);
~Rectangle(){}
int GetTop() const {return itsTop;}
int GetBottom() const {return itsBottom;}
int GetRigth() const {return itsRight;}
int GetLeft() const {return itsLeft;}
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 SetBottom(int bottom) {itsBottom=bottom;}
void SetLeft(int left) {itsLeft=left;}
void SetRight(int right) {itsRight=right;}
int GetArea() const;
private:
int itsTop, itsBottom, itsLeft, itsRight;
Point itsUpperLeft;
Point itsLowerLeft;
Point itsUpperRight;
Point itsLowerRight;
};
//koniec naglowka
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop=top;
itsBottom=bottom;
itsLeft=left;
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);
}
//obliczanie pola prostokata
int Rectangle::GetArea() const
{
int Width=itsRight-itsLeft;
int Height=itsTop-itsBottom;
return(Width*Height);
}
using namespace std;
int main()
{
//lokalna zmienna typu Rectangle
Rectangle MyRectangle(100, 20, 50, 80);
int Area =MyRectangle.GetArea();
cout << "Pole prostokąta wynosi: " << Area << "n";
system("PAUSE");
return 0;
}
Jednak wyświetla mi błędy:
Kod
86 C:Documents and SettingsMateuszPulpitC++main.cpp
`Rectangle' undeclared (first use this function)
86 linijka:
Rectangle MyRectangle(100, 20, 50, 80);
`Rectangle' undeclared (first use this function)
86 linijka:
Rectangle MyRectangle(100, 20, 50, 80);
Chciałem tu stworzyc zmienną typu Rectangle
i kolejny błąd:
Kod
87 C:Documents and SettingsMateuszPulpitC++main.cpp
`MyRectangle' undeclared (first use this function)
87 linijka:
int Area =MyRectangle.GetArea();
`MyRectangle' undeclared (first use this function)
87 linijka:
int Area =MyRectangle.GetArea();