This Pointer points to individual object in which the function is running. - C++ Function

C++ examples for Function:Function Pointer

Description

This Pointer points to individual object in which the function is running.

Demo Code

                                          
#include <iostream> 
                                           
class Rectangle /*from   ww w .  j  av  a2s. c o  m*/
{ 
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." << std::endl; 
    std::cout << "theRect is " << theRect.GetWidth()  << " feet wide." << std::endl; 
                                           
    theRect.SetLength(20); 
    theRect.SetWidth(10); 
    std::cout << "theRect is " << theRect.GetLength() << " feet long." << std::endl; 
    std::cout << "theRect is " << theRect.GetWidth() << " feet wide." << std::endl; 
                                             
    return 0; 
}

Result


Related Tutorials