0
// 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.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by StuXYZ
0

In addition, replace the semi colon on line 21 with a colon ( : ).

Swap line 22 with line 23. That will avoid a compiler warning.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.