Wednesday, March 28, 2018

Write a C++ program to illustrate this pointer.


AIM: Write a  C++ program to illustrate this pointer.
THEORY:
The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
For a class X, the type of this pointer is ‘X* const’. Also, if a member function of X is declared as const, then the type of this pointer is ‘const X *const’
SOURCE CODE:
#include<iostream>
using namespace std;
class name{
            char str[15];
            int age;
            public:
                        void input(){
                                    cout<<"\n enter name and age:";
                                    cin>>str;
                                    cin>>age;
                        }
                        void show(){
                                    cout<<str<<age;
                        }
                        name display(name x) {
                                    cout<<this->str<<endl;
                                    cout<<this->age<<endl;
                                    cout<<x.str<<endl;
                                    cout<<x.age<<endl;
                                    if(this->age>x.age){
                                                return *this;
                                    }
                                    else{
                                                return x;
                                    }
                        }
};
int main(){
            name n1,n2,n;
            n1.input();
            n2.input();
            n=n1.display(n2);
            n.show();
            return 0;
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home