ich diesen Code wurde mit: Der erste ist Rectangle.hppProbleme mit Klassen wie andere Klassen-Mitgliedsdaten in 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();
};
Dies ist 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;
}
Mein Problem ist, dass Wenn es kompiliert wird, gibt es 0 aus, wenn es ausgeben sollte. Wie mache ich es so, dass es ausgibt, was es soll? Warum funktioniert es überhaupt nicht?
Sie haben auch ein anderes Problem: Ihr Code nicht korrekt eingerückt ist, und aus diesem Grund ist ziemlich unlesbar. Es ist doppelt so schwer, Bugs zu beheben, wenn der Code verstümmelt ist. Sie müssen Ihren Code korrigieren und ihn richtig einrücken, um Ihre Chancen zu verbessern, jemanden zu finden, der bereit ist, ihn zu durchsuchen, und Ihr Code-Problem herauszufinden. –
Sicher sollte einer dieser Punkte 2,0 und nicht 0,2 sein? – user657267