CPP Program For this Pointer - WAY2IT

Latest Post :

Home Top Ad

CPP Program For this Pointer




Program 1.




#include<iostream.h>

#include<conio.h>



class trythis

{

int n;

public:

void getdata(int x)

{

n=x;

}

void putdata()

{

cout<<"n at class level"<<n<<endl;

}

trythis()

{

}

trythis(int n)

{

//this->n=n;

n=n;

cout<<"n with THIS "<<this->n<<endl;

}

}  ;

void main()

{

clrscr();

trythis t1,t2(80);

t1.getdata(10);

t1.putdata();

getch();

}


●▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬


Program 2.



#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>



 class person
{
  char name[20];

  int age;

  public:

   person(char *s,int a)
{
    strcpy(name,s);
    age = a;
}
   person &person:: greater (person &x)
{
    if(x.age>=age)
    return x;
    else
    return *this;
}

  void display(void)
{
   cout<<"Name: "<<name<< "\n";
   cout<<"Age :  "<< age<<"\n";
}
};
 void main()
{
  clrscr();
  person P1("John",37);
  person P2("Aliya",29);
  person P3("Herbert",40);

 person P = P1.greater(P3);
 cout<<"Elder person is : \n";
 P.display();

 P = P1.greater(P2);
 cout<<"Elder person is : \n";
 P.display();
 getch();
}