Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
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\n";
std::cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
std::cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n";
pRect->SetWidth(10);
pConstPtr->SetWidth(10);
std::cout << "pRect width: " << pRect->GetWidth() << " feet\n";
std::cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
std::cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n";
return 0;
}
pRect width: 5 feet
pConstRect width: 5 feet
pConstPtr width: 5 feet
pRect width: 10 feet
pConstRect width: 5 feet
pConstPtr width: 10 feet