In the following code, why
x=new int;
y=new int;
are a must, if I comment out these two then I will have run-time error. I think I have int * x; and int* y; already shows that x and y are pointers point to int. There is no need to redo the following
x=new int;
y=new int;
Thanks
#include "stdafx.h"
#include <iostream>
using namespace std;
class Rectangle
{
int * x;
int * y;
public:
Rectangle (int a, int b);
~Rectangle ();
int area()
{
return *x * *y;
}
};
Rectangle::Rectangle (int a, int b)
{
x=new int;
y=new int;
* x=a;
* y=b;
}
Rectangle::~Rectangle()
{
}
int _tmain(int argc, _TCHAR* argv[])
{
Rectangle rect2(5,6);
cout<<rect2.area()<<endl;
return 0;
}