Mega Code Archive

 
Categories / C++ Tutorial / Class
 

Using the this pointer

#include <iostream>    class Rectangle  {  public:      Rectangle();      ~Rectangle();      void SetLength(int length) { this->itsLength = length; }      int GetLength() const { return this->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 theRect;      std::cout << "theRect is " << theRect.GetLength()                 << " feet long.\n";      std::cout << "theRect is " << theRect.GetWidth()                 << " feet wide.\n";        theRect.SetLength(20);      theRect.SetWidth(10);      std::cout << "theRect is " << theRect.GetLength()                << " feet long.\n";      std::cout << "theRect is " << theRect.GetWidth()                << " feet wide.\n";        return 0;  } theRect is 10 feet long. theRect is 5 feet wide. theRect is 20 feet long. theRect is 10 feet wide.