Practical-24: Write A Program Using This POINTER
Practical-24: Write A Program Using This POINTER
#include<string.h>
class person
char name[20];
float age;
public:
person(char *s,float a)
strcpy(name,s);
age=a;
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);
P.display();
P=P1.greater(P2);
return 0;
}
OUTPUT
Elder person is:
Name:Hebber
Age:40.25
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;
bptr->show();
DC derived;
bptr=&derived;
bptr->b=200;
bptr->show();
DC*dptr;
dptr=&derived;
dptr->d=300;
dptr->show();
cout<<”using((DC*)bptr)\n”;
((DC*)bptr)->d=400;
((DC*)bptr)->show();
return 0;
}
OUTPUT
bptr points base object
b=100
b=200
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;
bptr=&B;
bptr->display();
bptr=&D;
bptr->display();
bptr->show();
}
OUTPUT
Bptr points to Base
Display base
Show base
Display base
Show derived
Part2=35.5
Sports wt:7
Total Score:63