I need a little help on understanding when pointers are involved in classes. Here is an example code.
Code:
// Listing 8.10
// Using pointers with const methods
#include <iostream>
using namespace std;
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;
cout << "pRect width: " << pRect->GetWidth()
<< " feet\n";
cout << "pConstRect width: " << pConstRect->GetWidth()
<< " feet\n";
cout << "pConstPtr width: " << pConstPtr ->GetWidth()
<< " feet\n";
pRect->SetWidth(10);
// pConstRect->SetWidth(10);
pConstPtr->SetWidth(10);
cout << "[Rect width: " << pRect->GetWidth()
<< "feet\n";
cout << "pConstRect width: " << pConstRect->GetWidth()
<< " feet\n";
cout << "pConstPtr width: " << pConstPtr->GetWidth()
<< " feet\n";
return 0;
}
I am wondering about the lines:
Code:
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;
Rectangle * const pConstPtr = new Rectangle;
Do they mean that basically instead of declaring an object as : and so on, it just declares the object as a pointer? It is a little confusing to me when the difference operator is involved. Also, I am not too clear as to the use of new keyword.