#include <iostream>
#include <conio.h>
using namespace std;
class Crectangle {
int * height, * width;
public: Crectangle();
Crectangle(int, int);
Crectangle();
int area() {
return (*height * *width);
}
};
Crectangle::Crectangle() {
*height = 3;
*width = 5;
}
Crectangle::Crectangle(int a, int b) {
height = new int;
width = new int;
*height = a;
*width = b;
}
Crectangle::~Crectangle() {
delete height;
delete width;
}
int main() {
Crectangle rect(1, 2);
Crectangle rectb;
cout << "rect = " << rect.area() << "\n";
cout << "rectb = " << rectb.area();
getch();
}
"2"대신 "6"이 될 영역을 얻고 있습니다. 누군가 실수를 지적 할 수 있습니까? 여기생성자 오버로드. 잘못된 솔루션 얻기
왜 높이 및 너비 포인터가 있습니까? 기본 생성자는 높이 또는 너비에 대한 공간을 할당하지 않으므로 잘못되었습니다. int를 직접 사용하고 기본값을 원한다면'Crectangle :: Crectangle (int a = 3, int b = 5);를 사용하십시오. – Corbin