我使用此代碼:首先是Rectangle.hpp問題與使用類在C++其他類的數據成員
#include <iostream>
//Point
class point {
public:
void setxy(int nx, int ny);
const int getx();
const int gety();
private:
int x;
int y;
};
void point::setxy(int nx, int ny) {
x = nx;
y = ny;
};
const int point::getx() { return x; };
const int point::gety() { return y; };
//Rectangle
class rectangle {
public:
rectangle(point nLl, point nLr, point nUl, point nUr);
void getArea();
const point getLl() { return Lleft; };
const point getLr() { return Lright; };
const point getUl() { return Uleft; };
const point getUr() { return Uright; };
const int getRight() { return Right; };
const int getLeft() { return Left; };
const int getTop() { return Top; };
const int getBottom() { return Bottom; };
private:
point Lleft;
point Lright;
point Uleft;
point Uright;
int Right;
int Left;
int Top;
int Bottom;
};
void rectangle::getArea() {
int width = Right - Left;
int height = Top - Bottom;
std::cout << "The area is " << width * height << ".\n";
};
rectangle::rectangle (point nLl, point nLr, point nUl, point nUr)
{
Lleft = nLl;
Lright = nLr;
Uleft = nUl;
Uright = nUr;
Right = Lright.getx();
Left = Lleft.getx();
Top = Uleft.gety();
Bottom = Lleft.gety();
};
這是Rectangle.cpp:
#include <iostream>
#include "rectangle.hpp"
int main() {
point nnUleft;
nnUleft.setxy(0,2);
point nnUright;
nnUright.setxy(2,2);
point nnLright;
nnLright.setxy(0, 0);
point nnLleft;
nnLleft.setxy(0, 2);
rectangle qd(nnLleft, nnLright, nnUleft, nnUright);
qd.getArea();
char bin;
std::cin >> bin;
std::cout << bin;
}
我的問題是, ,當編譯時,它輸出0,當它應該輸出4.我怎樣才能輸出它應該輸出的內容?爲什麼它不在首位工作?
你也有另外一個問題:你的代碼是不正確的縮進,正因爲如此,幾乎是不可讀。當代碼亂碼時,修復錯誤是兩倍。您必須修正您的代碼並正確縮進它,以提高找到願意挖掘它的人的機會,並找出您的代碼問題。 –
當然,其中一個點應該是2,0而不是0.2? – user657267