C++ this pointer

In C++ programming language the this pointer is used to refer to the current object. The this pointer is not use to access static members of the class because the static members do not belong to a specific object.

#include <iostream>
#include <conio.h>
#include<cmath>
using namespace std;
class Triangle{
    //data members
    private:
         double a,b;
    //constructors
    public:    
    Triangle(){}
    Triangle(double a,double b){this->a=a;this->b=b;}
    //this->a refers to class data member a
    //a refers to a argument
    //this->b refers to class data member b
    //b refers to b argument

    //method members
    public:
         double getPerimeter();
         double getPerimeter(double a,double b);
         double getArea();
         double getArea(double a,double b);
        
    };
double Triangle::getPerimeter(double a,double b){
       double c=sqrt(pow(a,2)+pow(b,2));
       return(a+b+c);
     }
double Triangle::getArea(double a,double b){
       return(a*b*0.5);
     }
double Triangle::getPerimeter(){
       double c=sqrt(pow(a,2)+pow(b,2));
       return(a+b+c);
     }
double Triangle::getArea(){
       return(a*b*0.5);
     }
    
int main(){
   Triangle tr(10,20);
   cout<<"Perimeter="<<tr.getPerimeter()<<endl;
   cout<<"Area="<<tr.getArea()<<endl;
   Triangle tr1;
   cout<<"Perimeter="<<tr1.getPerimeter(5.0,10.0)<<endl;
   cout<<"Area="<<tr1.getArea(5.0,10.0)<<endl;
   getch();
   return 0;
}

Posted by: kello bashy | post date: 01-26-2013 | Subject: C++ Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.