1234567891011121314151617181920212223242526272829
#include <iostream> using namespace std; class CRectangle { int *width, *height; public: CRectangle (int,int); ~CRectangle (); int area () {return (*width * *height);} }; CRectangle::CRectangle (int a, int b) { width = new int;//this height = new int;//this *width = a; *height = b; } CRectangle::~CRectangle () { delete width; delete height; } int main () { CRectangle rect (3,4), rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }
1234
width = new int;//the size of width in here is 4 cos it is an integer or bcoz it is a pointer? //if we assign like below width = new int[5];//the size of width is 5 right? I'm not sure about the correction of the code