OOPc using C++

With this blog I want to share my knowledge of OOPs and C++. I Hope this can quench your thirst for learning.Any suggestions and comments are welcome.
Showing posts with label This Pointer. Show all posts
Showing posts with label This Pointer. Show all posts

Monday, August 23, 2010

Program 13 - C++ program to illustrate the usage of this pointer

/*C++ program to illustrate the usage of this pointer*/
#include<iostream.h>
#include<conio.h>
#include<string.h>

class person
{
       char name[20];
       float age;
public:
      person(char *s,float a)
     {
             strcpy(name,s);
             age=a;
       }
     person &person::greater(person &x)
    {
           if(x.age>=age)
                 return x;
          else
                 return *this;
     }
   void display()
   {
          cout<<"Name: "<<name<<"\nAge: "<<age<<endl;
     }
};
void main()
{
     clrscr();
     person p1("John",37.50),p2("Ahmed",29.0),p3("Heffer",40.25);
     person p=p1.greater(p3);
     cout<<"Elder person is:\n";
     p.display();
     p=p1.greater(p2);
     cout<<"Elder person is\n";
     p.display();
   
    getch();
}

OUTPUT:
Elder person is:
Name: Heffer
Age: 40.25
Elder person is
Name: John
Age: 37.5