#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();
}
我将矩形区域设为“6”,而不是“2”。有人可以指出错误。