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

Recommended Answers

All 2 Replies

line 21: remove the semicolon at the end

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.