0% found this document useful (0 votes)
25 views

Practical-24: Write A Program Using This POINTER

The document contains code for 3 programs - the first uses pointers to compare the ages of person objects and return the older one, the second demonstrates using pointers to base and derived classes, and the third implements virtual functions to override a display method in a derived class. The code is presented along with the output from running each program.

Uploaded by

harleen kaur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Practical-24: Write A Program Using This POINTER

The document contains code for 3 programs - the first uses pointers to compare the ages of person objects and return the older one, the second demonstrates using pointers to base and derived classes, and the third implements virtual functions to override a display method in a derived class. The code is presented along with the output from running each program.

Uploaded by

harleen kaur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PRACTICAL-24

Write a program using this POINTER.


#include<iostream.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(void)

cout<<”Name:”<<name<<”\n”<<”Age:”<<age<<”\n”;
}

};

int main()

person P1(“John”,37.50),P(“Ahmed”,29.0),P3(“Heber”,40.25);

person P=P1.greater(P3);

cout<<”Elder person is:\n”;

P.display();

P=P1.greater(P2);

cout<<”Elder person is:\n”;

return 0;

}
OUTPUT
Elder person is:

Name:Hebber

Age:40.25

Elder person is:

Name:John

Age:37.5
PRACTICAL-22
Write a program using pointers to derived objects.
#include<iostream.h>

public:

int b;

void show()

Cout<<”b=”<<b<<”\n”;

};

Class DC:public BC

public:

int d;

void show()

cout<<”b=”<<b<<”\n”<<”d=”<<d<<”\n”;

};

int main()

BC*bptr;
BC base;

bptr=&base;

bptr->b=100;

cout<<”bptr points to base object\n”;

bptr->show();

DC derived;

bptr=&derived;

bptr->b=200;

cout<<”bptr now points to derived object\n”;

bptr->show();

DC*dptr;

dptr=&derived;

dptr->d=300;

cout<<”dptr is derived type pointer\n”;

dptr->show();

cout<<”using((DC*)bptr)\n”;

((DC*)bptr)->d=400;

((DC*)bptr)->show();

return 0;

}
OUTPUT
bptr points base object

b=100

bptr now points to derived object

b=200

dptr is derived type pointer

b=200

d=300

using((DC*)bptr)

b=200

d=400
PRACTICAL-23
Write a program using virtual functions.
Class Base

public:

void display()

cout<<”\nDisplay base”;
cout<<”\nshow derived”;

};

int main()

Base B;

Derived D;

Base *bptr;

cout<<”\nbptr points to Base\n”;

bptr=&B;

bptr->display();

cout<<”\n\nbptr points to derived\n”;

bptr=&D;
bptr->display();
bptr->show();

}
OUTPUT
Bptr points to Base

Display base

Show base

Bptr points to Derived

Display base

Show derived
Part2=35.5

Sports wt:7

Total Score:63

You might also like