Saturday, 24 March 2018

this pointer in C++

this is a keyword that refers to the current instance of the class.
this pointer holds the address of current object.
Uses:
  1. It can be used to pass current object as a parameter to another method.
  2. It can be used to refer current class instance variable.
Example:
#include <iostream> 
using namespace std; 
class Student { 
   public: 
       int age; //data member (also instance variable)     
       string name; //data member(also instance variable) 
       float marks; 
      Student(int age, string name, float marks)   
        {   
             this->age = age;   
            this->name = name;   
            this->marks = marks;  
        }   
       void display()   
        {   
            cout<<"age is:"<<age<<"\n"<<"name is:"<<name<<" \n "<<"marks is:"<<marks<<endl;   
        }   
      }; 
int main(void) { 
 Student  s1 (14, "Suryosnata", 89.45); //creating an object of Student
  s1.display();   
  return  0; 
} 
Output:

age is:14                                                                                                                     
name is:Suryosnata                                                                                                            
marks is:89.45

Note:
Friend functions do not have a this pointer ,because friends are not members of a class.only member function have a this pointer. 

1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...