◄Example► ◄Contents► ◄Index► ◄Back► PROGRAM objectdemo; { OBJECTDE.PAS : Demonstrates object techniques with geometric shapes } {M+} TYPE geo_shape = OBJECT area : Real; height : Real; what : STRING; PROCEDURE geo_shape.init; PROCEDURE geo_shape.say_what; FUNCTION geo_shape.get_area : Real; END; rectangle = OBJECT(geo_shape) len : Real; FUNCTION rectangle.is_square : Boolean; PROCEDURE rectangle.init; OVERRIDE; FUNCTION rectangle.get_area : Real; OVERRIDE; END; circle = OBJECT(geo_shape) radius: Real; PROCEDURE circle.init; OVERRIDE; FUNCTION circle.get_area : Real; OVERRIDE; END; PROCEDURE geo_shape.init; BEGIN SELF.area := 0; SELF.height := 0; SELF.what := 'Geometric shape'; END; PROCEDURE geo_shape.say_what; BEGIN Writeln( SELF.what ); END; FUNCTION geo_shape.get_area : Real; BEGIN SELF.area := SELF.height * SELF.height; get_area := SELF.area; END; PROCEDURE circle.init; BEGIN INHERITED SELF.init; SELF.radius := 4; SELF.what := 'circle'; END; FUNCTION circle.get_area: Real; BEGIN SELF.area := Pi * Sqr( SELF.radius ); get_area := SELF.area; END; PROCEDURE rectangle.init; BEGIN INHERITED SELF.init; SELF.height := 5; SELF.len := 5; SELF.what := 'Rectangle'; END; FUNCTION rectangle.is_square: Boolean; BEGIN is_square := False; IF SELF.len = SELF.height THEN is_square := True; END; FUNCTION rectangle.get_area: Real; BEGIN SELF.area := SELF.len * SELF.height; get_area := SELF.area; END; VAR the_circle : circle; the_rect : rectangle; BEGIN New( the_circle ); the_circle.init; New( the_rect ); the_rect.init; the_circle.say_what; Writeln( 'area: ', the_circle.get_area ); Writeln; the_rect.say_what; Writeln( 'area: ', the_rect.get_area ); Dispose( the_circle ); Dispose( the_rect ); END.