// using pointers with
// const methods
#include <iostream>
class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) { itsLength = length; }
int GetLength() const { return itsLength; }
void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }
private:
int itsLength;
int itsWidth;
};
Rectangle::Rectangle();
itsWidth(5),
itsLength(10)
{}
Rectangle::~Rectangle()
{}
int main()
{
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;
Rectangle * const pConstPtr = new Rectangle;
std::cout << "pRect width: "
<< pRect->GetWidth() << " feet" << std::endl;
std::cout << "pConstRect width: "
<< pConstRect->GetWidth() << " feet" << std::endl;
std::cout << "pConstPtr width: "
<< pConstPtr->GetWidth() << " feet" << std::endl;
pRect->SetWidth(10);
// pConstRect->SetWidth(10);
pConstPtr->SetWidth(10);
std::cout << "pRect width: "
<< pRect->GetWidth() << " feet" << std::endl;
std::cout << "pConstRect width: "
<< pConstRect->GetWidth() << " feet" << std::endl;
std::cout << "pConstPtr width: "
<< pConstPtr->GetWidth() << " feet" << std::endl;
return 0;
}
This program comes back with three errors.
1) error C2761: '{ctor}' : member function redeclaration not allowed
2) error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
3) error C2448: 'itsLength' : function-style initializer appears to be a function definition
I was also wondering if you should always set a pointer to NULL when you delete it, and waht could happen if you didn't.