I studied, with my poor knowledge and competence in computer science, the notion of inheritance and here is a little script which attempts to illustrate the question.
Please, would you critically examine it so as to let me know wether I've well understood the concept.
Code: Select all
#!/usr/bin/newlisp
(context 'rectangle)
; Class 'rectangle' with a constructor method
(define (rectangle:rectangle (width 30) (height 15))
(set 'w width)
(set 'h height)
(set 'nam "rectangle"))
(define (perimeter)
(string "(" w " + " h ") x 2 = " (mul (add w h) 2)))
(define (area)
(string w " x " h " = " (mul w h)))
(define (measure)
(println "A " nam " of " w " by " h)
(println "has a surface area of " (area) ",")
(println "and a perimeter of " (perimeter) ".\n"))
(context MAIN)
(context 'square)
; Class 'square' inherits from 'rectangle'
(new rectangle)
; Constructor of 'square'
(define (square:square (side 10))
(set 'w side)
(set 'h side)
(set 'nam "square"))
(context MAIN)
; main program
(new rectangle 'fig1)
(fig1 27 12)
(fig1:measure)
(new square 'fig2)
(fig2 13)
(fig2:measure)
;)