Skip to content

Test Program 4

This program tests the runtime binding of the variables of a class.

Input: If any input > 0

Output

In A F1
0

Input : If any input <= 0

Output :

In B F1
1

This program uses the concepts of inheritance and subtype polymorphism.

class
A
{
decl
int i;
int f0();
int f1();
enddecl
int f0() {
decl
int c;
enddecl
begin
c = self.f1();
write(self.i);
return 1;
end
}
int f1() {
decl
enddecl
begin
self.i=0;
write("In A F1");
return 1;
end
}
}
B extends A
{
decl
int f1();
enddecl
int f1() {
decl
enddecl
begin
self.i=1;
write("In B F1");
return 1;
end
}
}
endclass
decl
int n;
A obj;
enddecl
int main() {
decl
enddecl
begin
n=initialize();
read(n);
if(n>0) then
obj = new(A);
else
obj = new(B);
endif;
n = obj.f0();
return 1;
end
}